C # Three digital image processing methods,
Source: http://zxlovenet.cnblogs.com
This article mainly introduces three methods for C # To process digital images through color image grayscale. Bitmap, BitmapData, and Graphics are three important methods for C # To process images.
As long as Bitmap is used to process the objects of images defined by pixel data, the main methods and attributes are as follows:
The GetPixel method and SetPixel method obtain and set the color of a specified pixel of an image.
The PixelFormat attribute returns the pixel format of the image.
Palette attribute, which is the color Palette used for obtaining or origami images.
Returns the Height and Width of the image.
The LockBits method and the UnlockBits method respectively lock and unlock bitmap pixels in the system memory.
The BitmapData object specifies the properties of the bitmap:
Height attribute, the Height of the locked bitmap.
The Width attribute of the locked bitmap.
The actual pixel format of the data.
Scan0 attribute, the first byte address of the locked array.
Stride attribute, Stride, also known as scan width.
Grayscale color images
Each pixel of a 24-bit color image is represented in three bytes. Each byte corresponds to the brightness (red, green, and blue) of the R, G, and B components ). When three components do not want to be displayed as grayscale images at the same time. There are three conversion formulas:
Gray (I, j) is the Gray value of the converted grayscale image at (I, j. Because the human eyes have different colors, the following conversion formula is provided:
We can see that green occupies the largest proportion. Therefore, G value is used for conversion:
Image processing methods include pixel, memory, and pointer. They have their own characteristics.
Pixel Extraction Method
The Bitmap. GetPixel and Bitmap. SetPixel methods in GDI + are used.
12345678910111213141516 |
if (bitmap != null ) { newbitmap = bitmap.Clone() as Bitmap; Color pixel; int ret; for ( int x = 0; x < newbitmap.Width; x++) { for ( int y = 0; y < newbitmap.Height; y++) { pixel = newbitmap.GetPixel(x, y); ret = ( int )(pixel.R * 0.299 + pixel.G * 0.587 + pixel.B * 0.114); newbitmap.SetPixel(x, y, Color.FromArgb(ret, ret, ret)); } } pictureBox1.Image = newbitmap.Clone() as Image; } |
Memory Method
The memory method is to directly copy image data to the memory, so that the program runs faster.
123456789101112131415161718192021222324 |
if (bitmap != null ) { newbitmap = bitmap.Clone() as Bitmap; Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height); System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat); IntPtr ptr = bmpdata.Scan0; int bytes = newbitmap.Width * newbitmap.Height * 3; byte [] rgbvalues = new byte [bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbvalues, 0, bytes); double colortemp = 0; for ( int i = 0; i < rgbvalues.Length; i += 3) { colortemp = rgbvalues[i + 2] * 0.299 + rgbvalues[i + 1] * 0.587 + rgbvalues[i] * 0.114; rgbvalues[i] = rgbvalues[i + 1] = rgbvalues[i + 2] = ( byte )colortemp; } System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes); newbitmap.UnlockBits(bmpdata); pictureBox1.Image = newbitmap.Clone() as Image; } |
Pointer Method
This method is similar to the memory method. At first, the LockBits method is used to obtain the first address of the bitmap. This method is more concise and uses pointers for bitmap operations. Therefore, the memory operation must be performed under unsafe.
1234567891011121314151617181920212223242526 |
if (bitmap != null ) { newbitmap = bitmap.Clone() as Bitmap; Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height); System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat); byte temp; unsafe { byte * ptr = ( byte *)(bmpdata.Scan0); for ( int x = 0; x < bmpdata.Width; x++) { for ( int y = 0; y < bmpdata.Height; y++) { temp = ( byte )(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]); ptr[0] = ptr[1] = ptr[2] = temp; ptr += 3; } ptr += bmpdata.Stride - bmpdata.Width * 3; } } newbitmap.UnlockBits(bmpdata); pictureBox1.Image = newbitmap.Clone() as Image; } |
3Comparison of Methods
By comparison, we can draw a conclusion that the pixel extraction method is relatively simple, but the efficiency is relatively low; the memory method is greatly improved, but the code is relatively complicated; the pointer method is more efficient than the memory method, but is not safe. In summary, the memory method is better, and the efficiency is high, and the advantages of C # security can be realized.