Найти контуры цвета в определенных областях кадров
cap = cv2.VideoCapture('C:/Users/kamil/Desktop/video.asf')
#1. detect red color only, in video.
#2. specify the area of the detection.
while(True):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
a, b = 0, 0
height, width, layers = frame.shape
start_height = height - height
left_x = width - 500
black_min = np.array([0, 0, 0], np.uint8)
black_max = np.array([179, 255, 70], np.uint8)
red_min = np.array([0, 70, 50], np.uint8)
red_max = np.array([20, 255, 255], np.uint8)
red1 = np.array([170, 70, 50], np.uint8)
red2 = np.array([180, 255, 255], np.uint8)
red_mask = cv2.inRange(hsv, red1, red2)
red_mask2 = cv2.inRange(hsv, red_min, red_max)
kernel = np.ones((7, 7), "uint8")
red_mask = cv2.dilate(red_mask, kernel)
res_red = cv2.bitwise_and(frame, frame, mask=red_mask)
red_mask2 = cv2.dilate(red_mask2, kernel)
res_red2 = cv2.bitwise_and(frame, frame, mask=red_mask2)
cnts, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, c in enumerate(cnts):
area1 = frame[a:a+height, b:b+300]
left_x = width - 500
area2 = frame[a:a + height, left_x:left_x + 500]
area = cv2.contourArea(c)
if area in area2:
x,y,w,h = cv2.boundingRect(c)
#image =
cv2.rectangle(area2, (x, y),(x + w, y + h),(0, 0, 255), 2)
x, y, w, h = cv2.boundingRect(c)
cv2.putText(frame, "Red detected", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
cv2.imshow("Video from camera", frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
я стараюсь объединить два кода, в первом я могу выделить нужную часть кадра:
height, width, layers = frame.shape
number_slices = 3
width_slices = width/number_slices
start_width = width - (width_slices * number_slices)
start_height = height - height
print(width_slices)
y,x=0,0
left_x = width-500
#right_x = frame[y:y+height, x:x+300]
crop_right = frame[y:y+height, x:x+300]#for right side
crop_left = frame[y:y+height, left_x:left_x+500]
#for area in frame:
cv2.imshow("image1", crop_left)
cv2.imshow("image2", crop_right)
if cv2.waitKey(0):
cv2.destroyAllWindows()
во втором я определяю нужные объекты:
cnts, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, c in enumerate(cnts):
area = cv2.contourArea(c)
#print(area)
if (700 > area > 350):
x, y, w, h = cv2.boundingRect(c)
image = cv2.rectangle(frame, (x, y),(x + w, y + h), (0, 0, 255), 2)
cv2.putText(frame, "Red detected", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))
cv2.imshow("Video from camera", frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
Хочу объединить и найти объекты в области area1 = frame[a:a+height, b:b+300]. Заранее спасибо за любую помощь!