Я новый кодер, и я работаю над дипломным проектом с библиотекой OpenCV [код Python].
Мне нужно написать код, чтобы обнаружить воздушный шар на постоянном фоне и контролировать его.
Я решил работать с функцией Substruct + HoughCircles, проблема в том, что каждый код работает хорошо [кроме обнаружения круга, который работает не лучше], Когда я объединяю два кода, он работает очень медленно и застрял ALOT, может кто-нибудь подсказать мнепожалуйста, как правильно его вставить, чтобы он не застрял и не улучшил HoughCircles?
import numpy as np
#import RPi.GPIO as GPIO
import time
import cv2
cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()
while True:
ret, frame = cap.read()
if frame is None:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
fgmask = fgbg.apply(gray)
#changes = sum(sum(fgmask>200))
changes = (fgmask>200).sum() #
is_moving = (changes > 10000)
print(changes, is_moving)
items = []
contours, hier = cv2.findContours(fgmask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area = cv2.contourArea(cnt)
if 200 < area:
(x,y,w,h) = cv2.boundingRect(cnt)
cv2.rectangle(fgmask, (x,y),(x+w,y+h),255, 2)
cv2.rectangle(frame, (x,y),(x+w,y+h),(0,255,0), 2)
items.append( (area, x, y, w, h) )
if items:
main_item = max(items)
area, x, y, w, h = main_item
if w > h:
r = w//2
else:
r = h//2
cv2.circle(frame, (x+w//2, y+h//2), r, (0,0,255), 2)
print(x + w // 2, y + h // 2)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,50,
param1=50,param2=30,minRadius=0,maxRadius=0)
#circles = np.uint16(np.around(circles))
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle in the image
# corresponding to the center of the circle
cv2.circle(gray, (x, y), r, (0, 255, 0), 4)
#cv2.rectangle(gray, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# time.sleep(0.5)
print("Column Number: ")
print(x)
print("Row Number: ")
print(y)
print("Radius is: ")
print(r)
cv2.imshow('fgmask', fgmask)
cv2.imshow('frame', frame)
cv2.imshow('gray',gray)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
cv2.destroyAllWindows()
cap.release()
Большое спасибо!