簡體   English   中英

使用霍夫圓變換從圖像中檢測圓

[英]detect circles from the image using Hough Circle transform

我正在嘗試使用 OpenCV 的 Hough Circles 函數從下圖中檢測圓圈

在此處輸入圖片說明

我的代碼(帶有 Python 的 OpenCV)

myImage = cv2.imread("C:\\sample.jpg") 
img = cv2.resize(myImage,(640,480))        
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

circles = cv2.HoughCircles(gray,cv2.cv.CV_HOUGH_GRADIENT,1,10, param1=50,param2=35,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',myImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

但由於某種原因,我無法獲得正確的輸出。 我得到以下輸出

在此處輸入圖片說明

更新

謝謝它現在正在工作。 通過將param2設置為高,我可以檢測到 2 個圓圈。 我錯誤地顯示它們,現在一切都很好

你好像給坐標錯了。

    # draw the outer circle
    cv2.circle(myImage,(i[1],i[0]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[1],i[0]),2,(0,0,255),3)

將其更改為

    # draw the outer circle
    cv2.circle(myImage,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(myImage,(i[0],i[1]),2,(0,0,255),3)

嗯,有一件事是最大半徑設置為 0 ...

即您的范圍是 0 < 半徑 < 0。

除非我弄錯了(?),這有點限制,是嗎?

您正在顯示原始圖像cv2.imshow('detected circles',myImage)但這些圓圈是在灰色 reescales 圖像上計算的。 改變

cv2.imshow('detected circles',myImage)

為了

cv2.imshow('detected circles',img)

你就完成了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM