我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用cv2.SimpleBlobDetector()。
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 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 # ============================================================= #
def find_blobs(img): # Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params() # Change thresholds params.minThreshold = 100; params.maxThreshold = 5000; # Filter by Area. params.filterByArea = True params.minArea = 200 # Filter by Circularity params.filterByCircularity = False params.minCircularity = 0.785 # Filter by Convexity params.filterByConvexity = False params.minConvexity = 0.87 # Filter by Inertia #params.filterByInertia = True #params.minInertiaRatio = 0.01 # Set up the detector with default parameters. detector = cv2.SimpleBlobDetector(params) # Detect blobs. keypoints = detector.detect(img) print keypoints # 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(img, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.imwrite("blobs.jpg", im_with_keypoints);
def blobdetect(img): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) yellowMin = (20,100,100) yellowMax = (30, 255, 255) blueMin=(50,100,100) blueMax=(100,255,255) brownMin=(0,100,0) brownMax=(20,255,255) yellow=cv2.inRange(hsv,yellowMin, yellowMax) blue=cv2.inRange(hsv,blueMin,blueMax) brown=cv2.inRange(hsv,brownMin,brownMax) params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea=150 detector=cv2.SimpleBlobDetector(params) keypoints=detector.detect(255-yellow) food=[] for i in keypoints: x=i.pt[0]; y=i.pt[1] food.append([x,y]) params.maxArea=250 detector=cv2.SimpleBlobDetector(params) keypoints=detector.detect(255-yellow) wood=[] for i in keypoints: x=i.pt[0]; y=i.pt[1] wood.append([x,y]) params=cv2.SimpleBlobDetector() keypoints=params.detect(255-blue) rivers=[] for i in keypoints: x=i.pt[0]; y=i.pt[1] rivers.append([x,y]) keypoints=params.detect(255-brown) centre=[] for i in keypoints: x=i.pt[0]; y=i.pt[1] centre.append([x,y]) return [food, wood, rivers, centre]