Image pair: bitmap in jpg format that has been processed (apture photo)
Algorithm principles:
The progress is judged by the RGB value per pixel point. We know that bitmaps are all formed by pixel points, and each pixel point has an RBG value, so you can use the RGB value to determine whether the picture is colored.
[RGB]: R represents red, G represents green, and B represents blue, and a colorful color is formed through the principles of three primary colors.
Starting thoughts:
1. For pure color pictures, you can only judge whether the color of the pixel is black or white. Once you encounter a non-color color, you can think of the length and short color pictures.
2. Grayscale color pictures, because there is a grayscale, they cannot be judged by whether the pixel points are colored, but the RGB of grayscale pixel points has a feature: [R=G=B]
Algorithm optimization:
1. Pure color, only white and black, white RGB [R=G=B=255], black color [R=G=B=0];
2. Grayscale, RGB【R=G=B】;
It can be seen that both color and gray scale are in RGB [R=G=B]
Encountered with problems:
Some of them can be thought of as color pictures, which are green or red, and cannot be judged by the [R=G=B] method.
Processing thoughts:
Although these pictures [R<>B<>G] are generally consistent in color and are close to grayscale color, so the difference between R, G, and B should not be very large. After my own test, I found that the absolute maximum difference of R, G, and B in this [color cast color photo] in the picture with pixel points does not exceed 50 (R-G, R-B, G-B), while the absolute maximum difference of R, B, and G in the color picture has pixel points with pixel points exceeding 50.
In short, it is:
1. [Custom] Color offset Diff = Max (|R-G|,|R-B|,|G-B|);
2. The largest color picture is Diff < 50;
[: There may be errors in this algorithm. Strictly speaking, R=G=B and Diff=0 are the correct ones. 】
Detailed reality:
/// <summary>
/// Determine whether the picture is colored
/// </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))
{
//Travel through the pixels of the picture
for (int y = 0; y < ; y++)
{
for (int x = 0; x < ; x++)
{
c = (x, y);
//Judge the color deviation value of the pixel point Diff
if (GetRGBDiff(, , ) > 50)
{
return false;
}
}
}
return true;
}
}
public int GetRGBDiff(int r,int g,int b)
{
//Online, very simple, it is to take the maximum value of the absolute value of r-g, r-b, g-b.
}