Summary record--(OpenCV take arbitrary position rectangle correction algorithm)

Source: Internet
Author: User
How to copy a diagonal rectangle from an image.
The general idea is as follows:
1. Create a single-channel matrix mask with the same size as the source image src, and cvzero (mask);
2. Use Cvpolyline () to connect a known 4 corner point to a four-sided shape, painted on the mask;
3, with Cvfillconvexpoly or cvfillpoly the mask on the quad coloring;
4. Use Cvcopy (src, DST, mask) to copy the quad region to DST.

After cvcopy, you can use Cvresize to convert the image to a smaller image, specifically:

1-4 ... ;
5, to find a known corner of the horizontal/vertical minimum rectangle minrect (can be used cvboundingrect, can also be programmed to obtain minrect);
6, create a picture with the same size minrect scaled_dst;
7. Set the ROI of the DST image obtained by Cvcopy to the location of Minrect: Cvsetimageroi (DST, minrect);
8. Get the small image you need: Cvresize (DST, SCALED_DST)

This process replicates the rectangle, but the rectangle is still oblique in the new SCALED_DST, or it can be said that he has copied the smallest rectangle that surrounds the diagonal rectangle, but the diagonal rectangle is still not coming. It would be nice to get the smallest rectangle enclosing the diagonal rectangle on the original image with Cvsetimageroi ().

Cvsetimageroi (), can be set to a diagonal rectangle. It seems that its argument must be a positive rectangle.
I was trying to cuff this diagonal rectangle. Nothing else.

That can be achieved with the OpenCV affine transform function, specifically:

1. Create an image with the same size as the inclined rectangle DST;
2, the angular point coordinate of the inclined rectangle is Srt_pns converted to the corner coordinate Dst_pns of the vertical rectangle, and the cvgetaffinetransform is used to obtain the rotation matrix Map_matrix;
3, using the Cvwarpaffine function to rotate the original image srt, so that the rectangular area of interest from tilt to vertical (or horizontal);
4. Use Cvsetimageroi and cvresize to copy the area of interest to DST.

Yes, this method should be feasible, my original rotation is a bit troublesome, I am currently using Cvgetquadranglesubpix () to achieve rotation, but it has a drawback is that it must be the center of rotation in the center of the original image (otherwise rotated coordinates do not know how to correspond), so my approach is actually:
1. Calculates the upper-left point coordinate of the diagonal rectangle in the original image.
2. Use Cvgetrectsubpix () to copy a sub-image dst1 that includes the original oblique rectangle, centered on the top left corner coordinates.
3. Use Cvgetquadranglesubpix () to rotate the upper Dst1 and copy a new sub-figure dst2 (consistent with the size of the sub-image), the angle can be calculated from the upper left corner point and the original right corner point coordinates.
4. Use Cvgetrectsubpix () to copy the sub-image rotated by the oblique rectangle, at which point the upper left corner of the original oblique rectangle is at the center of the Dst2 and has been transferred to the horizontal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.