How to crop a bounding box out of an image

2020-07-18 05:00发布

问题:

To explain the question a bit. I have an image that already contains a white bounding box as shown here: Input image

What I need is to crop the part of the image surrounded by the bounding box.

FindContours doesn't seem to work here so I tried something using the following code:

import cv2
import numpy as np

bounding_box_image = cv2.imread('PedestrianRectangles/1/grim.pgm')

edges = cv2.Canny(bounding_box_image, 50, 100)  # apertureSize=3

cv2.imshow('edge', edges)
cv2.waitKey(0)

lines = cv2.HoughLinesP(edges, rho=0.5, theta=1 * np.pi / 180, 
threshold=100, minLineLength=100, maxLineGap=50)

# print(len(lines))

for i in lines:
    for x1, y1, x2, y2 in i:
        # print(x1, y1, x2, y2)
        cv2.line(bounding_box_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv2.imwrite('houghlines5.jpg', bounding_box_image)

without any success. Playing around with the parameters didn't help too much either. The result of my code snippet is show on the following image: Output

I had the idea to do cropping after the line detection etc.

I am relatively new to opencv so help would be appreciated. Is there a good or easy approach to this problem that I'm missing? Googling around didn't help so any links, code snippets would be helpful.

回答1:

  1. Threshold to binary @ 250
  2. Find contours in the binary
  3. Filter the contour by height/width of boundingRect



回答2:

Thanks to Silencer, with his help I was able to make it work, so I'll provide the code and hope it helps other people:

import cv2
import numpy as np

bounding_box_image = cv2.imread('PedestrianRectangles/1/grim.pgm')
grayimage = cv2.cvtColor(bounding_box_image, cv2.COLOR_BGR2GRAY)

ret, mask = cv2.threshold(grayimage, 254, 255, cv2.THRESH_BINARY)

cv2.imshow('mask', mask)
cv2.waitKey(0)

image, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, 
cv2.CHAIN_APPROX_SIMPLE)

for contour in contours:

    if cv2.contourArea(contour) < 200:
        continue

    rect = cv2.minAreaRect(contour)
    box = cv2.boxPoints(rect)

    ext_left = tuple(contour[contour[:, :, 0].argmin()][0])
    ext_right = tuple(contour[contour[:, :, 0].argmax()][0])
    ext_top = tuple(contour[contour[:, :, 1].argmin()][0])
    ext_bot = tuple(contour[contour[:, :, 1].argmax()][0])

    roi_corners = np.array([box], dtype=np.int32)

    cv2.polylines(bounding_box_image, roi_corners, 1, (255, 0, 0), 3)
    cv2.imshow('image', bounding_box_image)
    cv2.waitKey(0)

    cropped_image = grayimage[ext_top[1]:ext_bot[1], ext_left[0]:ext_right[0]]
    cv2.imwrite('crop.jpg', cropped_image)

And the output