I use ImageBoundingBoxes
to get the bounding boxes of objects in an image. I then want to crop out each object and I tried directly passing the Rectangle objects from ImageBoundingBoxes
to ImageTake
, but it does not work. I also tried converting the rectangle to a list of points by doing
{rect[[1]],rect[[2]]}
and passing that to ImageTake
, but that also did not work, probably because ImageTake
expects row and col number, which start from the top left corner, whereas Rectangle
contains x and y which start from the bottom left corner. How can I convert Rectangle
to a form that I can pass to ImageTake
so that I can crop out the part of the image that the bounding box covers?
EDIT: I looked at ImageTrim and wrote the following function to to get a list of the four corners of a Rectangle object so that it can be passed to ImageTrim:
rectToList[rect_] := {rect[[1]], rect[[2]], {rect[[1, 1]], rect[[2, 2]]}, {rect[[2, 1]], rect[[1, 2]]}};
Then, I was able to crop out the bounding box portion as follows:
croppedImg = ImageTrim[img,rectToList[rect]];
Is this the best way to crop out the bounding box portion of an image?
ImageTrim
? $\endgroup$ImageTrim
can, as the documentation says, take a region of interest (or a list of regions). Consider the examples under "Application" for example. Say, theFindFaces
example.FindFaces
returnsRectangle
objects and yet it works. $\endgroup$