如何在Python OpenCV中简单裁剪边界框

11

我正在学习OpenCV,并通过测试一些用例来实现研究项目。我正在尝试使用Python OpenCV裁剪图像中的边界框,但是我成功创建了边界框,但在裁剪时失败了。

这是图像:

enter image description here

import cv2
import matplotlib.pyplot as plt
img = cv2.imread("Segmentacion/Img_183.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    
dst = cv2.Canny(gray, 0, 150)
blured = cv2.blur(dst, (5,5), 0)    
MIN_CONTOUR_AREA=200
img_thresh = cv2.adaptiveThreshold(blured, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
Contours,imgContours = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in Contours:
    if cv2.contourArea(contour) > MIN_CONTOUR_AREA:
        [X, Y, W, H] = cv2.boundingRect(contour)
        box=cv2.rectangle(img, (X, Y), (X + W, Y + H), (0,0,255), 2)

cropped_image = img[X:W, Y:H]
print([X,Y,W,H])
cv2.imwrite('contour.png', cropped_image )
1个回答

27

我找到了从原始图像裁剪边界框的公式。

cropped_image = img[Y:Y+H, X:X+W]
print([X,Y,W,H])
plt.imshow(cropped_image)
cv2.imwrite('contour1.png', cropped_image)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接