Home Line & Circle & Blob Detection
Post
Cancel

Line & Circle & Blob Detection

Line detection

Line detection is performed by two methods

  • Hough Lines: cv2.HoughLines(image, rho, theta, threshold) where:
    • image : binarized/thresholded image
    • rho : Distance resolution of the accumulator in pixels.
    • theta : Angle resolution of the accumulator in radians.
    • threshold : the minimum vote for it to be considered a line.
  • Probabilistic Hough Lines : cv2.HoughLinesP(image, rho, theta, threshold, minLineLength, maxLineGap) where:
    • image : Ninarized/thresholded image
    • rho : Distance resolution of the accumulator in pixels.
    • theta : Angle resolution of the accumulator in radians.
    • threshold : The minimum vote for it to be considered a line.
    • minLineLength : The minimum pixels of line. default = 0.
    • maxLineGap : The gap of pixels between lines. default = 0.



Result








Circle detection

Line detection is performed by:
cv2.HoughCircles(image, method, dp, minDist, param1, param2, minRadius, maxRadius) where:

  • image : Binarized/thresholded image.
  • method : Detection method.
  • dp : Inverse ratio of accumulator resolution.
  • minDist : The minimum distance between the center of detected circles.
  • param1 : Gradient value used in the edge detection. default = 100.
  • param2 : Accumulator threshold for the “method” (lower allows more circles to be detected (false positives)). default = 100.
  • minRadius : Limits the smallest circle to this size (via radius). default = 0.
  • maxRadius : Limits the biggest circle to this size (via radius). default = 0.



Method of HoughCircles



  • cv2.HOUGH_GRADIENT
  • cv2.HOUGH_GRADIENT_ALT



Result







Blob Detection

Blob detection is performed by:
cv2.drawKeypoints(image, keypoints, outImage, color, flags) where:

  • image : The image detected by cv2.SimpleBlobDetector.create()
  • keypoints : Keypoints from the source image.
  • outImage : Output image. Its content depends on the flags value defining what is drawn in the output image.
  • color : Color of keypoints. default = Scalar::all(-1)
  • flags : Flags setting drawing features. Possible flags bit values are defined by DrawMatchesFlags. default = DRAW_MATCHES_FLAGS_DEFAULT



Flags of Blob detections



  • DRAW_MATCHES_FLAGS_DEFAULT
  • DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
  • DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG
  • DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS





Result







Counting Blobs

We can count blobs by:
cv2.SimpleBlobDetector.Params()
We can set the filtering parameters of simpleBlobDetector. Let’s see what the parameters are in it.



We can set those parameters by:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
params = cv2.SimpleBlobDetector.Params()

# Set Threshold filtering parameters
params.minThreshold = 10
params.maxThreshold = 200

# Set Area filtering parameters
params.filterByArea = True
params.minArea = 100

# Set Circularity filtering parameters
params.filterByCircularity = True 
params.minCircularity = 0.9

# Set Convexity filtering parameters
params.filterByConvexity = False
params.minConvexity = 0.2
    
# Set inertia filtering parameters
params.filterByInertia = True
params.minInertiaRatio = 0.01

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector.create(params)
    
# Detect blobs
keypoints = detector.detect(image)





Result







Implementation

This post is licensed under CC BY 4.0 by the author.