C
C
ChemAli2011-01-27 17:16:33
PHP
ChemAli, 2011-01-27 17:16:33

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

Two images are given. From a human perspective, one is in color and the other is in black and white. You need to distinguish them on the basis of color using PHP / GD2.
The only color detection function I found is called imageistruecolor, but the problem is that both images have a full color palette and the result of the function is the same - the comparison fails.
Found a suggestion to loop through all the pixels in an image and determine the color of each one using ImageColorAt. Probably, this is an option, but I don’t want to (if the image is large, the bypass will take a lot of time, if it is reduced beforehand, we waste resources on conversion).
Maybe I missed something and there are "green" solutions?

Answer the question

In order to leave comments, you need to log in

11 answer(s)
A
Alex_EXEcuter, 2011-01-27
@Alex_EXEcuter

Resize to one pixel, see its color.
If I imagine it correctly, this pixel will have the average color of the entire picture.
Are the files in different formats or a specific one?

H
holivar, 2011-01-27
@holivar

Specify whether you need a black and white image, monochrome, or visually looking black and white / monochrome. And in a certain format or arbitrary. Because in formats with loss of quality during compression (for example, JPEG), there will be no black and white image, even if it looks like it
,
and a color image may visually look monochrome.
Pixel art won't work for sure.

Y
Yuri Morozov, 2011-01-27
@metamorph

Blur with a monstrous radius and look at any pixel.

S
Sererator, 2011-01-27
@Serator

And if you divide the image into squares and selectively check n pixels in each? The more pixels to check, the more accurate the result.

V
vril, 2011-01-27
@vril

Alternatively, you can use the approximate "Monte Carlo method": take n random pixels, checking the equality of the values ​​of all color channels. The greater the number of samples, the greater the likelihood that the image is grayscale.
Naturally, at the very first color pixel, return a negative result.
The result will not be 100% accurate, but relatively fast.

M
Mikhail Davydov, 2011-01-27
@azproduction

1. Algorithm for gray scale online.
1.1. We have a formula for obtaining the probability that a pixel is black and white Sij=(Rij+Gij+Bij)/3; Pij = 1 - (|Sij-Rij| + |Sij-Gij| + |Sij-Bij|)/765;
1.2. We set the final probability T = 1. We start scanning the image along the grid with a step of 1-50 pixels or a few pixels randomly; the more we scan, the more likely our probability is. For each of the scanned pixels, we find Pij; T = T * Pij; If the final probability fell below 0.9, then we stop scanning and say that the picture is not black and white, if it reaches the end, then we say that it is black and white.
2. Algorithm with obtaining a palette.
2.1. We run through the picture, collect all the colors if there are more than 500 colors, the picture is not black and white, if less, then we find the probability of color relatedness in tone, based on the probability we say that the picture is black and white or sepia, etc.

D
Dunadan, 2011-01-27
@Dunadan

It's a little unclear: by "black and white" is meant exactly a bitmap (with two colors) or shades of gray?

S
sl4mmer, 2011-01-27
@sl4mmer

ImageColorAt is a normal option. On medium-sized pictures (500x500 for example), the processing speed is good (I somehow did the conversion of the image to grayscale for the gallery), but to save resources, you can, indeed, as suggested above, iterate not all pixels, but let's say every fifth one in length in height ( this for example).

N
Nikolai Vasilchuk, 2011-01-27
@Anonym

The first thing that comes to mind is to make it black and white again and compare it with the original. Ideally, a “double black and white” image should be the same as a “once black and white” image.

G
gro, 2011-01-28
@gro

>The only color detection function I found is called imageistruecolor, but the problem is that both images have a full color palette and the result of the function is the same - the comparison fails.
check palette?

A
Anatoly, 2011-01-29
@taliban

If there is no serious direct task, then you can get by with resizing (let's say 10x10) + ImageColorAt and sorting through all the colors, then you will not face high costs if the picture is large, but you can also miss the color if there are few color areas. But if your color pictures are clearly colored and monochrome are clearly monochrome, this method will help not bad.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question