bfinder.background_init = 60 bfinder.threshold = 10 bfinder.gaussian_blur = 1 bfinder.skip_filter_contours = False # Parameter for croping xmargin = [0, 0] ymargin = [0, 0] # Build up an expected histogram ninitframes = 50 bin_edges = np.arange(1, 255) ref_cumsum = np.zeros(bin_edges.shape[0] - 1) for _ in range(bfinder.background_init): # grab the current frame and initialize the occupied/unoccupied frame = btgen.randomblob(background, color=blob_brightness[0])[0] bfinder.run(frame) for _ in range(ninitframes): # grab the current frame and initialize the occupied/unoccupied frame = btgen.randomblob(background, color=blob_brightness[0])[0] bfinder.run(frame) # image = bfinder.original_image image = image.astype(np.float) image[bfinder.processed_image == 0] = np.nan croped_images = crop(image, bfinder.filtered_contours, xmargin, ymargin) hist = btfilt.image2hist(croped_images[0], bin_edges) cumsum = btfilt.hist2cumsum(hist) ref_cumsum += cumsum / ninitframes frame_i = 0
if not grabbed: break # Calculate normed difference frame = frame[..., 0] diff = frame - background diff = np.abs(diff) diff -= diff.min() diff /= diff.max() diff *= 255 diff = 255 - diff.astype(np.uint8) # for display image = bfimshow.bw2color(diff.copy()) #Find contours bmarker.run(diff) # Assign center of mass ellipses ellipses = list() for con in bmarker.contours: moments = cv2.moments(con) if moments['m00'] <= 0: continue cx = moments['m10'] / moments['m00'] cy = moments['m01'] / moments['m00'] ellipse = Ellipse(x=cx, y=cy, height=5, width=5) ellipses.append(ellipse) btio.append(file, frame_i, ellipses) # display images when asked if (time.time() - t_start) > refresh_time:
while True: frame_i += 1 # grab the current frame and initialize the occupied/unoccupied (grabbed, frame) = camera.read() # if the frame could not be grabbed, then we have reached the end # of the video if not grabbed: break # Apply background frame=frame[...,0] diff = background.apply(frame.copy(), learningRate=0)#frame-background #Find largest contours bbee.run(diff) area = 0 cont = [] for con in bbee.contours: moments = cv2.moments(con) if moments['m00'] <=0: continue if moments['m00']>area: area = moments['m00'] cont = con cx_bee = moments['m10']/moments['m00'] cy_bee = moments['m01']/moments['m00'] if len(cont) < 5: continue ell = Ellipse()
class TestBlobFinder(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestBlobFinder, self).__init__(*args, **kwargs) self.run_ntimes_oneblob() def run_ntimes_oneblob(self): """ Run the BlobFinder on one blob """ self.background = background() self.mask = mask(self.background) blob = randomblob(self.background)[0] self.bfinder = BlobFinder(self.mask) # init background for _ in range(10): blob = randomblob(self.background)[0] self.bfinder.run(blob) self.blobs_center = list() self.blobs_found = list() for frame_i in range(10): blob, blob_center, _, _ = \ randomblob(self.background) self.bfinder.run(blob) self.blobs_center.append(blob_center) self.blobs_found.append(self.bfinder.filtered_contours) self.blob = blob def test_setget_image(self): myim = self.bfinder.original_image self.assertTrue(np.allclose(myim, self.blob)) def test_masked(self): myim = self.bfinder.masked_image testimage = cv2.bitwise_and(self.blob, self.blob, mask=self.mask) self.assertTrue(np.allclose(myim, testimage)) def test_segmented(self): myim = self.bfinder.segmented_image self.assertTrue(np.allclose(np.unique(myim), [0, 255])) def test_oneblob_numbers(self): condition = True for frame_i in range(len(self.blobs_center)): contours = self.blobs_found[frame_i] if len(contours) > 1: print('Too many blobs detected') condition = False continue elif len(contours) < 1: print('Too few blobs detected') condition = False continue self.assertTrue(condition) def test_oneblob_positions(self): condition = True for frame_i in range(len(self.blobs_center)): contours = self.blobs_found[frame_i] contours = contours[0] px_error = np.sqrt( np.sum(contours.center - self.blobs_center[frame_i])**2) if px_error > 1: print('Pixel error too large : {}'.format(px_error)) condition = False self.assertTrue(condition)