Python+OpenCV实现单个圆形孔和针检测

 更新时间:2022年10月28日 14:33:32   作者:天人合一peng  
这篇文章主要为大家详细介绍了如何通过Python+OpenCV实现单个圆形孔和针检测功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

如果中间红色区域是针则可以用下面的代码检测,其阈值和斑点检测的参数根据图像像素值做相应修改

检测的主要思路是先通过找到外面的大圆,再通过圆心定位出一个ROI区域,在ROI区域中检测中心的检测对象

import os
import cv2
import numpy as np
import math
 
# 检测针脚位置
def needelCenter_detect(img):
    params = cv2.SimpleBlobDetector_Params()
    # Setup SimpleBlobDetector parameters.
    # print('params')
    # print(params)
    # print(type(params))
 
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 100
    params.maxArea = 10e3
    params.minDistBetweenBlobs = 50
    # params.filterByColor = True
    params.filterByConvexity = False
    # tweak these as you see fit
    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = 0.2
    # params.blobColor = 0
    # # # Filter by Convexity
    # params.filterByConvexity = True
    # params.minConvexity = 0.87
    # Filter by Inertia
    # params.filterByInertia = True
    # params.filterByInertia = False
    # params.minInertiaRatio = 0.01
 
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
    # Detect blobs.
    minThreshValue = 110
    _, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY)
    # gray = cv2.resize(gray, dsize=None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
    # plt.imshow(gray)
    # cv2.imshow("gray",gray)
 
    # 找到距离原点(0,0)最近和最远的点
 
    detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(gray)
    # print(len(keypoints))
    # print(keypoints[0].pt[0])
    # 如果这儿没检测到可能会出错
    if len(keypoints) == 0:
        print("没有检测到针角坐标,可能需要调整针角斑点检测参数")
        return keypoints
 
    else:
        print(len(keypoints))
        im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (255, 0, 0),
                                              cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
 
        # if keypoints is not None:
 
        color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)
        # 画出圆的圆心
        cv2.circle(color_img, (int(keypoints[0].pt[0]), int(keypoints[0].pt[1])), 5, (0, 255, 0), -1)
        cv2.imshow("color_img",color_img)
        # cv2.waitKey()
 
        return keypoints
 
 
 
# 检测连接器圆形位置
def circle_detect(image):
    # 灰度化
    circle_img = image.copy()
    gray = cv2.cvtColor(circle_img, cv2.COLOR_BGR2GRAY)
    # 输出图像大小,方便根据图像大小调节minRadius和maxRadius
    # print(image.shape)
    # 进行中值滤波
    img = cv2.medianBlur(gray, 3)
 
    # 针角圆心坐标
    out_x = 0
    out_y = 0
 
    # 霍夫变换圆检测
    circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10e10, param1=100, param2=30, minRadius=10, maxRadius=100)
    # 如果没检测到会报错
    # 这种判断方式过于简单
    if circles is None:
        print("没有检测到连接器外圆")
 
    else:
        for circle in circles[0]:
            # 圆的基本信息
            # print(circle[2])
            # 坐标行列-圆心坐标
            out_x = int(circle[0])
            out_y = int(circle[1])
            # 半径
            r = int(circle[2])
            # 在原图用指定颜色标记出圆的边界
            cv2.circle(circle_img, (out_x, out_y), r, (0, 0, 255), 2)
            # # 画出圆的圆心
            cv2.circle(circle_img, (out_x, out_y), 3, (0, 255, 255), -1)
 
 
        # 记录外圆坐标
        out_xpoint = out_x
        out_ypoint = out_y
 
        # 只框出单个针角的位置区域
        step_center = 30
        step_rect = 60
        out_x -= step_center
        out_y -= step_center
 
        needleRect = image[out_y: out_y + step_rect, out_x: out_x + step_rect]
        # cv2.imshow("needleRect", needleRect)
 
        # 根据检测到的圆形连接器中心找针角位置
        centerPoint = needelCenter_detect(needleRect)
 
        if len(centerPoint) == 0:
            print("调整位置")
        else:
                # 将针角的坐标原还至原图
            in_x = int(centerPoint[0].pt[0])
            in_y = int(centerPoint[0].pt[1])
            in_x +=   out_x
            in_y +=   out_y
 
            # 画出针角的圆心
            cv2.circle(circle_img, (in_x, in_y), 3, (0, 255, 0), -1)
 
            # 计算两者的距离
            # 假设通过标定其一个像素代表0.0056mm
            DPI = 0.00568
            dis = math.sqrt(math.pow(out_xpoint - in_x,2) + math.pow(out_ypoint - in_y,2))
            print("两者相互之间的距离为(mm):", dis*DPI)
 
 
            cv2.imshow("image",circle_img)
            cv2.waitKey(1)
 
 
 
if __name__ == "__main__":
 
    # # 测试0 如果是小图  需要将检测程序中的cv2.waitKey(1)修改为cv2.waitKey()不然看到图片
    # image = cv2.imread("images/CircleLinker/CLinker01.jpg")
    # # cv2.imshow("show",image)
    # # cv2.waitKey()
    # roi = image
    # circle_detect(roi)
 
 
 
    # 测试1 从原图中换到连接器位置
    image = cv2.imread("SingleImages/src/single.jpg")
    # cv2.imshow("show",image)
    # cv2.waitKey()
    # 如何准确找到圆形连接器 ---》用yolo训练后能准备找到
    roi = image[1800:2300, 1800:2300 ]
    # cv2.imshow("show",roi)
    # cv2.waitKey()
    circle_detect(roi)
 
 
 
    # # 测试2 如果是小图  需要将检测程序中的cv2.waitKey(1)修改为cv2.waitKey()不然看到图片
    # image = cv2.imread("SingleImages/single04.jpg")
    # # cv2.imshow("show",image)
    # # cv2.waitKey()
    # roi = image
    # circle_detect(roi)
 
 
 
    # # 测试3 检测文件夹下所有图片
    # path = r"D:\BUFFER\Pycharm\ZhenJiaoDect\SingleImages"
    # for filename in os.listdir(path):  # listdir的参数是文件夹的路径
    #     filenames = path + '\\' + filename
    #     # print(filenames)
    #     img_orig = cv2.imread(filenames, 1)
    #     print(filenames)
    #
    #     if img_orig is None:
    #         print("Warning: No Pictures")
    #     else:
    #         circle_detect(img_orig)
 
 
    # # # 测试4 打开相机测试
    # # 需要将检测程序中的cv2.waitKey()修改为cv2.waitKey(1)
    # # 否则看到不视频实时检测结果
    # capture = cv2.VideoCapture(0)
    #
    # while (True):
    #     # 获取一帧
    #     ret, frame = capture.read()
    #     circle_detect(frame)
    #
    #     # cv2.imshow('frame', frame)
    #
    #     if cv2.waitKey(1) == ord('q'):
    #         break

以上就是Python+OpenCV实现单个圆形孔和针检测的详细内容,更多关于Python OpenCV圆形孔检测的资料请关注脚本之家其它相关文章!

相关文章

  • python爬虫获取京东手机图片的图文教程

    python爬虫获取京东手机图片的图文教程

    下面小编就为大家分享一篇python爬虫获取京东手机图片的图文教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • pytorch + visdom CNN处理自建图片数据集的方法

    pytorch + visdom CNN处理自建图片数据集的方法

    这篇文章主要介绍了pytorch + visdom CNN处理自建图片数据集的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法

    编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法

    这篇文章主要介绍了编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法,用到了Python的urllib和urllib2模块,需要的朋友可以参考下
    2016-01-01
  • Python伪随机数模块random详解

    Python伪随机数模块random详解

    这篇文章主要为大家详细介绍了Python伪随机数模块random,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Python pandas的八个生命周期总结

    Python pandas的八个生命周期总结

    这篇文章主要从八个pandas的数据处理生命周期,整理汇总出pandas框架在整个数据处理过程中都是如何处理数据的,感兴趣的小伙伴可以了解一下
    2022-10-10
  • Python中match语句的详细用法实例

    Python中match语句的详细用法实例

    match语句接受一个表达式并将其值与作为一个或多个 case 块给出的连续模式进行比较,下面这篇文章主要给大家介绍了关于Python中match语句的详细用法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 利用Python的PyPDF2库提取pdf中的文字

    利用Python的PyPDF2库提取pdf中的文字

    PyPDF2是一个用于处理PDF文件的Python库,它提供了许多用于读取和操作PDF文件的功能,对于需要处理PDF文件的Python应用程序,PyPDF2是一个非常实用的工具库,本文将给大家详细介绍一下如何通过Python的PyPDF2库提取pdf中的文字,需要的朋友可以参考下
    2023-05-05
  • python如何提取xml指定内容

    python如何提取xml指定内容

    这篇文章主要介绍了python如何提取xml指定内容,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • TensorFlow2.0使用keras训练模型的实现

    TensorFlow2.0使用keras训练模型的实现

    这篇文章主要介绍了TensorFlow2.0使用keras训练模型的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python数据分析之pandas函数详解

    Python数据分析之pandas函数详解

    这篇文章主要介绍了Python数据分析之pandas函数详解,文中有非常详细的代码示例,对正在学习python的pandas函数的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-04-04

最新评论