Python+OpenCV實(shí)現(xiàn)尋找到圓點(diǎn)標(biāo)定板的角點(diǎn)
圖像大小按原圖計(jì)算
dis_mm是標(biāo)定板上的實(shí)際距離,要根據(jù)真實(shí)情況計(jì)算。
示例代碼
# 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) # 找到距離原點(diǎn)(0,0)最近和最遠(yuǎn)的點(diǎn) h, w = img.shape[:2] detector = cv2.SimpleBlobDetector_create(params) keypoints = detector.detect(gray) print("檢測點(diǎn)為", 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)最近和最遠(yuǎn)的點(diǎn) 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)最近和最遠(yuǎn)的點(diǎn) kpUpRight = [] disUpRight=[] for i in range(len(keypoints)): # 最大距離坐標(biāo) 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) # 顯示在原圖上 原圖減半因?yàn)橹胺糯罅? # 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 # 只計(jì)算斜對角線,約束條件簡單一些,增加適用性 # 單邊長a = 0.05*19 對角線 # DPI = (math.sqrt(1.3435)) / sum(DPIall) dis_mm = math.sqrt(math.pow(15, 2) + math.pow(15, 2)) print("兩點(diǎn)的像素距離為", dis_UR_DL, "實(shí)際距離為", 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)
到此這篇關(guān)于Python+OpenCV實(shí)現(xiàn)尋找到圓點(diǎn)標(biāo)定板的角點(diǎn)的文章就介紹到這了,更多相關(guān)Python OpenCV尋找角點(diǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)地圖可視化folium完整過程
Folium是一個(gè)基于leaflet.js的Python地圖庫,其中,Leaflet是一個(gè)非常輕的前端地圖可視化庫,本文重點(diǎn)給大家介紹Python實(shí)現(xiàn)地圖可視化folium完整過程,感興趣的朋友跟隨小編一起看看吧2021-05-05Python解析json文件相關(guān)知識學(xué)習(xí)
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式。接下來通過本文給大家介紹python解析json文件相關(guān)知識,對python解析json文件相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03女友半夜加班發(fā)自拍 python男友用30行代碼發(fā)現(xiàn)驚天秘密
大家好,我是Lex 喜歡欺負(fù)超人那個(gè)Lex 女友說今晚加班,還給我發(fā)了一張照片? 我心生懷疑,就用python分析了一下照片,結(jié)果發(fā)現(xiàn)。。。 劃重點(diǎn):利用Python讀取照片的GPS信息信息2021-08-08Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法
今天小編就為大家分享一篇Python提取轉(zhuǎn)移文件夾內(nèi)所有.jpg文件并查看每一幀的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python學(xué)習(xí)開發(fā)之圖形用戶界面詳解
當(dāng)前流行的計(jì)算機(jī)桌面應(yīng)用程序大多數(shù)為圖形化用戶界面(Graphic User Interface,GUI),python也提供了多個(gè)圖形開發(fā)界面的庫,這篇文章主要給大家介紹了關(guān)于Python學(xué)習(xí)開發(fā)之圖形用戶界面的相關(guān)資料,需要的朋友可以參考下2021-08-08python批量生成身份證號到Excel的兩種方法實(shí)例
這篇文章主要給大家介紹了關(guān)于python批量生成身份證號到Excel的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01