2017-03-07 36 views
0

這是我的第一個Python項目。無奈地與openCV和HoughCircles失去了聯繫,Python

我在試圖檢測這個黑圈here。不應該太困難,但由於某些原因,我只能得到0個圓或大約500個圓,這取決於參數。但沒有中間立場。感覺就像我試圖在幾個小時的爭論中一起玩,但絕對沒有成功。使用HoughCircles和黑色或白色圖片有問題嗎?這個任務對於人類來說似乎很簡單,但出於某種原因,這對計算機來說很難嗎?

這裏是我當前的代碼:

import numpy as np 
import cv2 

image = cv2.imread('temp.png') 
output = image.copy() 
blurred = cv2.blur(image,(10,10)) 

gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) 


circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 20, 100, 600, 10, 100) 


if circles is not None: 

    circles = np.round(circles[0, :]).astype("int") 
     print len(circles) 

    for (x, y, r) in circles: 
     cv2.circle(output, (x, y), r, (0, 255, 0), 4) 
     cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1) 

    show the output image 
cv2.imshow("output", np.hstack([output])) 
cv2.waitKey(0) 

謝謝。 第一條評論後編輯。

+0

您正在嘗試繪製'gray'圖像上的圓圈。嘗試在原始彩色圖像上繪製它。 –

+0

此外,您正在顯示'output',它是輸入圖像的副本.... –

+0

對不起,我在故障排除期間更改了代碼,以查看更改如何影響處理後的圖像,並設法在此處複製不完整的代碼。 仍然打印len(圈子)從來沒有1. – OlliJJJ

回答

1

你的方法有幾個小錯誤。

下面是我從文檔中使用的代碼:

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

circles = cv2.HoughCircles img,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,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),2) 
    # draw the center of the circle 
    cv2.circle(cimg,(i[0],i[1]),2,(0,255,255),3) 

cv2.imshow('detected circles.jpg',cimg) 

enter image description here

joint = np.hstack([cimg1, cimg]) #---Posting the original image along with the image having the detected circle 
cv2.imshow('detected circle and output', joint) 

enter image description here

+0

非常感謝你,它的工作原理!我早些時候嘗試過這些設置,但似乎這一行:img = cv2.medianBlur(img,5) 是決定性的。沒有它,結果與我的相似。很高興知道爲什麼需要中距離擊球。 – OlliJJJ

+0

我希望你能理解統計中位數。這裏也適用同樣的原則。 –

+0

也請嘗試文檔中給出的不同模糊技術。將他們全部應用到一個圖像,你會注意到不同之處 –