import numpy as np
import cv2
img=cv2.imread('image.jpg')
hsvFrame=cv2.cvtColor(img ,cv2.COLOR_BGR2HSV)
#SET RANGE FOR RED
#DEFINE MASk
red_lower=np.array([0,0,204],np.uint8)
red_upper=np.array([0,0,255],np.uint8)
red_mask=cv2.inRange(hsvFrame,red_lower,red_upper)
kernel=np.ones((5,5),"uint8")
red_mask=cv2.dilate(red_mask,kernel)
res_red=cv2.bitwise_and(img,img,mask=red_mask)
#creating contour
contours,hierarchy=cv2.findContours(red_mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for pic,contour in enumerate(contours):
area=cv2.contourArea(contour)
if area>300:
x,y,w,h=cv2.boundingRect(contour)
imageFrame=cv2.rectangle(img,(x,y),(x w,y h),(0,0,255),2)
cv2.putText(img,"red colour",(x,y),
cv2.FONT_HERSHEY_SIMPLEX,1.0,(0,0,255))
cv2.imshow("detected red ",img)
cv2.waitKey(0)
tried to detect red color in a given image but the program detects other colors. converted this program from a program that detects red color from the webcam feed and it works fine, but the red detection in images doesn't work
CodePudding user response:
Why are you converting your image to HSV? There appears to be a mismatch between the color range you are trying to match (which appears to be specified in BGR color space) vs the color space your image is in (HSV). I suspect that is the source of your issue.