Хотите получить только самый большой Объект каждого вида.
Здесь цель состоит в том, чтобы визуализировать только объекты RGB, по одному для каждого вида. Но это также и выбор объектов. Я нашел лучший способ обнаружения самого большого объекта по самому большому контуру, но здесь также показаны другие подобные предметы, я хочу их игнорировать.
import cv2
import numpy as np
# Webcamera no 0 is used to capture the frames
cap = cv2.VideoCapture(0)
#print(cv2.__version__)
# This drives the program into an infinite loop.
while(1):
# Captures the live stream frame-by-frame
_, frame = cap.read()
blurred_frame=cv2.GaussianBlur(frame,(5,5),100)
# Converts images from BGR to HSV
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)
#print(hsv)
lower_red = np.array([0,50,50])
upper_red = np.array([10,255,255])
lower_green = np.array([65,60,60])
upper_green = np.array([80,255,255])
lower_blue = np.array([100,150,0])
upper_blue = np.array([140,255,255])
# Here we are defining range of bluecolor in HSV
# This creates a mask of blue coloured
# objects found in the frame.
mask = cv2.inRange(hsv, lower_red, upper_red)
mask = cv2.erode(mask, None, iterations=3)
mask = cv2.dilate(mask, None, iterations=5)
mask1 = cv2.inRange(hsv, lower_green, upper_green)
mask1 = cv2.erode(mask1, None, iterations=3)
mask1 = cv2.dilate(mask1, None, iterations=5)
mask2 = cv2.inRange(hsv, lower_blue, upper_blue)
mask2 = cv2.erode(mask2, None, iterations=3)
mask2 = cv2.dilate(mask2, None, iterations=5)
#ret,thresh = cv2.threshold(mask, 0, 255, 0)
contours, hierarchy=cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours1, hierarchy = cv2.findContours(mask1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours2, hierarchy = cv2.findContours(mask2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print(contours)
mask_rg=cv2.bitwise_or(mask,mask1)
mask_res=cv2.bitwise_or(mask2,mask_rg)
res = cv2.bitwise_and(frame,frame, mask= mask_res)
#cv2.drawContours(frame, contours, -1, (0,0,255), 3)
if len(contours)!=0:
contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
for c in contours:
area=cv2.contourArea(c)
if area>50:
cv2.drawContours(res, biggest_contour,-5, (0,0,255), 10)
if len(contours1)!=0:
contour_sizes1 = [(cv2.contourArea(contour), contour) for contour in contours1]
biggest_contour1 = max(contour_sizes1, key=lambda x: x[0])[1]
for c in contours1:
area=cv2.contourArea(c)
if area>50:
cv2.drawContours(res, biggest_contour1,-5, (0,255,0), 10)
if len(contours2)!=0:
contour_sizes2 = [(cv2.contourArea(contour), contour) for contour in contours2]
biggest_contour2 = max(contour_sizes2, key=lambda x: x[0])[1]
for c in contours1:
area=cv2.contourArea(c)
if area>50:
cv2.drawContours(res, biggest_contour2,-5, (255,0,0), 10)
# The bitwise and of the frame and mask is done so
# that only the blue coloured objects are highlighted
# and stored in res
cv2.imshow('frame',frame)
#cv2.imshow('mask',mask)
cv2.imshow('res',res)
# This displays the frame, mask
# and res which we created in 3 separate windows.
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# Destroys all of the HighGUI windows.
cv2.destroyAllWindows()
# release the captured frame
cap.release()
введите описание изображения здесь
Я не хочу, чтобы 2 синих цветных объекта был виден только один, который определяется по контурам.
Пожалуйста, помогите мне.