Thank you for your help.I'd like to extract red and green colors from the image and enclose the color dot group in a rectangle, but I can't try any HSV pattern.Blue is working well now, but it's partly off, and it's also hanging in unrelated places.I'm not sure if it's a bad way to find a color like a program or if I should change the scope.Please tell me the solution.Thank you for your cooperation.
col_img=cv2.imread("/home/pi/seisaku/2.jpg", cv2.IMREAD_COLOR)
col_img=cv2.resize(col_img, None, fx=3, fy=3)
img=cv2.cvtColor(col_img, cv2.cv.CV_BGR2HSV)
h=copy.copy(img[:,:,0])
s=copy.copy(img[:,:,1])
v=copy.copy(img[:,:,2])
Blue=np.zeros (col_img.shape, dtype=np.uint8)
Red=np.zeros (col_img.shape, dtype=np.uint8)
Green=np.zeros (col_img.shape, dtype=np.uint8)
Blue [((h>200)|(h<260))&(s>100)] = 255
Red [((h<30)|(h>280))&(s>128)] = 255
Green [((h>80)|(h<230))&(s>170)] = 255
for colin [Red, Blue, Green]:
gray=cv2.cvtColor(col,cv2.COLOR_BGR2GRAY)
retval, bw = cv2.threshold (gray, 50, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("bw.jpg",bw)
contents,_=cv2.findContours(bw,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
## treatment of each contour
for i in range(0,len(contours)):
# Compute contour areas
area=cv2.contourArea(contours[i])
# circumscribed rectangle
if(len(contours[i])>0)&(100<area<400):
rect=contours[i]
x,y,w,h = cv2.boundingRect(rect)
ifabs(w-h)<5:
cv2.rectangle(col_img,(x,y),(x+w,y+h),(255,0,0),2)
# print(int(x+w/2), int(y+h/2))
cv2.imwrite("aux.jpg", col_img)
# cv2.waitKey(0)
Blue [((h>200)|(h<260))&(s>100)] = 255
Red [((h<30)|(h>280))&(s>128)] = 255
Green [((h>80)|(h<230))&(s>170)] = 255
Do you mean that you want to determine the hue (h
) by the angle [0,360)?After OpenCV HSV conversion, the value range is 0 to 179, so this formula does not work as you expected.
Question We have improved the marker criteria based on the source code, assuming Python 3 + OpenCV3.
#!/usr/bin/env python3
import cv2
import numpy as np
import path
col_img = cv2.imread("q37984.jpg", cv2.IMREAD_COLOR)
col_img=cv2.resize(col_img, None, fx=3, fy=3)
# Decomposition by HSV channel
img=cv2.cvtColor(col_img,cv2.COLOR_BGR2HSV)
h = img [:, :, 0]
s=img[:,:,1]
v=img[:,:,2]
shape=(img.shape[0], img.shape[1], 1)
Red=np.zeros (shape, dtype=np.uint8)
Green=np.zeros (shape, dtype=np.uint8)
Blue=np.zeros (shape, dtype=np.uint8)
# Hue (Hue) detection of color components; + threshold determination of saturation (S) and brightness (V)
HF = 0.5
Red [((h<0*HF)|(h>300*HF))&(s>16)&(v>128)] = 255#330±30°
Green [((h>30*HF)&(h<90*HF))&(s>16)&(v>128)] = 255#60±30°
Blue [((h>180*HF)&(h<240*HF))&(s>16)&(v>128)] = 255#210±30°
# Reduced noise removal (5x5 circular kernel)
kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
Red=cv2.morphonyEx (Red, cv2.MORPH_OPEN, kernel)
Green=cv2.morphonyEx (Green, cv2.MORPH_OPEN, kernel)
Blue=cv2.morphologyEx (Blue, cv2.MORPH_OPEN, kernel)
Round_TH = 0.4# circle decision threshold
for target in [Red, Green, Blue]:
_,contours,_=cv2.findContours(target,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
for cnt in contents:
# Calculate the circumscribed circular area area 1 of the detected contour
(cx, cy), radius=cv2.minEnclosureCircle(cnt)
area1=(radius*radius)*math.pi
# Calculate the real area area 0 of the detected contour
area0 = cv2.contourArea(cnt)
# Estimate the "circularity" of the contour from the area ratio
if(1-ROUND_TH)<area1/area0<(1+ROUND_TH):
cv2.circle(col_img, (int(cx), int(cy))), int(radius), (0,0,0), 2)
cv2.imwrite("result.jpg", col_img)
© 2024 OneMinuteCode. All rights reserved.