Determines whether a bitmap is a black-and-white image.

Source: Internet
Author: User

In recent work, it is necessary to determine whether the image is black or white. Because it is processed in large batches with more than 100,000 images, it is automatically determined by the program.

 

Image Object: processed jpg format Bitmap (profile photo)

 

Algorithm principle:

It is determined by the RGB value of each pixel. We know that bitmaps are composed of pixels, each of which has an RBG value. Therefore, we can use RGB values to determine whether the image is black or white.

[RGB]: R represents red, G Represents green, B Represents blue, and a variety of colors are formed through the principle of three primary colors.

 

Preliminary ideas:

1. Only black and white images can be regarded as black and white images.

2. gray-scale black and white images. Due to gray-scale images, it is not possible to determine whether the pixel is black or white. However, the gray-scale pixel RGB has the following features: [R = G = B]

 

Algorithm Optimization:

1. Black and white only, white and black color, white RGB [R = G = B = 255], black [R = G = B = 0 ];

2. grayscale, RGB [R = G = B ];

It can be seen that both black and white and gray scale are in RGB [R = G = B]

 

Problems:

Some images can be considered as black and white images, which may be green or red. [R = G = B] cannot be determined.

Solution:

Although the colors of these images [R <> B <> G] are roughly the same as those of gray-scale colors, the difference between R, G, and B is not very large. After my own tests, found this [color of black and white photos] All the pixels in the image of the R, G, B absolute maximum difference is not more than 50 (R-G, R-B, G-B), while the color of the image of the R, b. The maximum absolute value of G difference exists in pixels over 50.

 

In simple terms:

1. [custom] Color Deviation Value Diff = Max (| R-G |, | R-B |, | G-B | );

2. The maximum Diff value for all black and white images <50;

[P.S: This algorithm may have errors. Strictly speaking, R = G = B and Diff = 0 .]

 

Specific implementation:

[Csharp]
/// <Summary>
/// Determine whether the image is black or white
/// </Summary>
/// <Param name = "filename"> image file path </param>
/// <Returns> </returns>
Public bool isBlackWhite (string filename)
{
Color c = new Color ();
 
Using (Bitmap bmp = new Bitmap (filename ))
{
// Traverse the pixels of the image
For (int y = 0; y <bmp. Height; y ++)
{
For (int x = 0; x <bmp. Width; x ++)
{
C = bmp. GetPixel (x, y );
 
// Determine the color deviation Diff of the pixel
If (GetRGBDiff (c. R, c. G, c. B)> 50)
{
Return false;
}
}
}
 
Return true;
}
}
 
Public int GetRGBDiff (int r, int g, int B)
{
// Slightly, it is very easy to take the maximum value of the absolute values of r-g, r-B, and g-B.
}

/// <Summary>
/// Determine whether the image is black or white
/// </Summary>
/// <Param name = "filename"> image file path </param>
/// <Returns> </returns>
Public bool isBlackWhite (string filename)
{
Color c = new Color ();

Using (Bitmap bmp = new Bitmap (filename ))
{
// Traverse the pixels of the image
For (int y = 0; y <bmp. Height; y ++)
{
For (int x = 0; x <bmp. Width; x ++)
{
C = bmp. GetPixel (x, y );

// Determine the color deviation Diff of the pixel
If (GetRGBDiff (c. R, c. G, c. B)> 50)
{
Return false;
}
}
}

Return true;
}
}

Public int GetRGBDiff (int r, int g, int B)
{
// Slightly, it is very easy to take the maximum value of the absolute values of r-g, r-B, and g-B.
}
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.