圓形的辨識 --- cv2.HoughCircles

★cv2.HoughCircles的使用&格式


★範例所使用的圖檔下載位置

http://imgur.com/EyLp3Qx

★程式範例執行


import cv2
import numpy as np

img = cv2.imread('opencv_logo.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,
                            1,20,
                            param1=50,
                            param2=40, 
                            minRadius=0,
                            maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),3)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()


★範例執行前後之比較


★套用於齒輪辨識


import numpy as np
import cv2
from matplotlib import pyplot as plt

# 讀取圖檔
img = cv2.imread("gear1.png", 1)
# 圖檔轉灰階
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 灰階後進行存檔
cv2.imwrite('graygear.png', imgGray)

# 使用灰階圖抓圓
circles = cv2.HoughCircles(imgGray,cv2.HOUGH_GRADIENT,
                            1,20,
                            param1=60,
                            param2=50,
                            minRadius=0,
                            maxRadius=60)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # 圓形的繪製
    cv2.circle(img,(i[0],i[1]),i[2],(255,0,255),2)
    # 圓心的繪製
    cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)

cv2.imshow('detected circles',img)

cv2.waitKey(0)


★齒輪辨識目的

找出齒輪的圓心位置,方便之後直行計算相關程式的條件。


★齒輪辨識結果



Comments

comments powered by Disqus