PHP: How to distinguish a black and white image from a color?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
4 views
This two images. From the point of view of the person one colored and the other black and white. It is necessary to distinguish them on the basis of color using PHP/GD2.


The only thing I found is the function of colour is called imageistruecolor, but the problem is that both images have the full colour palette and the function execution result is the same — the comparison fails.


Found a suggestion to bypass all the pixels of the image and determine the colors of each of them using ImageColorAt. Perhaps this is an option, but I do not want (if the image is large, the bypass will take much time if first it reduce inefficient spending resources on converting).


Maybe I missed something and there are "green" solutions?
by | 4 views

7 Answers

0 like 0 dislike
Tresaith one pixel to view its color.
If I correctly imagine this pixel will be the average color of the entire picture.
\r
Files in different formats or some specific?
by
0 like 0 dislike
Specify you need a black and white image, monochrome, or visual-looking black-and-white/monochrome. And in a certain format or arbitrary. Because in the formats of lossy compression (e.g. JPEG) black and white image will not, even if it is at a glance this appears to
\rimage,
and color can visually look monochrome.
\r
More pixels just does not work
by
0 like 0 dislike
To rotblit with a monstrous radius and look at every pixel.
by
0 like 0 dislike
And if you divide the image into squares and in each test selectively n pixels? More pixels to check, the more accurate the result.
by
0 like 0 dislike
As a variant — it is possible to use approximate "Monte Carlo": take n random pixels, by checking the equality of values of all color channels. The greater the number of samples, the greater the likelihood that the image is grey-scale.
\r
Naturally, when the first color pixel to return a negative result.
\r
The result will not be 100% accurate but relatively fast.
by
0 like 0 dislike
1. The algorithm for grey-scale only.
1.1. We have the formula of obtaining the probability that the pixel black-and-white Sij=(Rij+Gij+Bij)/3; Pij = 1 — (|Sij-Rij| + |Sij-Gij| + |Sij-Bij|)/765;
1.2. Set the resulting probability T = 1. Begin to scan a picture on a grid in increments of 1-50 pixels or randomly a few pixels; the more we scan the more plausible our probability. For each of the scanned pixels find Pij; T = T * Pij; If the probability fell below 0.9, then stop scanning and say that the picture is not black and white when it comes to the end, then we say that black and white.
2. Algorithm of obtaining of the palette.
2.1. Run through the picture collect all the colors if the color is greater than 500 the picture is not black and white, if less, then find the probability of kinship colors in tone on the basis of probability say that the picture is b/W or Sepia, etc.
by
0 like 0 dislike
A bit unclear: by "black and white" is meant the bit pattern (with two colors) or grayscale?
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
27,913 users