2015-07-20 315 views
1

下面是代碼:Python + OpenCV =如何裁剪圓?

self.img = cv2.imread(image,) 
circle = cv2.HoughCircles(self.img, 3, 
          dp=1.5, minDist=1000, minRadius=100, maxRadius=1000) 
red = (0,0,255) 
x = circle[0][0][0] 
y = circle[0][0][1] 
r = circle[0][0][2] 
cv2.circle(self.img, (x, y), r, red, 2) 



    x - X 
    y - Y 
    r - Radius 
    For example: 521.25, 506.25, 318.919 

從代碼如何裁剪從給定的例子圓?

+0

您使用的是什麼版本的OpenCV? – galath

+0

OpenCV版本:3.0.0 –

+0

如果你的圖像是一個矩形網格,那麼你使用像'cropped = image [30:120,240:335]這樣的線裁剪它,其中數字是定義矩形的NumPy數組切片圖像的區域從(240,30)開始並在(335,120)結束。如果你想要一個特定於你的問題的答案,請按照[問]重新編寫。 – boardrider

回答

1

它的簡單..你需要得到矩形的右上角的x,y座標,並找到與高度。圈子只能以正方形包圍。

# given x,y are circle center and r is radius 
rectX = (x - r) 
rectY = (y - r) 
crop_img = self.img[y:(y+2*r), x:(x+2*r)] 
0

後圈= cv2.HoughCircles(IMG,...)

if len(circles) == 1: 
    x, y, r = circles[0][0] 
    print x, y, r 
    mask = np.zeros((w0,h0),dtype=np.uint8) 
    cv2.circle(mask,(x,y),r,(255,255,255),-1,8,0) 
    #cv2.imwrite(argv[2],mask) 
    out = img*mask 
    white = 255-mask 
    cv2.imwrite(argv[2],out+white)