Question

I am working in Processing and I would like to compare the color of 2 the pixels of 2 different images.

let's say we comparing the pixel in position 10

color c1= image1.pixels[10]; color c2= image2.pixels[10];

if(c1==c2) { //so something }

Firstly I was playing with brightnsess

if(brightness(c1)==brightness(c2))

Generally it was working but not exactly as I wanted as the pixels were a little bit similar but not exactly the same color.

Was it helpful?

Solution

if you want to compare colours you are probably better off comparing the three basic ones instead of the actual number that "color" is. Thus instead of

if(c1 == c2) 

where you compare two large numbers like 13314249 you can go

if(red(c1) == red(c2) && green(c1) == green(c2) && blue(c1) == blue(c2)) 

where you compare numbers from 0 - 255, the possible values of red or green or blue you can get from a colour. As for the "little bit similar" colours, you can set a threshold and any difference below that threshold will be considered negligible thus the colours are the same. Something like this:

int threshold = 5
if(abs(red(c1) red(c2)) < threshold && abs(green(c1) - green(c2)) < threshold && abs(blue(c1) == blue(c2)) < threshold)

Remember, you have to take the absolute difference! This way, if you decrease the threshold only very similar colours are considered the same while is you increase it different colours can be considered the same. That threshold number depends on your likings!

This would also work with your brightness example...

int threshold = 5
if(abs(brightness(c1) - brightness(c2)) < threshold)

OTHER TIPS

To extend on Petros's answer. Generally, when I am comparing image pixels, I normalize, so that the code will work with images that are not in standard range 0-255. It also is good when you are doing many operations on the images to keep in mind the range you are currently working with for scaling purposes.

MAX_PIXEL=255 //maybe range is different for some reason
MIN_PIXEL=0

pixel_difference = 10
threshold = pixel_difference/(MAX_PIXEL-MIN_PIXEL)

if ( abs( (brightness(c1)-brightness(c2))/(MAX_PIXEL-MIN_PIXEL))< threshold ) {
  //then the pixels are similar.
}

Sometimes you can gain more ground by transforming to a difference color space.

And depending on your task at hand you can build a background model that can adapt over time or compare higher level global features such as histograms or local features such as Scale Invariant Feature Transform (SIFT), or Corners, Edges.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top