Python + OpenCV =如何裁剪圆圈?

时间:2015-07-20 14:31:48

标签: python opencv numpy crop geometry

以下是代码:

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

从代码中如何裁剪给定示例中的圆圈?

2 个答案:

答案 0 :(得分:2)

它很简单..你需要获得矩形右上角的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)]

答案 1 :(得分:0)

在circle = 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)