Python+OpenCV实现寻找到圆点标定板的角点

 更新时间:2022年11月06日 10:27:06   作者:天人合一peng  
这篇文章主要为大家详细介绍了Python+OpenCV实现找到圆点标定板所有点后通过距离找两个角点,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下

图像大小按原图计算

dis_mm是标定板上的实际距离,要根据真实情况计算。

示例代码

# coding:utf-8
import math
import cv2
import numpy as np
import xml.etree.ElementTree as ET
 
import matplotlib.pyplot as plt
 
 
global DPI
DPI =  0.00245
 
def mainFigure(img):
    w = 20
    h = 5
    params = cv2.SimpleBlobDetector_Params()
    # Setup SimpleBlobDetector parameters.
    # print('params')
    # print(params)
    # print(type(params))
 
 
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 10e1
    params.maxArea = 10e4
    # 图大要修改  100
    params.minDistBetweenBlobs = 100
    # 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.
    # image = cv2.resize(gray_img, (int(img.shape[1]/4),int(img.shape[0]/4)), 1, 1, cv2.INTER_LINEAR)
    # image = cv2.resize(gray_img, dsize=None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)
    minThreshValue = 60
    _, gray = cv2.threshold(gray, minThreshValue, 255, cv2.THRESH_BINARY)
    # gray = cv2.resize(gray, dsize=None, fx=1, fy=1, interpolation=cv2.INTER_LINEAR)
    # gray = cv2.resize(gray, dsize=None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
 
    # plt.imshow(gray)
    # cv2.imshow("gray",gray)
 
    # 找到距离原点(0,0)最近和最远的点
    h, w = img.shape[:2]
 
    detector = cv2.SimpleBlobDetector_create(params)
    keypoints = detector.detect(gray)
    print("检测点为", len(keypoints))
    # opencv
    im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    # plt
    # fig = plt.figure()
    # im_with_keypoints = cv2.drawKeypoints(gray, keypoints, np.array([]), (0, 0, 255),  cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    color_img = cv2.cvtColor(im_with_keypoints, cv2.COLOR_BGR2RGB)
 
    DPIall = []
 
    if keypoints is not None:
        # 找到距离(0,0)最近和最远的点
        kpUpLeft = []
        disUpLeft = []
        for i in range(len(keypoints)):
            dis = math.sqrt(math.pow(keypoints[i].pt[0],2) + math.pow(keypoints[i].pt[1],2))
            disUpLeft.append(dis)
            kpUpLeft.append(keypoints[i].pt)
            # cv2.circle(img, (int(keypoints[i].pt[0]), int(keypoints[i].pt[1])), 10, (0, 255, 0), 2)
 
        # 找到距离(640*2,0)最近和最远的点
        kpUpRight = []
        disUpRight=[]
        for i in range(len(keypoints)):
            # 最大距离坐标
            dis2 = math.sqrt(math.pow(abs(keypoints[i].pt[0]-w),2) + math.pow(abs(keypoints[i].pt[1]),2))
            disUpRight.append(dis2)
            kpUpRight.append(keypoints[i].pt)
 
 
        if disUpRight and disUpLeft:
            disDownLeftIndex = disUpRight.index(max(disUpRight))
            pointDL = kpUpRight[disDownLeftIndex]
 
            disUpRightIndex = disUpRight.index(min(disUpRight))
            pointUR = kpUpLeft[disUpRightIndex]
 
 
            disDownRightIndex = disUpLeft.index(max(disUpLeft))
            pointDR = kpUpLeft[disDownRightIndex]
 
            disUpLeftIndex = disUpLeft.index(min(disUpLeft))
            pointUL = kpUpLeft[disUpLeftIndex]
 
 
            if (pointDR is not None) and (pointUL is not None) and (pointDL is not None) and (pointUR is not None):
                # cv2.circle(color_img, (int(pointDR[0]),int(pointDR[1])), 30, (0, 255, 0),2)
                # cv2.circle(color_img, (int(pointUL[0]),int(pointUL[1])), 30, (0, 255, 0),2)
                # cv2.line(color_img,(int(pointDR[0]),int(pointDR[1])), (int(pointDL[0]),int(pointDL[1])),(0, 0, 255),2)
                #
                # cv2.circle(color_img, (int(pointDL[0]),int(pointDL[1])), 30, (0, 255, 0),2)
                # cv2.circle(color_img, (int(pointUR[0]),int(pointUR[1])), 30, (0, 255, 0),2)
                # cv2.line(color_img, (int(pointDL[0]),int(pointDL[1])), (int(pointUR[0]),int(pointUR[1])), (0, 0, 255), 2)
                # cv2.line(color_img, (int(pointUL[0]),int(pointUL[1])), (int(pointUR[0]),int(pointUR[1])), (0, 0, 255), 2)
 
                # 显示在原图上 原图减半因为之前放大了
                # cv2.circle(img, (int(pointDR[0]/2), int(pointDR[1]/2)), 10, (0, 255, 0), 2)
                # cv2.circle(img, (int(pointUL[0]/2), int(pointUL[1]/2)), 10, (0, 255, 0), 2)
                # cv2.line(img,(int(pointDR[0]/2),int(pointDR[1]/2)), (int(pointUL[0]/2),int(pointUL[1]/2)),(0, 0, 255),2)
                # dis_UR_DL = math.sqrt(math.pow(pointUR[0]-pointDL[0], 2) + math.pow(pointUR[1]-pointDL[1], 2))/2
 
                cv2.circle(img, (int(pointDR[0] ), int(pointDR[1] )), 10, (0, 255, 0), 2)
                cv2.circle(img, (int(pointUL[0] ), int(pointUL[1] )), 10, (0, 255, 0), 2)
                cv2.line(img, (int(pointDR[0] ), int(pointDR[1] )), (int(pointUL[0] ), int(pointUL[1] )),
                         (0, 0, 255), 2)
                dis_UR_DL = math.sqrt(math.pow(pointUR[0] - pointDL[0], 2) + math.pow(pointUR[1] - pointDL[1], 2))
 
                DPIall.append(dis_UR_DL)
 
                global DPI
                # 只计算斜对角线,约束条件简单一些,增加适用性
                # 单边长a = 0.05*19 对角线
                # DPI = (math.sqrt(1.3435)) / sum(DPIall)
 
                dis_mm = math.sqrt(math.pow(15, 2) + math.pow(15, 2))
                print("两点的像素距离为", dis_UR_DL, "实际距离为", dis_mm)
                DPI = dis_mm / dis_UR_DL
                print("DPI", DPI)
 
 
                # configFile_xml = "wellConfig.xml"
                # tree = ET.parse(configFile_xml)
                # root = tree.getroot()
                # secondRoot = root.find("DPI")
                # print(secondRoot.text)
                #
                # secondRoot.text = str(DPI)
                # tree.write("wellConfig.xml")
                # print("DPI", DPI)
            else:
                pass
            print(DPI)
 
    # plt.imshow(color_img,interpolation='bicubic')
    # fname = "key points"
    # titlestr = '%s found %d keypoints' % (fname, len(keypoints))
    # plt.title(titlestr)
    # # fig.canvas.set_window_title(titlestr)
    # plt.show()
 
    # cv2.imshow('findCorners', color_img)
    cv2.namedWindow('findCorners',2)
    cv2.imshow('findCorners', img)
    cv2.waitKey()
 
 
 
if __name__ == "__main__":
 
    # # # 单张图片测试
    # DPI hole
    # 0.01221465904139037
    #
    # DPI needle
    # 0.012229753249515942
    # img = cv2.imread("TwoBiaoDing/ROI_needle.jpg",1)
    img = cv2.imread("TwoBiaoDing/ROI_holes.jpg",1)
 
    img_roi = img.copy()
    # img_roi = img[640:2000, 1530:2800]
    # cv2.namedWindow("img_roi",2)
    # cv2.imshow("img_roi", img_roi)
    # cv2.waitKey()
    # img = cv2.imread("circles/Snap_0.jpg",1)
 
    mainFigure(img_roi)
 
    # # 所有图片测试
    # for i in range(15):
    #     fileName = "Snap_" + str(i) + ".jpg"
    # # img = cv2.imread("circles/Snap_007.jpg",1)
    #     img = cv2.imread("circles/" + fileName,1)
    #     print(fileName)
    #     mainFigure(img)

到此这篇关于Python+OpenCV实现寻找到圆点标定板的角点的文章就介绍到这了,更多相关Python OpenCV寻找角点内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python实现地图可视化folium完整过程

    Python实现地图可视化folium完整过程

    Folium是一个基于leaflet.js的Python地图库,其中,Leaflet是一个非常轻的前端地图可视化库,本文重点给大家介绍Python实现地图可视化folium完整过程,感兴趣的朋友跟随小编一起看看吧
    2021-05-05
  • python程序的组织结构详解

    python程序的组织结构详解

    这篇文章主要为大家介绍了python程序的组织结构,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-12-12
  • Python解析json文件相关知识学习

    Python解析json文件相关知识学习

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。接下来通过本文给大家介绍python解析json文件相关知识,对python解析json文件相关知识感兴趣的朋友一起学习吧
    2016-03-03
  • python中的文件打开与关闭操作命令介绍

    python中的文件打开与关闭操作命令介绍

    下面小编就为大家分享一篇python中的文件打开与关闭操作命令介绍,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • 女友半夜加班发自拍 python男友用30行代码发现惊天秘密

    女友半夜加班发自拍 python男友用30行代码发现惊天秘密

    大家好,我是Lex 喜欢欺负超人那个Lex 女友说今晚加班,还给我发了一张照片? 我心生怀疑,就用python分析了一下照片,结果发现。。。 划重点:利用Python读取照片的GPS信息信息
    2021-08-08
  • Python图片处理模块PIL操作方法(pillow)

    Python图片处理模块PIL操作方法(pillow)

    这篇文章主要介绍了Python图片处理模块PIL操作方法(pillow),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Python提取转移文件夹内所有.jpg文件并查看每一帧的方法

    Python提取转移文件夹内所有.jpg文件并查看每一帧的方法

    今天小编就为大家分享一篇Python提取转移文件夹内所有.jpg文件并查看每一帧的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python安装Graphviz 超详细图文教程

    Python安装Graphviz 超详细图文教程

    这篇文章主要介绍了Python安装Graphviz 详细教程,在Python安装Graphviz画图器,首先要明确他是一个独立的软件,如果大家用pip的方法装了graphviz可以先卸载,本文通过图文并茂的形式详细讲解,需要的朋友参考下吧
    2023-02-02
  • Python学习开发之图形用户界面详解

    Python学习开发之图形用户界面详解

    当前流行的计算机桌面应用程序大多数为图形化用户界面(Graphic User Interface,GUI),python也提供了多个图形开发界面的库,这篇文章主要给大家介绍了关于Python学习开发之图形用户界面的相关资料,需要的朋友可以参考下
    2021-08-08
  • python批量生成身份证号到Excel的两种方法实例

    python批量生成身份证号到Excel的两种方法实例

    这篇文章主要给大家介绍了关于python批量生成身份证号到Excel的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01

最新评论