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
def printBlobs(img, min_color, max_color): blobs = cvblob.Blobs() #Starts the blob class size = cv.GetSize(img) #gets the size of the img hsv = cv.CreateImage( size, cv.IPL_DEPTH_8U, 3) #creates a new image for when the colored image is converted to hsv thresh = cv.CreateImage( size, cv.IPL_DEPTH_8U, 1) #creates a new image that is 1 channeled for the thresholding labelImg = cv.CreateImage( size, cvblob.IPL_DEPTH_LABEL, 1) #creates a image for later use for showing blobs cv.CvtColor(img, hsv, cv.CV_BGR2HSV) #converts the color image to an hsv image cv.InRangeS( hsv, min_color, max_color, thresh ) #finds colors between a min range and a max range with a source img and a destination image #these are for corrections to thresholding to remove as many false positives cv.Smooth(thresh, thresh, cv.CV_BLUR) #smooths out the thresholded image cv.Dilate(thresh, thresh) #Dilates the thresholded image cv.Erode(thresh, thresh) #Erodes the thresholded image result = cvblob.Label( thresh, labelImg, blobs ) #Don't know what this does exactly but it is used later to print the total number of pixels found numblobs = len( blobs.keys() ) #Number of blobs found. blobs is a dictionary with the keys as the number of the blob found and the value as the pointer to the location of the blob avgSize = int(result / numblobs) #Average size of the blobs casted as an int print "average size: " + str(avgSize) arr = [] #empty array to keep track fo what blobs to remove for x in blobs: if ( blobs[x].area < avgSize ): #if the size of the blob is less than half of the mean size, the blob is added to the array arr.append(x) for x in arr: del blobs[x] # the blob is then removed from the dictionary of blobs for x in blobs: print str(blobs[x]) + "," + str( blobs[x].area) #prints the blob number and the area of the blob
hmin = 50 hrange = 50 min_green = cv.Scalar(hmin, 100, 100, 0) max_green = cv.Scalar(hmin + hrange, 256, 256, 0) #cv.Smooth(img, img, cv.CV_GAUSSIAN) #cv.Smooth(img, img, cv.CV_GAUSSIAN) cv.CvtColor(img, hsv, cv.CV_BGR2HSV) cv.InRangeS(hsv, min_green, max_green, green) #cv.InRangeS(img, cv.RGB(200,230,200), cv.RGB(250,256,250),green); ''' Smooth out the HSV_image and dilate/erode''' cv.Smooth(green, green, cv.CV_BLUR) cv.Dilate(green, green) cv.Erode(green, green) result = cvblob.Label(green, labelImg, blobs) numblobs = len(blobs.keys()) if (numblobs > 0): # Average Size of Blobs avgsize = int(result / numblobs) print str(numblobs) + " blobs found covering " + str(result) + "px" filtered = cv.CreateImage(cv.GetSize(img), cv.IPL_DEPTH_8U, 1) cvblob.FilterLabels(labelImg, filtered, blobs) # Largest Blob size bigblob = cvblob.GreaterBlob(blobs) print "largest blob is " + str(bigblob) + " which is " + str( blobs[bigblob].area) + " px" bigblobs = copy.copy(blobs)
blobs = cvblob.Blobs() size = cv.GetSize(img) hsv = cv.CreateImage(size, cv.IPL_DEPTH_8U, 3) h = cv.CreateImage(size, 8, 1) grey = cv.CreateImage(size, 8, 1) yuv = cv.CreateImage(size, 8, 3) red = cv.CreateImage(size, cv.IPL_DEPTH_8U, 1) labelImg = cv.CreateImage(size,cvblob.IPL_DEPTH_LABEL, 1) hsv_min = cv.Scalar(0,10,10,0) hsv_max = cv.Scalar(180,255,255,0) cv.InRangeS(img, cv.RGB(200,0,0), cv.RGB(255,225,225),red); result = cvblob.Label(red,labelImg,blobs) numblobs = len(blobs.keys()) avgsize = int(result/numblobs) print str(numblobs) + " Blobs found covering " + str(result) + "px" filtered = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U,1) cvblob.FilterLabels(labelImg,filtered,blobs) bigblob = cvblob.GreaterBlob(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) centroid = cvblob.Centroid(bigblobs[bigblob])
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