Skip to content

Commit

Permalink
find largest blob in an image
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Austin committed Feb 19, 2012
1 parent 8da86f0 commit 5b27c73
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions opencv-tutorial/find-contours.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/python

#
# Finding image contours after thresholding
# Find the largest colored blob in an image
#
# load an image, convert to HSV color space, threshold the image
# for yellow hue values, then find contours
# for yellow hue values, find contours, then iterate over them to
# find the largest contour
#

import cv
Expand Down Expand Up @@ -32,12 +33,21 @@
cv.Erode(image_threshed, image_threshed, None, 10)
cv.Canny(image_threshed, image_threshed, 10, 50, 3)

contours = cv.FindContours(cv.CloneImage(image_threshed), cv.CreateMemStorage(), cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
for c in contours:
print c
current_contour = cv.FindContours(cv.CloneImage(image_threshed), cv.CreateMemStorage(), cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE)
largest_contour = current_contour
while True:
current_contour = current_contour.h_next()
if (not current_contour):
break
if (cv.ContourArea(current_contour) > cv.ContourArea(largest_contour)):
largest_contour = current_contour

for c in largest_contour:
cv.Circle(image, c, 1, (0,255,0), 1)

moments = cv.Moments(contours, 1)
#cv.DrawContours(image, contours, (0,255,0), (255,0,0), 1)

moments = cv.Moments(largest_contour, 1)
center = (cv.GetSpatialMoment(moments, 1, 0)/cv.GetSpatialMoment(moments, 0, 0),cv.GetSpatialMoment(moments, 0, 1)/cv.GetSpatialMoment(moments, 0, 0))
cv.Circle(image, (int(center[0]), int(center[1])), 2, (0,0,255), 2)

Expand Down

0 comments on commit 5b27c73

Please sign in to comment.