'Opencv find colors in set of pictures

I'm working on a assignment where I have to detect two red spots and two green spots in a set of images. Problem is, the lighting in the images is not every where the same, so this makes masking a pain since the BGR values are in some images a bit off.

I've also tried to gray scale the image but I'm affraid I'm getting to much background noice. to dilate and erode.

What would you sugest I try?

With kind regards, Schweini

Edit: images

picture 1 picture 2



Solution 1:[1]

I figured out LAB color space works fine for both the images

Code:

img = cv2.imread(image_path)
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
thresh, th2 = cv2.threshold( lab[:,:,1], 150, 255, cv2.THRESH_BINARY)
thresh, th3 = cv2.threshold( lab[:,:,1], 110, 255, cv2.THRESH_BINARY_INV)

In the 3rd and 4th line I am applying threshold to the a-channel of the LAB converted image lab[:,:,1]. The a-channel depicts variations between the colors red and green. The b-channel on the other hand depicts variations between the colors blue and yellow. (Refer the axis a and b in the following diagram)

enter image description here

th2 highlights the red dots

th3 highlights the green dots

There is a lot of manual tweaking involved if there are other variations. Hope this gets you on the right track. Also LAB color space is independent of devices and easier to get same colors across different media.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeru Luke