def TrackFile(anim): tracks = cvb.Tracks() capture = cv.CreateFileCapture(anim) cv.GrabFrame(capture) img = cv.RetrieveFrame(capture) frame = cv.CreateImage(cv.GetSize(img), img.depth, img.nChannels) cnt = 1 while cv.GrabFrame(capture): # Capture Frames img = cv.RetrieveFrame(capture) cv.ResetImageROI(frame) cv.ConvertScale(img, frame, 1, 0) cv.Threshold(frame, frame, 100, 200, cv.CV_THRESH_BINARY) #rct=cv.Rectangle(0, 25, 383, 287) cv.SetImageROI(frame, (0, 25, 383, 287)) chB = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.Split(frame, chB, None, None, None) labelImg = cv.CreateImage(cv.GetSize(frame), cvb.IPL_DEPTH_LABEL, 1) # Get Blobs and try Update Tracks blobs = cvb.Blobs() result = cvb.Label(chB, labelImg, blobs) cvb.FilterByArea(blobs, 500, 1000) # Trys are implemented here just to ensure crashes don't happen when blobs are not present try: print type(blobs.items()[0][1]) except: pass cvb.UpdateTracks(blobs, tracks, 5., 10, 0) try: print type(blobs.items()[0][1]) except: pass try: print type(tracks.items()[0][1]) except: pass imgOut = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 3) cv.Zero(imgOut) cvb.RenderBlobs( labelImg, blobs, frame, imgOut, cvb.CV_BLOB_RENDER_COLOR | cvb.CV_BLOB_RENDER_CENTROID | cvb.CV_BLOB_RENDER_BOUNDING_BOX) # Save images to see what's blobs are getting out. cnt = cnt + 1 print cnt # cv.SaveImage('blobs' + str(cnt) + '.png', imgOut) return tracks, blobs
print "largest blob is " + str(bigblob) + " which is " + str( blobs[bigblob].area) + " px" bigblobs = copy.copy(blobs) cvblob.FilterByLabel(bigblobs, bigblob) #print str(len(bigblobs.keys())) + " blobs with label " + str(bigblob) # find centroid of largest blob centroid = cvblob.Centroid(bigblobs[bigblob]) print "centroid of blob " + str(bigblob) + " is " + str(centroid) imgOut = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(imgOut) cvblob.RenderBlobs( labelImg, blobs, img, imgOut, cvblob.CV_BLOB_RENDER_COLOR | cvblob.CV_BLOB_RENDER_CENTROID | cvblob.CV_BLOB_RENDER_BOUNDING_BOX | cvblob.CV_BLOB_RENDER_ANGLE, 1.0) print "mean color for blob " + str(bigblob) + " is " + str( cvblob.BlobMeanColor(blobs[bigblob], labelImg, img)) #cv.ShowImage("test_filtered", filtered) cv.ShowImage("Original", img) cv.ShowImage("test_rendered", imgOut) k = cv.WaitKey() while k != 27: k = cv.WaitKey(33) else: print "Zero blobs found..."
def findBlobs(img, min_color, max_color, window, renderedwindow, location, area): blobs = cvblob.Blobs() #initializes the blobs class size = cv.GetSize(img) #gets size of image hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) #New HSV image for alter thresh = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) #New Gray Image for later labelImg = cv.CreateImage(size, cvblob.IPL_DEPTH_LABEL, 1) #New Blob image for later cv.CvtColor(img, hsv, cv.CV_BGR2HSV) #converts image to hsv image cv.InRangeS(hsv, min_color, max_color, thresh) #thresholds it #Corrections to remove false positives cv.Smooth(thresh, thresh, cv.CV_BLUR) cv.Dilate(thresh, thresh) cv.Erode(thresh, thresh) result = cvblob.Label(thresh, labelImg, blobs) #extracts blobs from a greyscale image numblobs = len( blobs.keys()) #number of blobs based off of length of blobs dictionary #if there are blobs found if (numblobs > 0): avgsize = int(result / numblobs) print str(numblobs) + " blobs found covering " + str(result) + "px" #Removes blobs that are smaller than a certain size based off of average size remv = [] for x in blobs: if (blobs[x].area < avgsize / 3): remv.append(x) for x in remv: del blobs[x] numblobs = len( blobs.keys()) #gets the number of blobs again after removing some print str(numblobs) + " blobs remaining" filtered = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1) cvblob.FilterLabels( labelImg, filtered, blobs ) #Creates a binary image with the blobs formed (imgIn, imgOut, blobs) #Centroid, area, and circle for all blobs for blob in blobs: location.append(cvblob.Centroid(blobs[blob])) area.append(blobs[blob].area) cv.Circle(img, (int(cvblob.Centroid( blobs[blob])[0]), int(cvblob.Centroid(blobs[blob])[1])), int(math.sqrt(int(blobs[blob].area) / 3.14)) + 25, cv.Scalar(0, 0, 0)) imgOut = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 3) cv.Zero(imgOut) cvblob.RenderBlobs( labelImg, blobs, img, imgOut, cvblob.CV_BLOB_RENDER_COLOR | cvblob.CV_BLOB_RENDER_CENTROID | cvblob.CV_BLOB_RENDER_BOUNDING_BOX | cvblob.CV_BLOB_RENDER_ANGLE, 1.0) #Marks up the blobs image to put bounding boxes, etc on it cv.ShowImage("Window", img) #shows the orininalimage cv.ShowImage("Rendered", imgOut) #shows the blobs image return blobs #returns the list of blobs else: print " ...Zero blobs found. \nRedifine color range for better results" #if no blobs were found print an error message cv.ShowImage("Window", img) #show the original image