Draw square that covers area of pixel cluster in binary image (Python) [closed]

asked 2018-02-05 20:46:01 -0500

I have this image

image description

I'd like to crop to the orange content so that I end up with this: (Note that the forum's own code is zooming in the image to fit the frame but I don't in fact need it zoomed in. Just cropped to the orange content.

image description

The approach I'm taking is finding the follow by range and I end up with a binary mask of just the orange section. So after doing cv2.inRange I send up with this mask

image description

From there I trying to find the contours and then the convex hull to reduce the number of vertices to just 4 (the orange content has at least 8), but this is where I'm running into problems. Essentially I'd like to end up with a binary mask of a square that covers as far as the orange content reaches. It should look like this:

image description

So then with that mask I can do a bitwise and on the original image so that I end up with this:

image description

Then finally I'll crop out the black regions as much as I can (I think I have an idea of how I can do that), so that I can end up with the desired image:

image description

I'm stuck at creating the square that cover the entire area of the orange. So far I'm able to threshold and create the bitwise mask with the shape of the orange region. I can find the contours of it, but them simplifying the vertices and drawing a square is the hard part.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by cgranados
close date 2018-02-06 08:39:56.135192

Comments

1

After finding the Binary mask find Minimum bounding rectangle to find your desired Rect area. Please see this answer!

Balaji R gravatar imageBalaji R ( 2018-02-06 02:16:22 -0500 )edit

wow should have kept reading the manual after convex hull lol thank you

cgranados gravatar imagecgranados ( 2018-02-06 08:39:40 -0500 )edit