Python+OpenCV實(shí)現(xiàn)單個(gè)圓形孔和針檢測(cè)
如果中間紅色區(qū)域是針則可以用下面的代碼檢測(cè),其閾值和斑點(diǎn)檢測(cè)的參數(shù)根據(jù)圖像像素值做相應(yīng)修改
檢測(cè)的主要思路是先通過(guò)找到外面的大圓,再通過(guò)圓心定位出一個(gè)ROI區(qū)域,在ROI區(qū)域中檢測(cè)中心的檢測(cè)對(duì)象
import os import cv2 import numpy as np import math # 檢測(cè)針腳位置 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) # 找到距離原點(diǎn)(0,0)最近和最遠(yuǎn)的點(diǎn) detector = cv2.SimpleBlobDetector_create(params) keypoints = detector.detect(gray) # print(len(keypoints)) # print(keypoints[0].pt[0]) # 如果這兒沒(méi)檢測(cè)到可能會(huì)出錯(cuò) if len(keypoints) == 0: print("沒(méi)有檢測(cè)到針角坐標(biāo),可能需要調(diào)整針角斑點(diǎn)檢測(cè)參數(shù)") 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) # 畫(huà)出圓的圓心 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 # 檢測(cè)連接器圓形位置 def circle_detect(image): # 灰度化 circle_img = image.copy() gray = cv2.cvtColor(circle_img, cv2.COLOR_BGR2GRAY) # 輸出圖像大小,方便根據(jù)圖像大小調(diào)節(jié)minRadius和maxRadius # print(image.shape) # 進(jìn)行中值濾波 img = cv2.medianBlur(gray, 3) # 針角圓心坐標(biāo) out_x = 0 out_y = 0 # 霍夫變換圓檢測(cè) circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10e10, param1=100, param2=30, minRadius=10, maxRadius=100) # 如果沒(méi)檢測(cè)到會(huì)報(bào)錯(cuò) # 這種判斷方式過(guò)于簡(jiǎn)單 if circles is None: print("沒(méi)有檢測(cè)到連接器外圓") else: for circle in circles[0]: # 圓的基本信息 # print(circle[2]) # 坐標(biāo)行列-圓心坐標(biāo) out_x = int(circle[0]) out_y = int(circle[1]) # 半徑 r = int(circle[2]) # 在原圖用指定顏色標(biāo)記出圓的邊界 cv2.circle(circle_img, (out_x, out_y), r, (0, 0, 255), 2) # # 畫(huà)出圓的圓心 cv2.circle(circle_img, (out_x, out_y), 3, (0, 255, 255), -1) # 記錄外圓坐標(biāo) out_xpoint = out_x out_ypoint = out_y # 只框出單個(gè)針角的位置區(qū)域 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) # 根據(jù)檢測(cè)到的圓形連接器中心找針角位置 centerPoint = needelCenter_detect(needleRect) if len(centerPoint) == 0: print("調(diào)整位置") else: # 將針角的坐標(biāo)原還至原圖 in_x = int(centerPoint[0].pt[0]) in_y = int(centerPoint[0].pt[1]) in_x += out_x in_y += out_y # 畫(huà)出針角的圓心 cv2.circle(circle_img, (in_x, in_y), 3, (0, 255, 0), -1) # 計(jì)算兩者的距離 # 假設(shè)通過(guò)標(biāo)定其一個(gè)像素代表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__": # # 測(cè)試0 如果是小圖 需要將檢測(cè)程序中的cv2.waitKey(1)修改為cv2.waitKey()不然看到圖片 # image = cv2.imread("images/CircleLinker/CLinker01.jpg") # # cv2.imshow("show",image) # # cv2.waitKey() # roi = image # circle_detect(roi) # 測(cè)試1 從原圖中換到連接器位置 image = cv2.imread("SingleImages/src/single.jpg") # cv2.imshow("show",image) # cv2.waitKey() # 如何準(zhǔn)確找到圓形連接器 ---》用yolo訓(xùn)練后能準(zhǔn)備找到 roi = image[1800:2300, 1800:2300 ] # cv2.imshow("show",roi) # cv2.waitKey() circle_detect(roi) # # 測(cè)試2 如果是小圖 需要將檢測(cè)程序中的cv2.waitKey(1)修改為cv2.waitKey()不然看到圖片 # image = cv2.imread("SingleImages/single04.jpg") # # cv2.imshow("show",image) # # cv2.waitKey() # roi = image # circle_detect(roi) # # 測(cè)試3 檢測(cè)文件夾下所有圖片 # path = r"D:\BUFFER\Pycharm\ZhenJiaoDect\SingleImages" # for filename in os.listdir(path): # listdir的參數(shù)是文件夾的路徑 # 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) # # # 測(cè)試4 打開(kāi)相機(jī)測(cè)試 # # 需要將檢測(cè)程序中的cv2.waitKey()修改為cv2.waitKey(1) # # 否則看到不視頻實(shí)時(shí)檢測(cè)結(jié)果 # 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實(shí)現(xiàn)單個(gè)圓形孔和針檢測(cè)的詳細(xì)內(nèi)容,更多關(guān)于Python OpenCV圓形孔檢測(cè)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
pyqt QPlainTextEdit 中捕獲回車(chē)的示例代碼
在PyQt的QPlainTextEdit控件中,可以通過(guò)重寫(xiě)keyPressEvent()函數(shù)來(lái)捕獲鍵盤(pán)事件,這篇文章主要介紹了pyqt QPlainTextEdit 中捕獲回車(chē),需要的朋友可以參考下2024-03-03Python用61行代碼實(shí)現(xiàn)圖片像素化的示例代碼
這篇文章主要介紹了Python用61行代碼實(shí)現(xiàn)圖片像素化的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Python常用數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換總結(jié)
在本篇文章里小編給大家整理的是關(guān)于Python中常用數(shù)據(jù)類(lèi)型之間的轉(zhuǎn)換相關(guān)知識(shí)點(diǎn),有需要的朋友們可以學(xué)習(xí)下2019-09-09基于python實(shí)現(xiàn)弱密碼檢測(cè)工具
Python中一個(gè)強(qiáng)大的加密模塊,提供了許多常見(jiàn)的加密算法和工具,本文我們將使用Python編寫(xiě)一個(gè)弱密碼檢測(cè)工具,感興趣的小伙伴可以了解一下2024-01-01Python3+selenium配置常見(jiàn)報(bào)錯(cuò)解決方案
這篇文章主要介紹了Python3+selenium配置常見(jiàn)報(bào)錯(cuò)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08