Home Backend Development C++ How to Deskew Points Using Perspective Transformation and Affine Transforms?

How to Deskew Points Using Perspective Transformation and Affine Transforms?

Nov 21, 2024 am 01:15 AM

How to Deskew Points Using Perspective Transformation and Affine Transforms?

Perspective Transformation for Deskewing

To achieve a deskewing effect on a set of points using perspective transformation, it's essential to understand the following:

Point Ordering:

The order of points matters in perspective transformation. To ensure accuracy, the order must be consistent in both the source and destination vectors.

Image Size:

If you want the resulting image to only contain the object of interest, set its width and height to match the resulting rectangle's dimensions.

Performance Considerations:

For affine transformations like rotate, resize, and deskew, it's more efficient to use the affine counterparts:

  • getAffineTransform()
  • warpAffine()

Affine Transform:

getAffineTransform() only requires three points and provides a 2x3 matrix, while warpAffine() performs the warping.

Resizing the Resulting Image:

To resize the resulting image to a different size than the input, use:

cv::Size size(box.boundingRect().width, box.boundingRect().height);
cv::warpPerspective(src, dst, size, ... );
Copy after login

Example:

vector<Point> not_a_rect_shape;
not_a_rect_shape.push_back(Point(408, 69));
not_a_rect_shape.push_back(Point(72, 2186));
not_a_rect_shape.push_back(Point(1584, 2426));
not_a_rect_shape.push_back(Point(1912, 291));

RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape));

Point2f pts[4];
box.points(pts);

Point2f src_vertices[3];
src_vertices[0] = pts[0];
src_vertices[1] = pts[1];
src_vertices[2] = pts[3];

Point2f dst_vertices[3];
dst_vertices[0] = Point(0, 0);
dst_vertices[1] = Point(box.boundingRect().width-1, 0);
dst_vertices[2] = Point(0, box.boundingRect().height-1);

Mat warpAffineMatrix = getAffineTransform(src_vertices, dst_vertices);

cv::Size size(box.boundingRect().width, box.boundingRect().height);
warpAffine(src, rotated, warpAffineMatrix, size, INTER_LINEAR, BORDER_CONSTANT);
Copy after login

The above is the detailed content of How to Deskew Points Using Perspective Transformation and Affine Transforms?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

See all articles