I'm building a simple device for my lab which actually captures images at specific intervals. Here is a sequence of images for example.
While scrolling down, you will notice that the color of the liquid inside those tubes is changing. So what I wanna achieve is to programmatically calculate an index that corresponds to the difference between the liquid colors in the two cases. I've never before dealt with image analysis and thus I would like to hear your opinions.
So far I was able to crop a specific area from the images and do simple math to calculate if there is a difference. After cropping the image, I'm resizing it at 1x1 pixel in order to get the average color of it. Finally, as you can see I'm using only the Green and Blue channel of the RGB because Red one didn't have so much variance.
Here is the example code in python:
from PIL import Image
import os
img_dir = "./sal1000-2"
area_left = (250,340,350+16,340+37)
area_right = (419,340,419+16,340+37)
left_init = ()
right_init = ()
left = ()
right = ()
flag = 0
for file in sorted(os.listdir(img_dir)):
if file.endswith(".jpg"):
img = Image.open(img_dir+"/"+file)
# CROP THE IMAGE
cropped_left = img.crop(area_left)
cropped_right = img.crop(area_right)
# RESIZE THE IMAGES
resized_left = cropped_left.resize((1,1))
resized_right = cropped_right.resize((1,1))
# Keep the initial values from the first image
if flag == 0 :
left_init = resized_left.getpixel((0,0))
right_init = resized_right.getpixel((0,0))
flag = 1
else :
left = resized_left.getpixel((0,0))
right = resized_right.getpixel((0,0))
redL = left[0]-left_init[0]
greenL = left[1]-left_init[1]
blueL = left[2]-left_init[2]
redR = right[0]-right_init[0]
greenR = right[1]-right_init[1]
blueR = right[2]-right_init[2]
print("LEFT: \t", str(greenL-blueL), "\t RIGHT: \t", str(greenR-blueR), file)
I then used R to plot the printed values and I got a plot like this one:
With the right tube is denoted with red while the green is the left one.
As you can see the algorithm can discriminate the two tubes at the very beginning and I don't know if that's true or its some kind of artifact due to the code's logic.
Any ideas or hints are welcomed.
A few thoughts, and some code...
If you are interested in colour changes, JPEG is generally not the best choice of format because it does chroma subsampling, i.e. it reduces the colour accuracy in favour of the brightness accuracy because the human eye is less sensitive to colour variations than brightness variations - see downsampling. So, see if you can save in PNG or PPM format off your camera, that may help.
Also, as you are interested in colour changes, you may be better off in HSL or HSV colourspace because that will vary less as a result of any variations in lighting. So, I would suggest you consider working with Hue which represents what we would call "colour". That has the benefit that you will just get one value, i.e. Hue, rather than three, i.e. Red, Green and Blue.
So, with that said, the code could look like this:
#!/usr/bin/env python3
from PIL import Image
from glob import glob
area_L = (250,340,350+16,340+37)
area_R = (419,340,419+16,340+37)
files = glob("f*.jpg")
files.sort()
for f in files:
print('Processing {}'.format(f))
img = Image.open(f).convert('HSV')
H, S, V = img.split()
cropped_L = H.crop(area_L)
cropped_R = H.crop(area_R)
resized_L = cropped_L.resize((1,1))
resized_R = cropped_R.resize((1,1))
L = resized_L.getpixel((0,0))
R = resized_R.getpixel((0,0))
print('Left Hue: {}, Right Hue: {}'.format(L,R))
And the output is:
Processing f00.jpg
Left Hue: 66, Right Hue: 14
Processing f01.jpg
Left Hue: 58, Right Hue: 21
Processing f02.jpg
Left Hue: 57, Right Hue: 26
Processing f03.jpg
Left Hue: 58, Right Hue: 27
Processing f04.jpg
Left Hue: 59, Right Hue: 27
Processing f05.jpg
Left Hue: 57, Right Hue: 26
Processing f06.jpg
Left Hue: 57, Right Hue: 28
Processing f07.jpg
Left Hue: 60, Right Hue: 25
Processing f08.jpg
Left Hue: 60, Right Hue: 25
Processing f09.jpg
Left Hue: 58, Right Hue: 25
Processing f11.jpg
Left Hue: 59, Right Hue: 25
Processing f12.jpg
Left Hue: 59, Right Hue: 25
Processing f13.jpg
Left Hue: 57, Right Hue: 25
Processing f14.jpg
Left Hue: 60, Right Hue: 24
Processing f15.jpg
Left Hue: 58, Right Hue: 28
Processing f16.jpg
Left Hue: 60, Right Hue: 29
Processing f17.jpg
Left Hue: 60, Right Hue: 32
Processing f18.jpg
Left Hue: 60, Right Hue: 33
Processing f19.jpg
Left Hue: 58, Right Hue: 34