C # pseudo-color processing,
Source: http://zxlovenet.cnblogs.com
Pseudo-Color Processing refers to converting a gray image into a color image. Because the human eye's color resolution capability is much higher than the gray-scale image resolution capability, converting a gray-scale image into a color can improve the human eye's ability to identify image details. Pseudo-color does not actually reflect the color of the image.
:
Intensity layering and grayscale-Color Conversion Method:
(1) The intensity layering method is the simplest pseudo-color processing technology.
Set a cut plane parallel to the x-y plane on a gray level Li. The pixels below the cut plane are allocated to a color with a gray level less than Li, the corresponding cutting plane is larger than the gray-level Li pixels allocated to another color. In this way, the cut result can be divided into two layers of pseudocolor. You can use M flat to cut the image, and then you can get M gray-level areas. In this way, a color image with M colors is displayed. This method is simple, but the visual effect is not ideal.
(2) grayscale-color transformation can convert grayscale images into continuous color images with multiple color variations.
The main difference is to convert the image into three transformer modes: Red, green, and blue, and then combine the output of the three color channels into a certain color. Because of the differences in the three colors, different colors can be synthesized at different gray levels. A set of typical transform transfer functions are shown in figure.
Note that the code can only process JPG grayscale images, because the color depth of JPG images is 24 bits (R, G, B ), each pixel is represented by three bytes. However, the color depth of a png image is 32 bits (R, G, B, ).
The following code is a test code. Taking a 24-bit depth image as an example, the color values of different channels in the same pixel must be the same, and the combination indicates that the image has a certain gray color. In practical applications, You need to modify the following code based on the image format to be processed.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
# Region pseudo-Color Image Processing /// <summary> /// Pseudo-Color Image Processing /// Blog garden-first line http://www.cnblogs.com/zxlovenet /// Date: 2014.2.14 /// </summary> /// <Param name = "bmp"> input grayscale image </param> /// <Param name = "method"> which method is used, false strength layering, true grayscale-color conversion </param> /// <Param name = "seg"> Number of layers in the strength layer </param> /// <Returns> returns a pseudo-color image </returns> private Bitmap gcTrans(Bitmap bmp, bool method, byte seg) { if (bmp != null ) { if (System.Drawing.Imaging.PixelFormat.Format24bppRgb == bmp.PixelFormat) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = bmp.Width * bmp.Height * 3; byte [] grayValues = new byte [bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); bmp.UnlockBits(bmpData); byte [] rgbValues = new byte [bytes]; // Clear Array.Clear(rgbValues, 0, bytes); byte tempB; if (method == false ) { // Strength layering Method for ( int i = 0; i < bytes; i += 3) { byte ser = ( byte )(256 / seg); tempB = ( byte )(grayValues[i] / ser); // Assign any color rgbValues[i + 1] = ( byte )(tempB * ser); rgbValues[i] = ( byte )((seg - 1 - tempB) * ser); rgbValues[i + 2] = 0; } } else { // Grayscale-Color Conversion Method for ( int i = 0; i < bytes; i += 3) { if (grayValues[i] < 64) { rgbValues[i + 2] = 0; rgbValues[i + 1] = ( byte )(4 * grayValues[i]); rgbValues[i] = 255; } else if (grayValues[i] < 128) { rgbValues[i + 2] = 0; rgbValues[i + 1] = 255; rgbValues[i] = ( byte )(-4 * grayValues[i] + 2 * 255); } else if (grayValues[i] < 192) { rgbValues[i + 2] = ( byte )(4 * grayValues[i] - 2 * 255); rgbValues[i + 1] = 255; rgbValues[i] = 0; } else { rgbValues[i + 2] = 255; rgbValues[i + 1] = ( byte )(-4 * grayValues[i] + 4 * 255); rgbValues[i] = 0; } } } bmp = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); ptr = bmpData.Scan0; System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); return bmp; } else { return null ; } } else { return null ; } } #endregion |
Color ing:
The color ing method requires a color ing table. Different gray levels have corresponding colors. Similar to the intensity layering method, this method can be divided into different layers, and the corresponding color can be mapped based on the actual situation.
In practical applications, the infrared images produced by the thermal imaging temperature measuring system are black and white gray-scale images, and the gray-scale value does not have a dynamic range. It is difficult for human eyes to obtain rich information from these gray-scale images. In order to enhance the level of the displayed image more intuitively, improve the resolution capability of the human eye, and perform pseudo-color processing on the image taken by the system, the image enhancement effect can be achieved and the image information can be enriched. For example, when the image of a heated object is pseudo-colored, the area with low gray scale is set near blue (or blue gray or black ), areas with high gray level are set near Red (or brown, white, etc.) to facilitate observation of objects.
The following images are used in actual applications (image source network ):