处理由 opencv boxPoints 或轮廓包围的子图像?

process subimages enclosed by opencv boxPoints or contours in-place?

在 python 中使用 opencv3,我使用下面的代码得到了一个包含由 boxPoints 限定的不同片段的图像:

(_, conts, _) = cv2.findContours(img, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_SIMPLE)

boxes = []
# loop over contours
for c in conts:    
    # get min bounding rect.
    min_rect_center_xy = min_rect[0]
    min_rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(min_rect)
    box = np.int0(box)

    cv2.drawContours(img_segmented_boxed, contours=[box], contourIdx=0, color=(0,0,255), thickness=10)     
    boxes.append(box)

所以我现在有一个数组,框,包含所有轮廓感兴趣区域的框点。

我现在想做的是能够对每个框限定的不同 ROI 执行操作(例如,模糊框内包含的图像中的所有 sections/subimages)。谁能举例说明如何做到这一点? (能够对不同的子图像执行不同的操作将是一个加号)。

如果无法就地操作这些子图像,我如何将子图像分成单独的文件,对它们执行操作并将它们放回原始图像的正确位置?

谢谢:)

您可以裁剪这些 ROI:

# after boxes.append(box)
for box in boxes:
        roi = cv2.getRectSubPix(image, int(box[1][0]), int(box[1][1]), box[0])
        #apply whatever operation on each ROIs for example a Gaussian Blur:
        roi = cv2.GaussianBlur(roi,(3,3),0)

希望对您有所帮助。