圆形检测--轮廓检测法

本文编写了一种轮廓对比方法检测圆形,比霍夫检测更准确一些。
输入为路径,如果有圆,则返回圆的轮廓点,还可以画出来。
备注: cnt2 是圆的集合点,我事先把圆的集合点保存为了numpy格式,在调用就ok。

import numpy as np
import cv2

def img_read(path):  #读图片,返回二值图
    image = cv2.imread(path)
    img_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)   
    ret,th1 = cv2.threshold(img_gray,200,255,cv2.THRESH_BINARY)
    return th1
def img_erosion(image,k):   #图像腐蚀 增强线条 k为核大小
    kernel = np.ones((k,k),np.uint8)  
    erosion = cv2.erode(image,kernel,iterations = 1)
#    dilation = cv2.dilate(th1,kernel,iterations = 1)
    return erosion
def drawContours(image,counts): #根据点画出点图像
    color = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
    dst = cv2.drawContours(color, counts, -1, (0,255,0), 2) 
    cv2.imshow("dst", dst)
    cv2.waitKey(0)

path="test_images/26.jpg"  #图片路径
#path2="test_images/20.jpg"
def circle(path): #检测圆,返回圆的列表
    img1=img_read(path)
    img_er=img_erosion(img1,3)    #图像增强
    img1,contours1,hierarchy1=cv2.findContours(img_er,cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)     #找轮廓点
    ret_list=[] #相似率
    cnt_list=[] #圆的点
    for contour in contours1:   
        cnt1 = contour     
        cnt2 = np.load("counts2.npy")
        ret = cv2.matchShapes(cnt1,cnt2,1,0.0)   #图片1与cnt2(标准圆的轮廓点)做匹配对比
        if ret<0.005: #阀值设置   #越小,则越像圆
            ret_list.append(ret)
            cnt_list.append(cnt1)
#    print(ret_list)
    drawContours(img1,cnt_list)   #画出图像
    return cnt_list

circle(path)   #运行程序
  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值