四、opencv角点检测算法(python)

文章目录

  • 前言
  • 一、角点检测
  • 二、使用步骤
    • 1.角点检测算法
    • 2.代码
  • 总结


前言


一、角点检测

opencv角点检测,适用于角点周围无干扰的情况。如下图这种情况。
四、opencv角点检测算法(python)_第1张图片
如下图:周围有多个角点、或者直线等的干扰,效果是异常的惊人。
四、opencv角点检测算法(python)_第2张图片

二、使用步骤

1.角点检测算法

1.Moravec角点检测算法
2.Harris角点检测
3.Shi-Tomasi 算法
参考链接:链接

2.代码

代码如下(示例):


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

总结

这个确实能精准找到那个角点,但是随着使用场景的不同、图像数据被干扰会导致效果也不太稳定,必然造成使用的局限性。因此需要另谋高就,如果我发现好的检测手段,我会继续更新。

如下图为理想效果,如果你有好的想法,欢迎进行技术交流。
四、opencv角点检测算法(python)_第3张图片

你可能感兴趣的:(OpenCV,计算机视觉,opencv,python)