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

I have this image

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.

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

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:

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

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:

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 reopen merge delete