Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Corner and Blob Detection | Image Processing with OpenCV
Computer Vision Essentials

bookCorner and Blob Detection

Corner Detection

Corner detection is used to identify sharp changes in intensity where two edges meet. It helps in feature matching, object tracking, and structure recognition.

Popular Methods:

  • Harris corner detector (cv2.cornerHarris): detects corners based on gradient changes;
# Load image and convert to grayscale
image = cv2.imread("image.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert to float32
gray = np.float32(gray)

# Apply Harris Corner Detection
harris_corners = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)
harris_corners = cv2.dilate(harris_corners, None)  # Improve visibility

# Mark detected corners in red
image[harris_corners > 0.01 * harris_corners.max()] = [0, 0, 255]
Note
Note

Parameters:

  • src: input grayscale image (must be float32);
  • blockSize: size of the local window for computing gradients;
  • ksize: aperture size of the Sobel operator (odd, e.g., 3, 5, 7);
  • k: harris detector free parameter (typical values: 0.04 - 0.06).
  • Shi-Tomasi corner detector (cv2.goodFeaturesToTrack): selects the strongest corners in an image;
# Load image and convert to grayscale
image = cv2.imread("image.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect corners
corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10)

# Draw detected corners in blue
for corner in corners:
    x, y = np.int32(corner[0])
    cv2.circle(image, (x, y), 5, (255, 0, 0), -1)
Note
Note

Parameters:

  • image: input grayscale image;
  • maxCorners: maximum number of corners to detect;
  • qualityLevel: minimum quality of detected corners (range: 0.01 - 0.1);
  • minDistance: minimum distance between detected corners.
Note
Study More

Parameters of cv2.circle() method for highlighting objects:

  • image: the image on which the circle is to be drawn;
  • center_coordinates: the center coordinates of the circle, represented as a tuple of two values: (X, Y);
  • radius: the radius of the circle;
  • color: the color of the circle's border. In BGR format, passed as a tuple (e.g., (255, 0, 0) for blue);
  • thickness: the thickness of the circle's border in pixels. A value of -1 will fill the entire circle with the specified color.

Blob Detection

Blob detection finds regions of similar intensity in an image, useful for object detection and tracking.

One of the popular methods for blob detection is SimpleBlobDetector

  • cv2.SimpleBlobDetector: detects keypoints representing blobs based on size, shape, and intensity.
# Set up SimpleBlobDetector parameters
params = cv2.SimpleBlobDetector_Params()

# Adjust parameters for better detection
params.minThreshold = 5
params.maxThreshold = 255
params.filterByArea = True
params.minArea = 100
params.maxArea = 5000
params.filterByCircularity = True
params.minCircularity = 0.02
params.filterByConvexity = True
params.minConvexity = 0.5
params.filterByInertia = True
params.minInertiaRatio = 0.001

# Create detector and detect blobs
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(gray)

# Draw blobs on the image
image_with_blobs = cv2.drawKeypoints(image, keypoints, None, (0, 255, 0),
                                     cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
Note
Note

Parameters:

  • minThreshold, maxThreshold: intensity range for blob detection;
  • filterByArea: enables filtering based on blob size;
    • minArea, maxArea: specifies the size constraints for detected blobs;
  • filterByCircularity: enables filtering based on roundness;
    • minCircularity: lower values detect elongated blobs;
  • filterByConvexity: enables filtering based on convexity;
    • minConvexity: lower values allow concave shapes;
  • filterByInertia: enables filtering based on elongation;
    • minInertiaRatio: lower values detect elongated blobs.
Task

Swipe to start coding

You are given the images of factory (factory) and sunflowers (sunflowers):

  • Convert factory image to grayscale and store in gray_factory variable;
  • Convert sunflowers image to grayscale and store in gray_sunflowers variable;
  • It is necessary for Harris Detector to convert image matrix to float32, do it and store in gray_float;
  • Apply Harris corner detection and store in harris_corners (recommended parameters blockSize=2, ksize=3, k=0.04);
  • Use dilate() to improve visibility of harris_corners;
  • Apply Shi-Tomasi corner detection to image and store in shi_tomasi_corners (recommended parameters gray_factory, maxCorners=100, qualityLevel=0.01, minDistance=10)
  • Create SimpleBlobDetector_Params object to initialize the parameters and store in params;
  • Create a blob detector with specified parameters and store in detector;
  • Detect blob keypoints and store in keypoints.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 8
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between Harris and Shi-Tomasi corner detectors?

How do I choose between corner detection and blob detection for my application?

Can you provide more details on tuning the parameters for SimpleBlobDetector?

close

Awesome!

Completion rate improved to 3.45

bookCorner and Blob Detection

Swipe to show menu

Corner Detection

Corner detection is used to identify sharp changes in intensity where two edges meet. It helps in feature matching, object tracking, and structure recognition.

Popular Methods:

  • Harris corner detector (cv2.cornerHarris): detects corners based on gradient changes;
# Load image and convert to grayscale
image = cv2.imread("image.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert to float32
gray = np.float32(gray)

# Apply Harris Corner Detection
harris_corners = cv2.cornerHarris(gray, blockSize=2, ksize=3, k=0.04)
harris_corners = cv2.dilate(harris_corners, None)  # Improve visibility

# Mark detected corners in red
image[harris_corners > 0.01 * harris_corners.max()] = [0, 0, 255]
Note
Note

Parameters:

  • src: input grayscale image (must be float32);
  • blockSize: size of the local window for computing gradients;
  • ksize: aperture size of the Sobel operator (odd, e.g., 3, 5, 7);
  • k: harris detector free parameter (typical values: 0.04 - 0.06).
  • Shi-Tomasi corner detector (cv2.goodFeaturesToTrack): selects the strongest corners in an image;
# Load image and convert to grayscale
image = cv2.imread("image.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect corners
corners = cv2.goodFeaturesToTrack(gray, maxCorners=100, qualityLevel=0.01, minDistance=10)

# Draw detected corners in blue
for corner in corners:
    x, y = np.int32(corner[0])
    cv2.circle(image, (x, y), 5, (255, 0, 0), -1)
Note
Note

Parameters:

  • image: input grayscale image;
  • maxCorners: maximum number of corners to detect;
  • qualityLevel: minimum quality of detected corners (range: 0.01 - 0.1);
  • minDistance: minimum distance between detected corners.
Note
Study More

Parameters of cv2.circle() method for highlighting objects:

  • image: the image on which the circle is to be drawn;
  • center_coordinates: the center coordinates of the circle, represented as a tuple of two values: (X, Y);
  • radius: the radius of the circle;
  • color: the color of the circle's border. In BGR format, passed as a tuple (e.g., (255, 0, 0) for blue);
  • thickness: the thickness of the circle's border in pixels. A value of -1 will fill the entire circle with the specified color.

Blob Detection

Blob detection finds regions of similar intensity in an image, useful for object detection and tracking.

One of the popular methods for blob detection is SimpleBlobDetector

  • cv2.SimpleBlobDetector: detects keypoints representing blobs based on size, shape, and intensity.
# Set up SimpleBlobDetector parameters
params = cv2.SimpleBlobDetector_Params()

# Adjust parameters for better detection
params.minThreshold = 5
params.maxThreshold = 255
params.filterByArea = True
params.minArea = 100
params.maxArea = 5000
params.filterByCircularity = True
params.minCircularity = 0.02
params.filterByConvexity = True
params.minConvexity = 0.5
params.filterByInertia = True
params.minInertiaRatio = 0.001

# Create detector and detect blobs
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(gray)

# Draw blobs on the image
image_with_blobs = cv2.drawKeypoints(image, keypoints, None, (0, 255, 0),
                                     cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
Note
Note

Parameters:

  • minThreshold, maxThreshold: intensity range for blob detection;
  • filterByArea: enables filtering based on blob size;
    • minArea, maxArea: specifies the size constraints for detected blobs;
  • filterByCircularity: enables filtering based on roundness;
    • minCircularity: lower values detect elongated blobs;
  • filterByConvexity: enables filtering based on convexity;
    • minConvexity: lower values allow concave shapes;
  • filterByInertia: enables filtering based on elongation;
    • minInertiaRatio: lower values detect elongated blobs.
Task

Swipe to start coding

You are given the images of factory (factory) and sunflowers (sunflowers):

  • Convert factory image to grayscale and store in gray_factory variable;
  • Convert sunflowers image to grayscale and store in gray_sunflowers variable;
  • It is necessary for Harris Detector to convert image matrix to float32, do it and store in gray_float;
  • Apply Harris corner detection and store in harris_corners (recommended parameters blockSize=2, ksize=3, k=0.04);
  • Use dilate() to improve visibility of harris_corners;
  • Apply Shi-Tomasi corner detection to image and store in shi_tomasi_corners (recommended parameters gray_factory, maxCorners=100, qualityLevel=0.01, minDistance=10)
  • Create SimpleBlobDetector_Params object to initialize the parameters and store in params;
  • Create a blob detector with specified parameters and store in detector;
  • Detect blob keypoints and store in keypoints.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 8
single

single

some-alt