opencv角點檢測,適用於角點周圍無干擾的情況。如下圖這種情況。
如下圖:周圍有多個角點、或者直線等的干擾,效果是異常的驚人。
1.Moravec角點檢測算法
2.Harris角點檢測
3.Shi-Tomasi 算法
參考鏈接:鏈接
代碼如下(示例):
import numpy as np
import cv2,os,glob
from matplotlib import pyplot as plt
def harris():
file=r'/data2/enducation/datas/answer_card/cornor_detect'
for path in glob.glob(os.path.join(file,"*.jpg")):
img = cv2.imread(path)
resize=50
scale=min(resize/img.shape[0],resize/img.shape[1])
img=cv2.resize(img,(0,0),fx=scale,fy=scale)
# 1. Harris?????????
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,binary=cv2.threshold(gray,127, 255, cv2.THRESH_BINARY)
# 2. Harris????
dst = cv2.cornerHarris(binary, 2, 3, 0.1)
# ?????????
dst = cv2.dilate(dst, None)
# ???????
img[dst > 0.01 * dst.max()] = [0, 0, 255]
cv2.imwrite('blox-RedPoint.png', img)
cv2.imshow('dst', img)
cv2.waitKey(0)
def ShiTomasi(img_path):
img = cv2.imread(img_path)
resize = 50
scale = min(resize / img.shape[0], resize / img.shape[1])
image = cv2.resize(img, (0, 0), fx=scale, fy=scale)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gaussi = cv2.GaussianBlur(gray, (5, 5), 0)
ret, binary = cv2.threshold(gaussi, 127, 255, cv2.THRESH_BINARY)
# canny = cv2.Canny(GAOSI, 30, 150)
# Shi-Tomasi????
corners = cv2.goodFeaturesToTrack(binary,maxCorners=2,qualityLevel=0.5,minDistance=4)
'''
maxCorners=1 point numbers
qualityLevel=0.5 thresh
minDistance=4 disdance between points
'''
corners = np.int0(corners) # 20 ?????
# ??????[[[62, 64]]] -> [62, 64]
# x, y = corners.ravel()
for i in corners:
x, y = i.ravel()
x=int(img.shape[1]/resize*x)
y=int(img.shape[0]/resize*y)
print(x,y)
cv2.circle(img, (x, y), 2, (0, 0, 255), -1)
cv2.imwrite('Shi-Tomasi-corner.jpg', img)
cv2.imshow('dst', img)
cv2.waitKey(0)
if __name__ == '__main__':
# file = r'/data2/enducation/datas/answer_card/erro_image'
file=r"/data2/enducation/datas/answer_card/cornor_detect"
for path in glob.glob(os.path.join(file, "*.jpg" and "*.png")):
try:
ShiTomasi(path)
except:
pass
這個確實能精准找到那個角點,但是隨著使用場景的不同、圖像數據被干擾會導致效果也不太穩定,必然造成使用的局限性。因此需要另謀高就,如果我發現好的檢測手段,我會繼續更新。
如下圖為理想效果,如果你有好的想法,歡迎進行技術交流。