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
Although opencv is used, there are two methods for corner detection.
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')
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.
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()
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.
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