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


前言


一、角点检测

opencv角点检测,适用于角点周围无干扰的情况。如下图这种情况。
在这里插入图片描述
如下图:周围有多个角点、或者直线等的干扰,效果是异常的惊人。
在这里插入图片描述

二、使用步骤

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

总结

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

如下图为理想效果,如果你有好的想法,欢迎进行技术交流。
在这里插入图片描述

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值