views:

61

answers:

2

What's the best and most flexible algorithm to detect any black (or colored pixel) in a given image file?

Say I'm given an image file that could, say, have a blue background. And any non blue pixel, including a white pixel, is counted as a "mark". The function returns true if there are X number of pixels that deviate from each other at a certain threshold.

I thought it'd be fastest to just simply iterate through every pixel and see if its color matches the last. But if it's the case that pixel (0,0) is deviant, and every other pixel is the same color (and I want to allow at least a couple deviated pixels before considering an image to be "marked), this won't work or be terribly efficient.

A: 

You could always check nearby pixels, if pixel 3x3 is black then check 2x2, 2x3, 2x4, 3x2, 3x4 and so on and have a treshhold for how far away in colorvalue those nearby pixels can be for the black pixel to NOT count as deviation. and do the same for any color really. In a normal image every pixel should have some neighboring pixels with similar colors, there are very very few exceptions to this in med to high-res pictures.

Edit: Finding a treshhold that works can differ from image to image depending on a multitude of things so you'll likely have to be able to finetune the treshhold to get good results.

Jonas B
A: 

M1: scan the entire image.

M2: Xor with the color that you are searching for . For example you are looking for r,g,b = (112,233,35), XOR the first r layer with 112, g with 233 and b with 35. ( assuming its 24 bit image ( each layer is 8 bit) ). In the resulting image, find the brightest pixels, and go back to those pixels in the original image to check.

Egon