2017-05-08 9 views
0

나는이 자른 이미지에서 houghcircles()를 사용하여 작은 원을 탐지하려고합니다. enter image description herehoughCircles (OpenCV)를 사용하여 작은 원을 감지했습니다.

그리고 : 나는 그것의 매개 변수를 변경하려하지만 난 50 위 PARAM2을 증가시키고 그 값이 100보다 작은 경우 maxRadius 또한 오류를 얻을 지금은하지만 나쁜 성능을 실행하면이 오류를 얻을 수 이 원본 이미지 이 결과 이미지 : enter image description here

그리고 이것은 내 코드입니다 :

from imutils.perspective import four_point_transform 
from imutils import contours 
import numpy as np 
import argparse 
import imutils 
import cv2 

im = cv2.imread('crop.png') 
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(imgray, 200, 255,cv2.THRESH_BINARY) 
cimg = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR) 

c = cv2.HoughCircles(thresh, cv2.HOUGH_GRADIENT, 0.5, 41, param1=70, 
param2=30, minRadius=10,maxRadius=175) 
c = np.uint16(np.around(c)) 

for i in c[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,0,255),3) 

cv2.namedWindow('img',cv2.WINDOW_NORMAL) 
cv2.resizeWindow('img', 800,800) 
cv2.imshow('img',cimg) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

, 내가 어떻게이 매개 변수를 변경해야하세요?

답변

2

매개 변수가 무엇인지 이해하지 않고 매개 변수를 변경하려고하면이 문제를 해결하는 데 오랜 시간이 걸립니다.

이 설명은 당신이 명확하게 이미지의 원 반경을 고정하고 분리 분 거리를 가지고있는 것을 볼 수 있습니다 here

minDist: Minimum distance between the center (x, y) coordinates of detected circles. If the minDist is too small, multiple circles in the same neighborhood as the original may be (falsely) detected. If the minDist is too large, then some circles may not be detected at all.

param1: Gradient value used to handle edge detection in the Yuen et al. method.

param2: Accumulator threshold value for the cv2.HOUGH_GRADIENT method. The smaller the threshold is, the more circles will be detected (including false circles). The larger the threshold is, the more circles will potentially be returned.

minRadius: Minimum size of the radius (in pixels).

maxRadius: Maximum size of the radius (in pixels).

에서입니다. 이 두 가지를 설정하면 결과를 향상시킬 수 있습니다. 따라서 문서를 읽는 것이 중요합니다.

그리고 당신의 문제에 대해, Houghcircles를 사용해야 할 특별한 필요성이있는 경우 계속 진행하고 미세 조정하십시오. 개선하기 위해 할 수있는 일은 gaussianblur를 사용한 전처리이며 임계 값 대신 adaptivethreshold를 사용합니다.

호프 서클을 사용할 필요가없는 경우 윤곽선을 사용하는 것이 좋습니다. 왜냐하면 그것은 더 견고하고 다른 이미지들에 잘 적용되기 때문입니다. 윤곽선을 사용하여 얻은 결과는 다음과 같습니다. 내가 6 번의 반복에 대해 침식을 사용하고 3 번의 반복에 대해서만 확장을 사용했기 때문에 원이 더 작게 나타납니다. 여기

finding circles using contours

은 내가 사용하는 코드입니다.

import numpy as np 
import cv2 

image_color= cv2.imread("Ye3gs.png") 
image_ori = cv2.cvtColor(image_color,cv2.COLOR_BGR2GRAY) 

lower_bound = np.array([0,0,10]) 
upper_bound = np.array([255,255,195]) 

image = image_color 

mask = cv2.inRange(image_color, lower_bound, upper_bound) 

# mask = cv2.adaptiveThreshold(image_ori,255,cv2.ADAPTIVE_THRESH_MEAN_C,\ 
#    cv2.THRESH_BINARY_INV,33,2) 

kernel = np.ones((3, 3), np.uint8) 

#Use erosion and dilation combination to eliminate false positives. 
#In this case the text Q0X could be identified as circles but it is not. 
mask = cv2.erode(mask, kernel, iterations=6) 
mask = cv2.dilate(mask, kernel, iterations=3) 

closing = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) 

contours = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 
     cv2.CHAIN_APPROX_SIMPLE)[0] 
contours.sort(key=lambda x:cv2.boundingRect(x)[0]) 

array = [] 
ii = 1 
print len(contours) 
for c in contours: 
    (x,y),r = cv2.minEnclosingCircle(c) 
    center = (int(x),int(y)) 
    r = int(r) 
    if r >= 6 and r<=10: 
     cv2.circle(image,center,r,(0,255,0),2) 
     array.append(center) 

cv2.imshow("preprocessed", image_color) 
cv2.waitKey(0) 

희망이

+0

이 도움을 주셔서 감사합니다 :) 도움이 있지만, 그것은 나에게 오류 제공 :'contours.sort (키 = 람다 X : cv2.boundingRect (X) [0]) 형식 오류를 ' = 10 출처 = cv2.boundingRect (윤곽) 반환 tolerance_factor ((: 키 '내가이 기능'데프 get_contour_precedence (윤곽, COLS)를 추가 그것은 나를 위해 일한 ' –

+1

감사 작정이 기능에 대해 잘못된 키워드 인수는 원점 [1] // tolerance_factor) * tolerance_factor) * cols + origin [0]' –

+0

등고선 + minEnclosingC를 사용하는 것이 좋습니다. ircle 함수를 선택하고 원 경계/면적을 검출 된 윤곽과 비교하여 비원 형상을 필터링합니다 – Micka