Detecting Color Red is not working

Hi! I’m trying to work with detecting the color red by using OpenCV and masking. However, I seem to run into a problem. My code sometimes works and sometimes doesn’t work.

In some pictures, I get the perfect result.

However, when I try it with this one the detection is very bad even when I try to adjust the image’s gamma to make it lighter. Even working with the RGB values, I tried lowering the mask to 0,10,10 but still it doesn’t detect the red
opencv_2.5_86

Here’s my code:

import cv2

from matplotlib import image

import numpy as np



def adjust_gamma(image, gamma=1.0):

        invGamma = 1.0 / gamma

        table = np.array([((i / 255.0) ** invGamma) * 255

                for i in np.arange(0, 256)]).astype("uint8")

        return cv2.LUT(image, table)

path = r'C:\Users\Admin\Desktop\Researcher\VisualServoing\MovementCalibration\Height2.5Meters\opencv_2.5_86.png' #location of the image

image = cv2.imread(path, 1)


img = image

gamma = 1.0

img = adjust_gamma(img,gamma=gamma)

img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# lower mask (0-10)

lower_red = np.array([0,50,50]) # 0,50,50 original

upper_red = np.array([10,255,255])

mask0 = cv2.inRange(img_hsv, lower_red, upper_red)

# upper mask (170-180)

lower_red = np.array([160,100,100]) # 170,50,50 original

upper_red = np.array([180,255,255])

mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

mask = mask0+mask1

output_img = img.copy()

output_img[mask==0] = 0

output_img[mask!=0] = 255

pixels = np.where(mask==[0]) #Pixels for the Undetected

pixels2 =np.where(mask==[255]) #Pixels for the Detected


print("Undetected Pixels", len(pixels[0]))

print("Pixel Area:", len(pixels2[0]))


cv2.imshow("Orig Image", img)

cv2.imshow("Detected Red",output_img)

cv2.waitKey(0)

i modified (simplified) your code.

by this line img_hsv=cv2.cvtColor(img, cv2.COLOR_RGB2HSV) you will convert from RGB to HSV then you need to find blue color ( simpler to find )

import cv2
import numpy as np

path = r'C:\Users\Admin\Desktop\Researcher\VisualServoing\MovementCalibration\Height2.5Meters\opencv_2.5_86.png' #location of the imagethe image

img = cv2.imread(path)

img_hsv=cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
cv2.imshow("img_hsv", img_hsv)

# define range of blue color in HSV
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(img_hsv, lower_blue, upper_blue)

cv2.imshow("mask", mask)


pixels = np.where(mask==[0]) #Pixels for the Undetected

pixels2 =np.where(mask==[255]) #Pixels for the Detected


print("Undetected Pixels", len(pixels[0]))

print("Pixel Area:", len(pixels2[0]))


cv2.imshow("Orig Image", img)

cv2.waitKey(0)
2 Likes

Dude, this code is awesome. It worked on my marker no matter what the lighting is. Thanks a lot!

you are welcome. maybe i will improve the documentation by mentioning this trick
https://docs.opencv.org/4.x/df/d9d/tutorial_py_colorspaces.html

1 Like