So schließen Sie eine Lücke in der Kontur in OpenCVPython

Python-Programme
Anonymous
 So schließen Sie eine Lücke in der Kontur in OpenCV

Post by Anonymous »

Ich habe ein Problem, mit dem ich zu kämpfen habe, und finde scheinbar keine Lösung.
Ich möchte den Bereich der Konturen ermitteln, die ich in diesem Bild-/Videorahmen sehe:
Image

Das Problem ist, dass die Kontur rechts abgeschnitten ist, weshalb sie nicht als erkannt wird eine geschlossene Kontur und cv2.contourArea() gibt keinen Wert zurück/erkennt dies nicht.
Hat jemand eine Lösung, um diesen Bereich trotzdem zu erkennen? :)

Code: Select all

while True:
ret, frame = cap.read()

if not ret:
print("couldnt read video or video has ended!")
break

frame_prob = frame[ystart:yend, xstart:xend]
frame_all_contours = frame_prob.copy()
gray = cv2.cvtColor(frame_prob, cv2.COLOR_BGR2GRAY)
frame_norm = cv2.equalizeHist(gray)

_, mask = cv2.threshold(frame_norm, thresh=10, maxval=255, type= cv2.THRESH_BINARY_INV)
mask = cv2.dilate(mask, kernel, iterations = 2)
edge = cv2.Canny(mask, 90, 250, apertureSize=3)
contours, _ = cv2.findContours(edge, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(frame_all_contours, contours, -1, (0,255,0), 3)

min_area_threshold = 200

filtered_contours = []
for i in contours:
if cv2.contourArea(i) > min_area_threshold:
filtered_contours.append(i)

cv2.drawContours(frame_prob, filtered_contours, -1, (0,255,0), 3)
if filtered_contours:
max_contour = max(filtered_contours, key=cv2.contourArea)
max_areas.append(cv2.contourArea(max_contour))
cv2.drawContours(frame_prob, [max_contour], -1, (0,0,255), 3)
else:
max_areas.append(0)

opt_flow = cv2.calcOpticalFlowFarneback(mask_first, mask, None, 0.5, 3, 30, 3, 5, 1.2, 0)
mask_first = mask

frame_count += 1

cv2.imshow("All contours", frame_all_contours)
cv2.imshow("thresholded contours", frame_prob)
cv2.imshow("Canny mask to detect contours from", edge)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post