Jump to content
Mark Williams

Detect if image is color, greyscale or black white

Recommended Posts

Modern scanners on auto color detection mode seem to be very efficient in detecting whether a document is a text document and whether to scan it as monochrome even though the image contains some degree of color.

 

It has to be more than a pixel by pixel analysis to analyse the extent of the color in any image.

 

Does anyone have any idea how this is done or even better if they can point me o some example code?

 

I have searched on Google and the only example I can find is written in Python and I have no idea how to adapt for Delphi (Python example).

Share this post


Link to post

There is loads of stuff which is useful for this type of task at the old EFG graphics library site http://www.efg2.com.  The site still exists, but only links to the Wayback Machine now.  I needed to find backgrounds of light colours to change to white and of dark to change to black.  There is an EFG utility function CountColors which builds a sparse table of the frequrency distribution of each color, which you might find useful, and many others if you need something simpler.  Unfortunately I cannot remember which source file I found it in, but I could send you that routine if you cannot find it,  Choose a Wayback Machine snapshot a year or so ago (before the site closed) and have a browse.

 

Edit: the function I referred to is in showimage.zip in ImageProcessingPrimitives.pas. You can find the zip from Show Image in the main menu.

Edited by timfrost
more info

Share this post


Link to post

if the image file format is 32bit color bitmap, but the content is a black white picture, I think you can check it very quicklly by check pixels.

 

You donn't need to check every pixels of the picture, you just need check severial pixels samples in the picture.

Share this post


Link to post
7 minutes ago, pcplayer99 said:

if the image file format is 32bit color bitmap, but the content is a black white picture, I think you can check it very quicklly by check pixels.

 

You donn't need to check every pixels of the picture, you just need check severial pixels samples in the picture.

Thanks. I've sort of come up with something along those lines using Scanlines. It seems to be tolerably effective and sufficiently fast.

Share this post


Link to post
24 minutes ago, pcplayer99 said:

Using Scanlines is for every pixels. You donn't need check every pixels here.

Scanlines is fast enough for my purposes even when it scans every pixel. I would be reluctant to rely on random sampling. 

Share this post


Link to post
Guest

I suggest to at least skip one line when scanning, you can go up to 32 skipped lines while still have very low probability of false positive, you can tweak and decide what will work for you, just consider the height of the image, and i want to suggest to consider the divergence too, so lets say RGB with value like 05 05 05 is some grey shade but 06 05 05 is not, but very close and human eye might see it as grey and the scanner itself will not return white as pure white , for that use some formulas, 

I was going to suggest the fast simple formula with calculating average of R G B and then divide sum with it and compare it with a factor you can tweak but i think you got the idea and here an detailed answer to work with saturation 

https://stackoverflow.com/questions/16209111/given-the-rgb-components-of-a-color-how-can-i-decide-if-it-is-perceived-as-gray?noredirect=1&lq=1

 

Share this post


Link to post
Guest
7 minutes ago, Mark Williams said:

I would be reluctant to rely on random sampling. 

Exactly, skip lines not pixels.

Share this post


Link to post
5 minutes ago, Kas Ob. said:

I suggest to at least skip one line when scanning, you can go up to 32 skipped lines while still have very low probability of false positive, you can tweak and decide what will work for you, just consider the height of the image, and i want to suggest to consider the divergence too, so lets say RGB with value like 05 05 05 is some grey shade but 06 05 05 is not, but very close and human eye might see it as grey and the scanner itself will not return white as pure white , for that use some formulas, 

I was going to suggest the fast simple formula with calculating average of R G B and then divide sum with it and compare it with a factor you can tweak but i think you got the idea and here an detailed answer to work with saturation 

https://stackoverflow.com/questions/16209111/given-the-rgb-components-of-a-color-how-can-i-decide-if-it-is-perceived-as-gray?noredirect=1&lq=1

 

Ok. Thanks. I'll have a look at that and report back if I can improve on what I have already got.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×