[PYTHON] Feature detection using opencv (corner detection)

Introduction

About detection of image features using opencv. This time, we will detect corners (curves, etc.). Two patterns are detected. See the following page for the basics Basics of binarized image processing by Python ⇒ https://qiita.com/jin237/items/04ca3d0b56e10065c4e4

let's try it

Although opencv is used, there are two methods for corner detection.

Corner detection by "cv2.cornerHarris"

Assumed that the edge has a high brightness change in all directions.

#Corner detection from sample image
import matplotlib.pyplot as plt
%matplotlib inline
import cv2


#Image loading and binarization
img = cv2.imread("sample.png ",0)
#Corner detection
corners = cv2.cornerHarris(img, 3,1,0.04)
plt.imshow(corners, cmap='gray')
plt.savefig('gray_pltsample')

gray_pltsample.png

img = cv2.imread("sample.png ", 0)

By, "0" enables reading as a binary image at the same time as reading the image. Originally, it could be written as "cv2.cvtColor (img, cv2.COLOR_RGB2GRAY)", but this time I used this method to make the process easier to see. It is displayed and saved by matplotlib. Also,

corners = cv2.cornerHarris(img, 3,1,0.04)

about, 3 = Near pixel range (block size) 1 = kernel size (ksize) 0.04 = Harris detector free parameter (k)

blockSize --The size of the adjacent area to consider when detecting corners. ksize --Sobel gradient operator kernel size. k --Free parameters in the equation.

For the theory, see Harris Corner Detection.

Corner detection by "cv2.goodFeaturesToTrack"

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('sample.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

corners = cv2.goodFeaturesToTrack(gray,100000000,0.01,10)
corners = np.int0(corners)

for i in corners:
    x,y = i.ravel()
    cv2.circle(img,(x,y),3,255,-1)

plt.imshow(img),plt.show()

gFTT_lenna.png

You can make points for feature points. I set it a lot this time, but there is not much change in the result. If you devise this red dot, it may be a little easier to understand.

at the end

A corner was detected. It's easy to do, so it's a good idea to understand it, including theory. There are other than the ones introduced above, so I will write them in other articles.

Recommended Posts

Feature detection using opencv (corner detection)
[Python] Using OpenCV with Python (Edge Detection)
OpenCV feature detection with Google Colaboratory
I tried object detection using Python and OpenCV
Facial expression detection using Yolov5
Try edge detection with OpenCV
[Python] Using OpenCV with Python (Basic)
Try using OpenCV on Windows
Real-time edge detection with OpenCV
Try using Django's template feature
Face detection with Python + OpenCV
Try using Pelican's draft feature
Gamma correction without using OpenCV
Organized feature selection using sklearn
Using OpenCV with Python @Mac
Anime face detection with OpenCV
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
Let's dig a little deeper into feature point matching using OpenCV
Vertical Tower of Pisa using OpenCV
[Python] Using OpenCV with Python (Image Filtering)
Logo detection using TensorFlow Object Detection API
[Python] Using OpenCV with Python (Image transformation)
Make OpenCV object detection rotation invariant
I tried using GrabCut of OpenCV
Try using PyCharm's remote debugging feature
Outlier detection using One Class SVM
Horizon processing using OpenCV morphology transformation
Cat detection with OpenCV (model distribution)
Face detection using a cascade classifier