Search code examples
imagepngjpegbmp

Determine if image is photograph or drawing, quickly


Does anyone know of any very fast algorithms to determine if an image is a photograph or a drawing?

The main intention being to decide if or not the most appropriate format for a BMP image would be PNG or JPEG. I don't want files with lines to get blurred by jpeg, yet at the same time I do not want to be storing loss-less photographs.

At the moment I just pic the smallest file size out of a PNG and high quality JPEG. This seems to work for 99%. But there are always things like photos of drawings and drawings with lots of gradients that fool it.


Solution

  • you could use filters to 1. make the picture black and white and 2. pump up the contrast and then count the pixels and see if you get more white pixels then black / grey ones

    maybe like this:

    if( whitePixels.Count >= ( whitePixels.Count + blackAndGreyPixels.Count ) / 100 * 70 ) {
        // is drawing
    } else {
        // is photograph
    }
    

    you should test the 70% mark, it depends on the image size and the kind of image you are about to compare.

    hope this helps you to get a idea of how a SIMPLE algorythm could work.

    if you need more help on this you should say what langue do you want to use ( PHP for server side or maybe .NET for client side ) and then i could give you an example of the code.

    please vote / mark as answer if you find this useful.

    g.r. Ace