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

Question:

Using opencv3 in python, I have an image with different segments bounded by boxPoints using the code below:

(_, 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)

So I now have an array, boxes, containing the boxpoints of all of the regions of interest that were contoured.

What I would like to do now is be able to perform operations on the different ROIs bounded by each box (eg. blur all sections/subimages in the image contained within the boxes). Could anyone give an example of how this would be done? (Being able to perform different operations on the different subimages would be a plus).

If it is not possible to manipulate these subimages in-place, how could I separate the subimages into seperate files, perform operations on them and put them back into the original image in the correct positions?

Thanks 🙂

Asked By: lampShadesDrifter

||

Answers:

What you can do is crop those ROIs:

# 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)

I hope this helps.

Answered By: sixela