1. Image.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace binary
{
class Image
{
Bitmap bitmap;
Int32[,] r, g, b;// lấy 3 màu từ bitmap
int maxR = 0, maxG = 0, maxB = 0;
int minR = 255, minG = 255, minB = 255;
int h, w;//lấy chiều rộng và chiều cao
public Image(Bitmap bm)
{
this.bitmap = bm;
h = bm.Height;
w = bm.Width;
r = new Int32[w, h];
g = new Int32[w, h];
b = new Int32[w, h];
setImage();
}
private void setImage()// lấy 3 màu từ bitmap ra 3 ma trận màu r,g,b
{
int x = 0;
int y = 0;
Bitmap bm1 = new Bitmap(bitmap);//copy bitmap sang bmd
Rectangle rec = new Rectangle(0, 0, bm1.Width, bm1.Height);//taọ 1 hcn có kích thước như bitmap
BitmapData bmd = bm1.LockBits(rec, ImageLockMode.ReadWrite, bm1.PixelFormat);
IntPtr p = bmd.Scan0;
Int32 bytes = Math.Abs(bmd.Stride) * bm1.Height;
Byte[] m = new Byte[bytes];//mảng các điểm ảnh bm
System.Runtime.InteropServices.Marshal.Copy(p, m, 0, bytes);
for (int i = 0; i < m.Length - 1; i += 4)
{
if (x == w)
{
x = 0;
y++;
}
if (y > h) break;
r[x, y] = Convert.ToInt16(m[i]);
g[x, y] = Convert.ToInt16(m[i + 1]);
b[x, y] = Convert.ToInt16(m[i + 2]);
maxR = Math.Max(maxR, r[x, y]);
maxG = Math.Max(maxG, g[x, y]);
maxB = Math.Max(maxB, b[x, y]);
minR = Math.Min(minR, r[x, y]);
minG = Math.Min(minG, g[x, y]);
minB = Math.Min(minB, b[x, y]);
x++;
}
System.Runtime.InteropServices.Marshal.Copy(m, 0, p, bytes);
bm1.UnlockBits(bmd);
}
public Bitmap getImage()
{
return bitmap;
}
public int[,] getRed()
{
return r;
}
public int[,] getGreen()
{
return g;
}
public int[,] getBlue()
{
return b;
}
public int getMaxR()
{
return maxR;
}
public int getMaxG()
{
return maxG;
}
public int getMaxB()
{
return maxB;
}
public int getMinR()
{
return minR;
}
public int getMinG()
{
return minG;
}
public int getMinB()
{
return minB;
}
}
}
2. ShowImage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace binary
{
class ShowImage
{
Int32[,] r, g, b;
Bitmap bitmap;
int w, h;
public ShowImage(Bitmap bm,int[,]r,int[,]g,int[,]b)
{
this.r = r;
this.g = g;
this.b = b;
this.bitmap = bm;
w = bm.Width;
h = bm.Height;
}
public Bitmap show()// đưa 3 màu r,g,b trở lji bitmap sau xử lí
{
int x = 0;
int y = 0;
Bitmap bm = new Bitmap(bitmap);
Rectangle rec = new Rectangle(0,0,bm.Width,bm.Height);
BitmapData bmd = bm.LockBits(rec,ImageLockMode.ReadWrite,bm.PixelFormat);
IntPtr p = bmd.Scan0;
Int32 bytes = Math.Abs(bmd.Stride) * bm.Height;
Byte[] m= new Byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(p,m,0,bytes);
for (int i = 0; i < m.Length - 1; i += 4)
{
if (x == w)
{
x = 0;
y++;
}
if (y == h) break;
m[i] = Convert.ToByte(r[x, y]);
m[i + 1] = Convert.ToByte(g[x, y]);
m[i + 2] = Convert.ToByte(b[x, y]);
x++;
}
System.Runtime.InteropServices.Marshal.Copy(m,0,p,bytes);
bm.UnlockBits(bmd);
return bm;
}
}
}
3. Tobinary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace binary
{
class ToBinary
{
Image img;
int h, w;
Bitmap bm;
public ToBinary(Image img)
{
this.img = img;
this.bm = img.getImage();
h = bm.Height;
w = bm.Width;
}
public Bitmap toBinary(int X, int Y, int HEIGHT, int WIDTH, int band)
{
//tạo mảng màu của ảnh xử lý
int[,] r = img.getRed();
int[,] g = img.getGreen();
int[,] b = img.getBlue();
//chuyển màu vùng đã chọn
int x0, y0;
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++)
{
x0 = x + X;
y0 = y + Y;
int gray = (img.getRed()[x0, y0] + img.getGreen()[x0, y0] + img.getBlue()[x0, y0]) / 3;
if (gray > band)
r[x0, y0] = g[x0, y0] = b[x0, y0] = 255;
else
r[x0, y0] = g[x0, y0] = b[x0, y0] = 0;
}
ShowImage s = new ShowImage(bm, r, g, b);
return s.show();
}
public int getHeigh()
{
return h;
}
public int getWidth()
{
return w;
}
}
}
4. Form1.cs
4. Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace binary
{
public partial class Form1 : Form
{
private Graphics graph;
private Image image;
private Bitmap bitmap;
private int X, Y, WIDTH, HEIGHT, band;
private bool isClick = false, flag = false, load = false;
private string filename;
public Form1()
{
InitializeComponent();
graph = pictureBox1.CreateGraphics();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)//sự kiện nhấn chuột
{
if (!isClick && load)
{
X = e.X;
Y = e.Y;
HEIGHT = 0;
WIDTH = 0;
isClick = true;
flag = true;
textBox1.Text = "X: " + X + " - Y: " + Y + " - WIDTH: " + WIDTH + " - HEIGHT: " + HEIGHT;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)//sự kiện thả chuột
{
if (isClick)
{
graph.DrawRectangle(Pens.GreenYellow, X, Y, WIDTH - 1, HEIGHT - 1);
isClick = false;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)//sự kiện rê chuột
{
if (isClick)
{
if (e.X - X != WIDTH)
{
pictureBox1.Invalidate();
}
HEIGHT = e.Y - Y;
WIDTH = e.X - X;
HEIGHT = e.Y < pictureBox1.Height ? HEIGHT : pictureBox1.Height - Y;
WIDTH = e.X < pictureBox1.Width ? WIDTH : pictureBox1.Width - X;
textBox1.Text = "X: " + X + " - Y: " + Y + " - WIDTH: " + WIDTH + " - HEIGHT: " + HEIGHT;
graph.DrawRectangle(Pens.GreenYellow, X, Y, WIDTH - 1, HEIGHT - 1);
}
}
void load_image()
{
Bitmap bm = new Bitmap(filename);
image = new Image(bm);
int[,] r, g, b;
r = image.getRed();
g = image.getGreen();
b = image.getBlue();
ShowImage showImage = new ShowImage(bm, r, g, b);
pictureBox1.Image = showImage.show();
bitmap = showImage.show();
int maxRGB = Math.Max(image.getMaxB(), Math.Max(image.getMaxR(), image.getMaxG()));
int minRGB = Math.Min(image.getMinB(), Math.Min(image.getMinR(), image.getMinG()));
band = minRGB + (maxRGB - minRGB) / 2;
}
private void bt_load_Click(object sender, EventArgs e)//button laoad image
{
openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;//nhớ đường dẫn
openFileDialog.FileName = "";//chứa đường dẫn của load image
DialogResult kq = openFileDialog.ShowDialog();
if (kq == DialogResult.OK)
{
filename = openFileDialog.FileName;
pictureBox1.BackgroundImage = null;
load_image();
load = true;
}
}
private void bt_toBinary_Click(object sender, EventArgs e)//button ToGray
{
if (flag && load)
{
ToBinary img = new ToBinary(image);
int h = img.getHeigh();
int w = img.getWidth();
HEIGHT = Math.Min(HEIGHT, h - Y);
WIDTH = Math.Min(WIDTH, w - X);
Bitmap bm = img.toBinary(X, Y, HEIGHT, WIDTH, band);
image = new Image(bm);
pictureBox1.Image = bm;
graph.DrawRectangle(Pens.SteelBlue, X, Y, WIDTH, HEIGHT);
flag = false;
}
if (!load)
MessageBox.Show("Vui lòng mở ảnh!");
}
private void bt_reset_Click(object sender, EventArgs e)//button reset
{
load_image();
flag = false;
}
}
}