Skip to content

Commit 2de689d

Browse files
Joshua ViszlaiJoshua Viszlai
authored andcommitted
added finding largest area contour
1 parent 6bc09a6 commit 2de689d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

findRed.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@
2323
mask = cv2.dilate(mask, None, iterations=2)
2424
#Sort out all the different shapes that are in the range, and color them red
2525
mask, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
26-
#Display all contours
27-
cv2.drawContours(mask, contours, -1, (180,200,200), 3)
26+
#find biggest area
27+
biggestArea = 0
28+
biggestContour = None
29+
for c in contours:
30+
if cv2.contourArea(c) > biggestArea:
31+
biggestArea = cv2.contourArea(c)
32+
biggestContour = c
33+
#Removes all other contours and keeps biggest contour
34+
for c in contours:
35+
if c is not biggestContour:
36+
cv2.drawContours(mask, [c], -1, 0, -1)
37+
38+
cv2.drawContours(mask, [biggestContour], 0, (180,200,200), 3)
2839
#Display before and after frames side by side
2940
cv2.imshow('before', frame)
3041
cv2.imshow('after', mask)

0 commit comments

Comments
 (0)