grayscale of images :
The process of transforming color images into grayscale images becomes the grayscale processing of images. The color of each pixel in a color image is determined by the R, G, and b three components, and each component has a value of 255, so that a pixel can have a range of more than 16 million (255*255*255) color variations. and gray image is R, G, b three components of a special color image, the range of one pixel change is 255, so in the digital image processing species generally first convert the image of various formats into grayscale image so that the subsequent image of the calculation of less. The description of grayscale image still reflects the distribution and characteristics of the whole and local chroma and luminance levels of the whole image as well as the color image. Grayscale processing of images can be achieved in two ways.
The first method causes the average of the R, G, and b three components of each pixel to be averaged, and then assigns the average value to the three components of the pixel.
The second method is based on the YUV color space, the physical meaning of Y component is the brightness of the point, reflected by this value brightness level, according to the RGB and YUV color space change relationship can be established brightness Y and R, G, b three color components corresponding: Y=0.3R+0.59G+0.11B, The grayscale value of the image is expressed with this luminance value.
/// <summary>
/// 图像灰度化
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public
static
Bitmap ToGray(Bitmap bmp)
{
for
(
int i = 0; i < bmp.Width; i++)
{
for
(
int
j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
//利用公式计算灰度值
int
gray = (
int
)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
Color newColor = Color.FromArgb(gray, gray, gray);
bmp.SetPixel(i, j, newColor);
}
}
return bmp;
}
|
Grayscale inversion :
Set the value 0 of the R, G, and b three components of each pixel to 255,255 to 0.
/// <summary>
/// 图像灰度反转
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public
static
Bitmap GrayReverse(Bitmap bmp)
{
for
(
int
i = 0; i < bmp.Width; i++)
{
for
(
int
j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
Color newColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
bmp.SetPixel(i, j, newColor);
}
}
return
bmp;
}
|
grayscale image binary :
After grayscale processing, each pixel in the image has only one value, which is the grayscale value of the pixel. Its size determines how bright the pixels are. In order to carry out the image processing operation more conveniently, we also need to do a binary processing of the gray image that has been obtained. The two value of the image is to differentiate the pixels in the image into two colors according to certain criteria. In the system, it is processed into black and white color according to the gray value of the pixel. Similar to grayscale, the two value of the image also has many mature algorithms. It can adopt the adaptive threshold method or the given threshold method.
/// <summary>
/// 图像二值化1:取图片的平均灰度作为阈值,低于该值的全都为0,高于该值的全都为255
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public
static
Bitmap ConvertTo1Bpp1(Bitmap bmp)
{
int
average = 0;
for
(
int
i = 0; i < bmp.Width; i++)
{
for
(
int
j = 0; j < bmp.Height; j++)
{
Color color = bmp.GetPixel(i, j);
average += color.B;
}
}
average = (
int
)average / (bmp.Width * bmp.Height);
for
(
int
i = 0; i < bmp.Width; i++)
{
for
(
int
j = 0; j < bmp.Height; j++)
{
//获取该点的像素的RGB的颜色
Color color = bmp.GetPixel(i, j);
int
value = 255 - color.B;
Color newColor = value > average ? Color.FromArgb(0, 0, 0): Color.FromArgb(255,
255, 255);
bmp.SetPixel(i, j, newColor);
}
}
return
bmp;
}
/// <summary>
/// 图像二值化2
/// </summary>
/// <param name="img"></param>
/// <returns></returns>
public
static
Bitmap ConvertTo1Bpp2(Bitmap img)
{
int
w = img.Width;
int
h = img.Height;
Bitmap bmp =
new
Bitmap(w, h, PixelFormat.Format1bppIndexed);
BitmapData data = bmp.LockBits(
new
Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
PixelFormat.Format1bppIndexed);
for
(
int
y = 0; y < h; y++)
{
byte
[] scan =
new
byte
[(w + 7) / 8];
for
(
int
x = 0; x < w; x++)
{
Color c = img.GetPixel(x, y);
if
(c.GetBrightness() >= 0.5) scan[x / 8] |= (
byte
)(0x80 >> (x % 8));
}
Marshal.Copy(scan, 0, (IntPtr)((
int
)data.Scan0 + data.Stride * y), scan.Length);
}
return
bmp;
}
|
C # image grayscale, grayscale inversion, binary