We can define the range of red color in HSV as below. I want to detect that whether a certain pixel is red or not? How can I do that in Python? I spend whole day, but unable to find solution. Please resolve my problem. I'm very new to Python. Code that I'm using is:
img=cv2.imread("img.png")
img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# lower mask (0-10)
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
mask0 = cv2.inRange(img_hsv, lower_red, upper_red)
# upper mask (170-180)
lower_red = np.array([170,50,50])
upper_red = np.array([180,255,255])
mask1 = cv2.inRange(img_hsv, lower_red, upper_red)
image_height,image_width,_=img.shape
for i in range(image_height):
for j in range(image_width):
if img_hsv[i][j][1]>=lower_red and img_hsv[i][j][1]<=upper_red:
print("Found red")
You are almost right. You can merge the masks of lower RED and higher RED together to a single mask.
For this
ColorChecker.png
:My Steps to find the RED: