我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用cv2.SimpleBlobDetector_create()。
def _extract_spots(self) -> None: # Dilate and Erode to 'clean' the spot (nb that this harms the number itself, so we only do it to extract spots) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) img = cv2.dilate(self._img, kernel, iterations=1) img = cv2.erode(img, kernel, iterations=2) img = cv2.dilate(img, kernel, iterations=1) # Perform a simple blob detect params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea = 20 # The dot in 20pt font has area of about 30 params.filterByCircularity = True params.minCircularity = 0.7 params.filterByConvexity = True params.minConvexity = 0.8 params.filterByInertia = True params.minInertiaRatio = 0.4 detector = cv2.SimpleBlobDetector_create(params) self.spot_keypoints = detector.detect(img) # Log intermediate image img_with_keypoints = cv2.drawKeypoints(img, self.spot_keypoints, outImage=np.array([]), color=(0, 0, 255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) self.intermediate_images.append(NamedImage(img_with_keypoints, 'Spot Detection Image'))
def create_blob_detector(roi_size=(128, 128), blob_min_area=3, blob_min_int=.5, blob_max_int=.95, blob_th_step=10): params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea = blob_min_area params.maxArea = roi_size[0]*roi_size[1] params.filterByCircularity = False params.filterByColor = False params.filterByConvexity = False params.filterByInertia = False # blob detection only works with "uint8" images. params.minThreshold = int(blob_min_int*255) params.maxThreshold = int(blob_max_int*255) params.thresholdStep = blob_th_step ver = (cv2.__version__).split('.') if int(ver[0]) < 3: return cv2.SimpleBlobDetector(params) else: return cv2.SimpleBlobDetector_create(params)
def get_detector(detector_name, params): if detector_name == 'harris': detector = harris.HarrisDetector( params['quality_level'], params['block_size'], params['aperture_size'], params['alpha'], params['feature_size']) elif detector_name == 'blob': blob_params = cv2.SimpleBlobDetector_Params() blob_params.filterByArea = params['filter_by_area'] blob_params.minArea = params['min_area'] blob_params.maxArea = params['max_area'] blob_params.minThreshold = params['min_threshold'] blob_params.maxThreshold = params['max_threshold'] blob_params.filterByColor = params['filter_by_color']; blob_params.blobColor = params['blob_color']; blob_params.filterByCircularity = params['filter_by_circularity'] blob_params.minCircularity = params['min_circularity'] blob_params.filterByConvexity = params['filter_by_convexity'] blob_params.minConvexity = params['min_convexity'] blob_params.filterByInertia = params['filter_by_inertia'] blob_params.minInertiaRatio = params['min_inertia_ratio'] detector = cv2.SimpleBlobDetector_create(blob_params) elif detector_name == 'dog': detector = cv2.xfeatures2d.SIFT_create( params['n_features'], params['n_octave_layers'], params['contrast_threshold'], params['edge_threshold'], params['sigma']) return detector
def __find_blobs(input, min_area, circularity, dark_blobs): """Detects groups of pixels in an image. Args: input: A numpy.ndarray. min_area: The minimum blob size to be found. circularity: The min and max circularity as a list of two numbers. dark_blobs: A boolean. If true looks for black. Otherwise it looks for white. Returns: A list of KeyPoint. """ params = cv2.SimpleBlobDetector_Params() params.filterByColor = 1 params.blobColor = (0 if dark_blobs else 255) params.minThreshold = 10 params.maxThreshold = 220 params.filterByArea = True params.minArea = min_area params.filterByCircularity = True params.minCircularity = circularity[0] params.maxCircularity = circularity[1] params.filterByConvexity = False params.filterByInertia = False detector = cv2.SimpleBlobDetector_create(params) return detector.detect(input)
def add_blobs(crop_frame): frame=cv2.GaussianBlur(crop_frame, (3, 3), 0) # Convert BGR to HSV hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # define range of green color in HSV lower_green = np.array([70,50,50]) upper_green = np.array([85,255,255]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv, lower_green, upper_green) mask = cv2.erode(mask, None, iterations=1) mask = cv2.dilate(mask, None, iterations=1) # Bitwise-AND mask and original image res = cv2.bitwise_and(frame,frame, mask= mask) detector = cv2.SimpleBlobDetector_create(params) # Detect blobs. reversemask=255-mask keypoints = detector.detect(reversemask) if keypoints: print "found blobs" if len(keypoints) > 4: keypoints.sort(key=(lambda s: s.size)) keypoints=keypoints[0:3] # Draw detected blobs as red circles. # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob im_with_keypoints = cv2.drawKeypoints(frame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) else: print "no blobs" im_with_keypoints=crop_frame return im_with_keypoints #, max_blob_dist, blob_center, keypoint_in_orders
def execute_BlobDetector(proxy,obj): try: img=obj.sourceObject.Proxy.img.copy() except: img=cv2.imread(__dir__+'/icons/freek.png') im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) im=255-im im2 = img params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea = obj.Area params.filterByConvexity = True params.minConvexity = obj.Convexity/200 # Set up the detector with default parameters. detector = cv2.SimpleBlobDetector_create(params) # Detect blobs. keypoints = detector.detect(im) # Draw detected blobs as red circles. # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob if not obj.showBlobs: im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) obj.Proxy.img = im_with_keypoints for k in keypoints: (x,y)=k.pt x=int(round(x)) y=int(round(y)) # cv2.circle(im,(x,y),4,0,5) cv2.circle(im,(x,y),4,255,5) cv2.circle(im,(x,y),4,0,5) im[y,x]=255 im[y,x]=0 obj.Proxy.img = cv2.cvtColor(im, cv2.COLOR_GRAY2BGR) else: for k in keypoints: (x,y)=k.pt x=int(round(x)) y=int(round(y)) cv2.circle(im2,(x,y),4,(255,0,0),5) cv2.circle(im2,(x,y),4,(0,0,0),5) im2[y,x]=(255,0,0) im2[y,x]=(0,0,0) obj.Proxy.img = im2
def mask_to_objects(mask, threshold): """ applies a blob detection algorithm to the image Args: mask: image mask scaled between 0 and 255 threshold: min pixel intensity of interest Returns: list of objects [(x,y)] """ params = cv2.SimpleBlobDetector_Params() params.minThreshold = threshold params.maxThreshold = 255 params.filterByArea = True params.minArea = 150 params.maxArea = 10000 params.filterByCircularity = False params.filterByInertia = False params.filterByConvexity = False params.filterByColor = False params.blobColor = 255 # Create a detector with the parameters ver = (cv2.__version__).split('.') if int(ver[0]) < 3: detector = cv2.SimpleBlobDetector(params) else: detector = cv2.SimpleBlobDetector_create(params) keypoints = detector.detect(mask) objects = [] for k in keypoints: objects.append(Rect(int(k.pt[0] - k.size), int(k.pt[1] - k.size), int(k.size * 2), int(k.size * 2))) return objects # ============================================================= #