Source: http://zxlovenet.cnblogs.com
Pseudo-color processing refers to the conversion of grayscale images into color image. Because the human eye to the color resolution ability is much higher than the gray image resolution ability, so the gray image conversion to the color can enhance the human eye to the image detail the ability to discern. Pseudo-color does not reflect the color of the image as it really is.
:
intensity stratification method and gray level- Color Transform method:
(1) The intensity stratification method is the simplest one in pseudo-color processing technology.
A cut plane parallel to the X-y plane is set on a gray-level li, and the pixels below the cutting plane are assigned to a color with a gray level less than Li, and the corresponding cut plane is assigned to a different color than the gray-level Li pixel. The result of this cut can be divided into two layers of pseudo-color. You can use the M plane to cut, you will get m a different gray level of the area, so that the M color is a color image. Although this method is simple, the visual effect is not ideal.
(2) Gray level-color transformation method can transform the gray image into a continuous color image with multiple color gradients.
Mainly is the image through the different transformation characteristics of red, green, blue 3 converters, and then the output of three color channels to synthesize a color. Due to the difference of three color transformations, different sizes can be synthesized in different shades. A typical set of transformation transfer functions such as.
Note that the code can only be processed in JPG format grayscale image, because the color depth of the JPG image is 24-bit representation (r,g,b), each pixel is represented by 3 bytes, but the color depth of the PNG image is 32-bit representation (R,G,B,A).
The following code is the test code to deal with 24-bit depth of the image as an example, the same pixel color values of different channels to the same, the combination of the color is a certain grayscale. In practice, the following code needs to be modified according to the image format to be processed.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586878889909192939495 |
#region 伪彩色图像处理
/// <summary>
/// 伪彩色图像处理
/// 博客园-初行 http://www.cnblogs.com/zxlovenet
/// 日期:2014.2.14
/// </summary>
/// <param name="bmp">传入的灰度图像</param>
/// <param name="method">使用何种方法,false强度分层法,true灰度级-彩色变换法</param>
/// <param name="seg">强度分层中的分层数</param>
/// <returns>返回伪彩色图像</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];
//清零
Array.Clear(rgbValues, 0, bytes);
byte
tempB;
if
(method ==
false
)
{
//强度分层法
for
(
int
i = 0; i < bytes; i += 3)
{
byte
ser = (
byte
)(256 / seg);
tempB = (
byte
)(grayValues[i] / ser);
//分配任意一种颜色
rgbValues[i + 1] = (
byte
)(tempB * ser);
rgbValues[i] = (
byte
)((seg - 1 - tempB) * ser);
rgbValues[i + 2] = 0;
}
}
else
{
//灰度级-彩色变换法
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 mapping:
Color mapping method needs to make a color mapping table, different shades of gray will have a corresponding color. This is similar to the intensity stratification method, can be divided into different levels, the corresponding color can be mapped according to the actual situation.
In practical application, the infrared image produced by thermal imaging temperature measurement system is black-and-white grayscale image, the dynamic range of gray value is not big, the human eye is difficult to get rich information from these gray scale. In order to enhance the level of display image more visually, improve the resolution of human eyes, the image taken by the system is processed by pseudo-color processing, so as to achieve the effect of image enhancement and enrich the image information. For example, when the image of a heated object is pseudo-colored, the area with low gray level is set near the blue (or blue gray, black, etc.), and the area with high grayscale is set near red (or brown, white, etc.) to facilitate people's observation of objects.
The following pictures are in the actual application of the situation (Image source network):
C # pseudo-color processing