OpenCV Tutorial

Image Processing

Feature Detection and Description

Drawing Functions

Video Processing

Applications and Projects

Detect corner of an image in OpenCV

Detecting corners in an image is an essential step in many computer vision applications, including image registration, object detection, and feature extraction. OpenCV provides various methods to detect corners, with the Harris Corner Detection and Shi-Tomasi (Good Features to Track) methods being the most popular. In this tutorial, we'll explore the Harris Corner Detection method:

Prerequisites:

  • Install necessary libraries:
pip install opencv-python

Step-by-Step Tutorial:

  • Import necessary libraries:
import numpy as np
import cv2
  • Read the Image and Convert to Grayscale:
image = cv2.imread('path_to_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  • Apply Harris Corner Detection:
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

Here:

  • The second argument (2) is the block size. It decides the size of the neighborhood considered for corner detection.
  • The third argument (3) is the aperture parameter for the Sobel operator.
  • The last argument (0.04) is the free parameter in the Harris detection equation.
  • Dilate to Mark the Corners:

This step is optional, but it helps in marking the corners.

dst = cv2.dilate(dst, None)
  • Threshold to identify strong corners:

This step will mark the corners in the original image.

image[dst > 0.01 * dst.max()] = [0, 0, 255]  # Marking the corners in red color
  • Display the Result:
cv2.imshow('Harris Corners', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Notes:

  • The parameters in the cv2.cornerHarris() function can be adjusted based on the type of image and the desired precision. For instance, the threshold value (0.01 in the tutorial) can be increased or decreased to detect fewer or more corners, respectively.

  • If you're interested in detecting corners with the Shi-Tomasi method instead, you can use the cv2.goodFeaturesToTrack() function. It often provides better results, especially in cases with more prominent and clearer corners.

Conclusion:

Detecting corners in an image is a foundational task in computer vision. The Harris Corner Detection method provided by OpenCV is a robust method for this purpose. Proper parameter tuning is essential to achieve the best results tailored to the specific image or application.

  1. Corner Detection in OpenCV:

    • Description: An introduction to corner detection and its importance in computer vision.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg', 0)
      
      # Apply corner detection algorithm (e.g., Harris)
      corners = cv2.cornerHarris(img, blockSize, ksize, k)
      
      # Display the result
      cv2.imshow('Corners', corners)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  2. Harris Corner Detection in OpenCV:

    • Description: Explains the Harris corner detection algorithm and its implementation in OpenCV.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg', 0)
      
      # Apply Harris corner detection
      corners = cv2.cornerHarris(img, blockSize, ksize, k)
      
      # Threshold and mark the corners
      img[corners > threshold * corners.max()] = [0, 0, 255]
      
      # Display the result
      cv2.imshow('Harris Corners', img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  3. Shi-Tomasi Corner Detection in OpenCV:

    • Description: Overview of Shi-Tomasi corner detection, which is often preferred over Harris for feature extraction.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg', 0)
      
      # Detect Shi-Tomasi corners
      corners = cv2.goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance)
      
      # Draw corners on the image
      for corner in corners:
          x, y = corner.ravel()
          cv2.circle(img, (x, y), 3, 255, -1)
      
      # Display the result
      cv2.imshow('Shi-Tomasi Corners', img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  4. Corner Detection and Feature Extraction in OpenCV:

    • Description: Demonstrates how to extract features from detected corners.
    • Code:
      import cv2
      import numpy as np
      
      # Read the image
      img = cv2.imread('image.jpg', 0)
      
      # Detect corners using Shi-Tomasi
      corners = cv2.goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance)
      
      # Compute features (e.g., using SIFT or ORB)
      orb = cv2.ORB_create()
      keypoints, descriptors = orb.detectAndCompute(img, None)
      
      # Draw keypoints on the image
      img_keypoints = cv2.drawKeypoints(img, keypoints, None)
      
      # Display the result
      cv2.imshow('Corners and Features', img_keypoints)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
  5. Real-time Corner Detection using OpenCV:

    • Description: Shows how to perform real-time corner detection from a camera feed.
    • Code:
      import cv2
      
      # Initialize camera
      cap = cv2.VideoCapture(0)
      
      while True:
          # Capture frame-by-frame
          ret, frame = cap.read()
      
          # Convert to grayscale
          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      
          # Apply corner detection (e.g., Shi-Tomasi)
          corners = cv2.goodFeaturesToTrack(gray, maxCorners, qualityLevel, minDistance)
      
          # Draw corners on the frame
          for corner in corners:
              x, y = corner.ravel()
              cv2.circle(frame, (x, y), 3, 255, -1)
      
          # Display the result
          cv2.imshow('Real-time Corner Detection', frame)
      
          # Break the loop on 'q' key press
          if cv2.waitKey(1) & 0xFF == ord('q'):
              break
      
      # Release the camera and close windows
      cap.release()
      cv2.destroyAllWindows()
      
  6. FAST Corner Detection in OpenCV:

    • Description: Focuses on the FAST (Features from Accelerated Segment Test) corner detection algorithm.
    • Code:
      import cv2
      
      # Read the image
      img = cv2.imread('image.jpg', 0)
      
      # Create FAST object
      fast = cv2.FastFeatureDetector_create()
      
      # Detect and draw FAST corners
      kp = fast.detect(img, None)
      img_fast = cv2.drawKeypoints(img, kp, None, color=(255, 0, 0))
      
      # Display the result
      cv2.imshow('FAST Corners', img_fast)
      cv2.waitKey(0)
      cv2.destroyAllWindows()