You are on page 1of 5

Viết chương trình xử lý ảnh thực hiện:

Open: đọc ảnh và hiển thị ảnh


Gray: Chuyển ảnh màu về ảnh xám
Binary: Chuyển ảnh màu về ảnh nhị phân
Light Image: Tăng độ sáng của ảnh
PHẦN CÀI ĐẶT
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.Drawing.Imaging;

namespace GrayIamge
{
public partial class Form1 : Form
{
Image file;
public Form1()
{
InitializeComponent();
}
//------------------------------
private void btnOpen_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
file = Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = file;
}
}
//-------------------------------
/* public static bool CovertToGray(Bitmap b)
{
// GDI+ return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width,
b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride; // bytes in a row 3*b.Width
IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
byte red, green, blue;
int nOffset = stride - b.Width*3;
for(int y=0; y < b.Height; ++y)
{
for(int x=0; x < b.Width; ++x )
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red
+ .587 * green + .114 * blue);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}*/
//-------------------------------
/*void GrayScale()
{
try
{
Bitmap img = new Bitmap(pictureBox1.Image);
Color c;
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
c = img.GetPixel(i, j);
int gray = (int)(0.29 * c.R + 0.59 * c.G + 0.11 * c.B);
c = Color.FromArgb(gray, gray, gray);
img.SetPixel(i, j, c);
}
}
pictureBox2.Image = img;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}*/
private Bitmap AnhXam(Bitmap bm)
{
//Bitmap bm = new Bitmap(bmp); // Lớp chỉ xem được file ảnh
Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
// Sử dụng lớp này để có thể chỉnh sửa\
//dùng LockBits để khóa, và chuyển từ một ảnh sang một vùng nhớ Bitmap
data
BitmapData bmData = bm.LockBits(rec, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Lấy số byte thực sự mà máy lưu trữ mỗi hàng của ảnh
int stride = bmData.Stride;
int nOffset = stride - bm.Width * 3; // Phần rìa của bức ảnh
// Địa chỉ đầu của ảnh
IntPtr ptr = bmData.Scan0;
int bytes = bm.Width * bm.Height * 3; // Số byte của ảnh
byte[] rgbValue = new byte[bytes]; // Mảng các pixel
//Copy dữ liệu từ vùng nhớ của ảnh vào mảng rgbValue trên RAM -->Xu
ly nhanh
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValue, 0, bytes);
//-------------------------------
//-- Các thuật toán XLA có thể cài ở đây
// Ví dụ: Xám hóa ảnh màu
for (int i = 0; i < bytes; i += 3)
{
byte tam = (byte)(.299 * rgbValue[i] + .587 * rgbValue[i + 1] + .
114 * rgbValue[i + 2]);
rgbValue[i] = rgbValue[i + 1] = rgbValue[i + 2] = tam;
}
//--------------- Đã xử lý xong
// chép kq ngược trở lại vào vùng nhớ ảnh
System.Runtime.InteropServices.Marshal.Copy(rgbValue, 0, ptr, bytes);
bm.UnlockBits(bmData);
//pictureBox2.Image = (Image)bm;
return bm;
}
//-------------------------------
private void btnGray_Click(object sender, EventArgs e)
{
//GrayScale();
pictureBox2.Image =(Image)AnhXam((Bitmap)pictureBox1.Image);
}
//-------------------------------

//-------------------------------
private Bitmap AnhSang(Bitmap bm, byte c)
{
Rectangle rec = new Rectangle(0, 0, bm.Width, bm.Height);
// Sử dụng lớp này để có thể chỉnh sửa
//dùng LockBits để khóa, và chuyển từ một ảnh sang một vùng nhớ Bitmap
data
BitmapData bmData = bm.LockBits(rec, ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb);
// Lấy số byte thực sự mà máy lưu trữ mỗi hàng của ảnh
int stride = bmData.Stride;
int nOffset = stride - bm.Width * 3; // Phần rìa của bức ảnh
// Địa chỉ đầu của ảnh
IntPtr ptr = bmData.Scan0;
int bytes = bm.Width * bm.Height * 3; // Số byte của ảnh
byte[] rgbValue = new byte[bytes]; // Mảng các pixel
//Copy dữ liệu từ vùng nhớ của ảnh vào mảng rgbValue trên RAM -->Xu
ly nhanh
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValue, 0, bytes);
//-------------------------------
//-- Các thuật toán XLA có thể cài ở đây
for (int i = 0; i < bytes; i += 3)
{
rgbValue[i] += c;
rgbValue[i + 1] += c;
rgbValue[i + 2] += c;
}
//--------------- Đã xử lý xong
// chép kq ngược trở lại vào vùng nhớ ảnh
System.Runtime.InteropServices.Marshal.Copy(rgbValue, 0, ptr, bytes);
bm.UnlockBits(bmData);
return bm;
}
//-------------------------------

private void BinaryImage(int band)


{
try
{
Bitmap img = new Bitmap(pictureBox1.Image);
Color c;
for (int i = 0; i < img.Width; i++)
{
for (int j = 0; j < img.Height; j++)
{
c = img.GetPixel(i, j);
int gray = (c.R + c.G + c.B) / 3;
if (gray < band)
{
c = Color.FromArgb(0, 0, 0);
img.SetPixel(i, j, c);
}
else
{
c = Color.FromArgb(255, 255, 255);
img.SetPixel(i, j, c);
}
}
}
pictureBox3.Image = img;

}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
//-------------------------------
/*private Bitmap ToBinaryLocBits(Bitmap bm, Byte band)
{
Bitmap bitmap = new Bitmap(bm);
Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(rec,
System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = bm.Width*bm.Height*3;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
Byte color;
for (int counter = 0; counter < bytes; counter += 4)
{
color = rgbValues[counter];
if (color < band)
{
rgbValues[counter + 0] = 0;
rgbValues[counter + 1] = 0;
rgbValues[counter + 2] = 0;
}
else
{
rgbValues[counter + 0] = 255;
rgbValues[counter + 1] = 255;
rgbValues[counter + 2] = 255;
}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
bitmap.UnlockBits(bmpData);
return bitmap;
}*/
//---------------------------------------
private void btnBinary_Click(object sender, EventArgs e)
{
BinaryImage(128);

private void btnLightImage_Click(object sender, EventArgs e)


{
pictureBox4.Image = (Image)AnhSang((Bitmap)pictureBox1.Image,12);
}
//-------------------------------
}
}

You might also like