Colour matching between two images

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
ekke85
Posts: 4
Joined: 2016-12-05T08:51:19-07:00
Authentication code: 1151

Colour matching between two images

Post by ekke85 »

Hi Guys,

If this has been asked else where, please point me to it (I could not find it so far in the forum).
I keep fish and the test kits for the water is really bad and some of the colour is so close to one another that it is hard to figure out what it is. I was thinking I might be able to use imagemagick to compare the water colour (test vial) to the colour chart.
That way I can take a picture of the vial on a white background and run a script to tell me what the correct colour reading is.

Here is an example of the colour chart and also a picture of a vial on a white background:
http://i.imgur.com/07HVbGN.jpg
http://i.imgur.com/eKfYotr.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colour matching between two images

Post by snibgo »

If you take a photo of the test tube, and another photo of the chart, you probably can't use that to measure a tube against a chart. The reason is (a) the colour of the lighting may be different between the photos and (b) the camera will do some kind of colour-balancing, probably different for the two photos.

You might overcome (a) by ensuring the lighting is always exactly the same, and (b) by using raw photos without auto-white balance.

Having the tube and chart on one photo solves those problems.

On the photo with tube and chart, there is almost no difference between the colours of 40 ppm and 80 ppm. I guess the colour in the tube is closest to one of those, but which?
snibgo's IM pages: im.snibgo.com
ekke85
Posts: 4
Joined: 2016-12-05T08:51:19-07:00
Authentication code: 1151

Re: Colour matching between two images

Post by ekke85 »

thanks for the quick reply.
"On the photo with tube and chart, there is almost no difference between the colours of 40 ppm and 80 ppm. I guess the colour in the tube is closest to one of those, but which?" - Is exactly my problem.
These test kits kinda suck with their colours being so close to one another, but I think in that picture it is 40 ppm.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colour matching between two images

Post by snibgo »

It depends on how we measure it. Going by hue, the best match is with 160 ppm. To see this, isolate just the hue channel. Windows CMD syntax:

Code: Select all

convert tubeCols.jpg -colorspace HCL -channel G -evaluate set 100% -channel B -evaluate set 50% +channel -colorspace sRGB x.png
I have set chroma (saturation) to 100% and lightness to 50%, so we have only hue. The best match, visually, is with 160.

Another way:

Code: Select all

convert tubeCols.jpg -colorspace HCL -channel R -separate x2.png
This shows hue as a grayscale. The tube and 160 show as white, which is just one side of exactly red (0% == 100%). The other samples on the right are near black, so they are the other side of exactly red.
snibgo's IM pages: im.snibgo.com
ekke85
Posts: 4
Joined: 2016-12-05T08:51:19-07:00
Authentication code: 1151

Re: Colour matching between two images

Post by ekke85 »

wow that is amazing, thanks!
I've been playing with different pictures in different light conditions and time and time again you can see the difference and how colours match. That is amazing thanks, do you think there is a way I can automate it so I can just give it an image and it recognises which colour it is and just give me a stand out like 160ppm or do you think I am asking a little but too much? :P
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colour matching between two images

Post by snibgo »

I crop four areas, from the last four chart samples (20, 40, 80 and 160 ppm), and scale each to a single pixel, and append these side by side.

Then I make a sample from the lightest part of the test tube, and scale that to the same size.

Take the difference of these 4x1 images, and find the RMS of the colour channels. Write the result as text.

Windows BAT syntax:

Code: Select all

convert ^
  tubeCols.jpg +write mpr:TC -delete 0 ^
  ( mpr:TC -crop 400x200+1300+1500 +repage ) ^
  ( mpr:TC -crop 400x200+1300+1920 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2330 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2730 +repage ) ^
  -scale "1x1^!" ^
  +append ^
  ( mpr:TC -crop 100x700+870+1700 +repage -scale "4x1^!" ) ^
  -compose Difference -composite ^
  -grayscale RMS ^
  txt:
The result is:

Code: Select all

# ImageMagick pixel enumeration: 4,1,65535,gray
0,0: (17347,17347,17347)  #434343  gray(67)
1,0: (10409,10409,10409)  #292929  gray(41)
2,0: (10251,10251,10251)  #282828  gray(40)
3,0: (16932,16932,16932)  #424242  gray(66)
The third pixel has the lowest (darkest) gray, so that is the best match, ie 80ppm, by this measure.

However, if we use Lab colorspace instead of sRGB:

Code: Select all

convert ^
  tubeCols.jpg -colorspace Lab +write mpr:TC -delete 0 ^
  ( mpr:TC -crop 400x200+1300+1500 +repage ) ^
  ( mpr:TC -crop 400x200+1300+1920 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2330 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2730 +repage ) ^
  -scale "1x1^!" ^
  +append ^
  ( mpr:TC -crop 100x700+870+1700 +repage -scale "4x1^!" ) ^
  -compose Difference -composite ^
  -grayscale RMS ^
  txt:

Code: Select all

# ImageMagick pixel enumeration: 4,1,65535,gray
0,0: (6872,6872,6872)  #1B1B1B  gray(27)
1,0: (4100,4100,4100)  #101010  gray(16)
2,0: (4365,4365,4365)  #111111  gray(17)
3,0: (10497,10497,10497)  #292929  gray(41)
Here, the second sample is closest.

Perhaps we should use Lab but exclude Lightness from the calculation:

Code: Select all

convert ^
  tubeCols.jpg -colorspace Lab ^
  -channel R -evaluate set 50%% +channel ^
  +write mpr:TC -delete 0 ^
  ( mpr:TC -crop 400x200+1300+1500 +repage ) ^
  ( mpr:TC -crop 400x200+1300+1920 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2330 +repage ) ^
  ( mpr:TC -crop 400x200+1300+2730 +repage ) ^
  -scale "1x1^!" ^
  +append ^
+write info: ^
  ( mpr:TC -crop 100x700+870+1700 +repage -scale "4x1^!" ) ^
  -compose Difference -composite ^
  -grayscale RMS ^
  txt:

Code: Select all

# ImageMagick pixel enumeration: 4,1,65535,
0,0: (6416,6416,6416)  #191919  gray(25)
1,0: (3616,3616,3616)  #0E0E0E  gray(14)
2,0: (3848,3848,3848)  #0F0F0F  gray(15)
3,0: (6994,6994,6994)  #1B1B1B  gray(27)
Again, the second sample is the closest.

As you can see, the result depends on what colour model we choose to measure the "closeness" of the colour patches.

If the elements are in the same positions in all photos, the same code will work.
snibgo's IM pages: im.snibgo.com
ekke85
Posts: 4
Joined: 2016-12-05T08:51:19-07:00
Authentication code: 1151

Re: Colour matching between two images

Post by ekke85 »

WOW that is amazing, thank you so much.
I will create some sort of template to make sure the pictures is always the same.
Thanks again!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colour matching between two images

Post by snibgo »

This could be extended to a script that simply wrote "80 ppm" or whatever the closest match was. However, I think it is useful to also write the numbers, so you can see the closeness of other patches.

Perhaps you should also include the other colour patches. The second scale should be "-scale Nx1" where N is the number of patches.

When taking the photos, be careful not to obscure a patch with your fingers, or the shadow of your fingers.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Colour matching between two images

Post by snibgo »

Oops, I messed up, sorry.

For the patch of the test tube, I scaled it directly to 4x1. But I should instead have scaled to 1x1, then up to 4x1. So that line in all commands should read:

Code: Select all

  ( mpr:TC -crop 100x700+870+1700 +repage -scale "1x1^!" -scale "4x1^!" ) ^
This doesn't make much difference, but is enough to tip the balance so in the three cases, the second patch is closest.
snibgo's IM pages: im.snibgo.com
Post Reply