程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

4、 Opencv corner detection algorithm (Python)

編輯:Python

List of articles

  • Preface
  • One 、 Corner detection
  • Two 、 Use steps
    • 1. Corner detection algorithm
    • 2. Code
  • summary


Preface


One 、 Corner detection

opencv Corner detection , It is applicable to the situation that there is no interference around the corner . As shown in the figure below .

Here's the picture : There are multiple corners around 、 Or straight line interference , The effect is extraordinary .

Two 、 Use steps

1. Corner detection algorithm

1.Moravec Corner detection algorithm
2.Harris Corner detection
3.Shi-Tomasi Algorithm
Reference link : link

2. Code

The code is as follows ( Example ):


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

summary

This really can find the corner accurately , But with the different use scenarios 、 If the image data is disturbed, the effect will be unstable , It will inevitably lead to limitations in use . Therefore, we need to find another job , If I find a good detection method , I will continue to update .

The following figure shows the ideal effect , If you have a good idea , Welcome to technical exchanges .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved