Ask Your Question
0

Cropping minAreaRect from image

asked 2016-03-31 16:46:07 -0500

manrajsingh gravatar image

updated 2016-04-01 01:14:00 -0500

I am trying to extract an object that is inclined/oriented at an angle in the image. I have drawn bounding box around it and able to crop area using command img[y:y+h,x:x+w] where x ,y, h and w are points, height and width . However, this is not possible with minAreaRect() as returned value has angle parameter. Is there a way or in built function to extract this rectangle? I'm able to draw that rectangle around it.

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-04-01 02:16:55 -0500

updated 2016-04-01 06:06:22 -0500

You first need to use the function cv::rotatedRect::boundingRect which returns you a rotatedRect object from which you can derive the angle as a float parameter!

UPDATE

Ow excuse me :) What you should do is rotate the image over the angle to obtain a rectangular bounding box and then crop it out. That would be the fastest way. Rotating an image around a point is illustrated here, which is a Python tutorial but the idea in C++ is exactly the same.

edit flag offensive delete link more

Comments

I believe what you want to say is get angle from the contour using minAreaRect. I'm already getting that. Third parameter here is angle. ((196.22874450683594, 149.16806030273438), (194.9783935546875, 59.012847900390625), -77.98100280761719) What I want to know is how to crop the area around this.

manrajsingh gravatar imagemanrajsingh ( 2016-04-01 03:07:36 -0500 )edit
1

Hey! :) Thanks for the pointer. I was able to extract it after some manipulations. Would love to have it as an inbuilt function as it did require finding height and width for cropping. Sadly boundingRect() didn't work for me in this case. Thank you once again. I guess you can update your answer so that I can mark it as correct and upvote it. :)

manrajsingh gravatar imagemanrajsingh ( 2016-04-01 05:01:54 -0500 )edit
1

Done ;) As to providing an inbuilt function to do so, you might be up to supplying your code or suggest a PR with this functionality?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-04-01 06:20:16 -0500 )edit
1

Sure, I'll try it out on some more real world cases and if I find it is really robust, I'll issue a PR. :D

manrajsingh gravatar imagemanrajsingh ( 2016-04-01 06:38:20 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-03-31 16:46:07 -0500

Seen: 2,541 times

Last updated: Apr 01 '16