Note that there are some explanatory texts on larger screens.

plurals
  1. POExecuting cv::warpPerspective for a fake deskewing on a set of cv::Point
    text
    copied!<p>I'm trying to do a <a href="http://nuigroup.com/forums/viewthread/3414/">perspective transformation</a> of a set of points in order to achieve a <a href="http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/">deskewing</a> effect:</p> <p><a href="http://nuigroup.com/?ACT=28&amp;fid=27&amp;aid=1892_H6eNAaign4Mrnn30Au8d">http://nuigroup.com/?ACT=28&amp;fid=27&amp;aid=1892_H6eNAaign4Mrnn30Au8d</a></p> <p>I'm using the image below for tests, and the <strong>green</strong> rectangle display the area of interest.</p> <p><img src="https://i.stack.imgur.com/r8fmh.jpg" width="300" height="350"></p> <p>I was wondering if it's possible to achieve the effect I'm hoping for using a simple combination of <code>cv::getPerspectiveTransform</code> and <code>cv::warpPerspective</code>. I'm sharing the source code I've written so far, but it doesn't work. This is the resulting image:</p> <p><img src="https://i.stack.imgur.com/206aG.jpg" width="100" height="130"></p> <p>So there is a <code>vector&lt;cv::Point&gt;</code> that <strong>defines the region of interest</strong>, but the points are <strong>not stored in any particular order</strong> inside the vector, and that's something I can't change in the detection procedure. Anyway, <strong>later</strong>, the points in the vector are used to define a <code>RotatedRect</code>, which in turn is used to assemble <code>cv::Point2f src_vertices[4];</code>, one of the variables required by <code>cv::getPerspectiveTransform()</code>.</p> <p>My understanding about <strong>vertices</strong> and how they are organized <strong>might be one of the issues</strong>. I also think that using a <strong><code>RotatedRect</code> is not the best idea</strong> to store the original points of the ROI, since the <strong>coordinates will change</strong> a little bit to fit into the rotated rectangle, and <strong>that's not very cool</strong>.</p> <pre><code>#include &lt;cv.h&gt; #include &lt;highgui.h&gt; #include &lt;iostream&gt; using namespace std; using namespace cv; int main(int argc, char* argv[]) { cv::Mat src = cv::imread(argv[1], 1); // After some magical procedure, these are points detect that represent // the corners of the paper in the picture: // [408, 69] [72, 2186] [1584, 2426] [1912, 291] vector&lt;Point&gt; 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)); // For debugging purposes, draw green lines connecting those points // and save it on disk const Point* point = &amp;not_a_rect_shape[0]; int n = (int)not_a_rect_shape.size(); Mat draw = src.clone(); polylines(draw, &amp;point, &amp;n, 1, true, Scalar(0, 255, 0), 3, CV_AA); imwrite("draw.jpg", draw); // Assemble a rotated rectangle out of that info RotatedRect box = minAreaRect(cv::Mat(not_a_rect_shape)); std::cout &lt;&lt; "Rotated box set to (" &lt;&lt; box.boundingRect().x &lt;&lt; "," &lt;&lt; box.boundingRect().y &lt;&lt; ") " &lt;&lt; box.size.width &lt;&lt; "x" &lt;&lt; box.size.height &lt;&lt; std::endl; // Does the order of the points matter? I assume they do NOT. // But if it does, is there an easy way to identify and order // them as topLeft, topRight, bottomRight, bottomLeft? cv::Point2f src_vertices[4]; src_vertices[0] = not_a_rect_shape[0]; src_vertices[1] = not_a_rect_shape[1]; src_vertices[2] = not_a_rect_shape[2]; src_vertices[3] = not_a_rect_shape[3]; Point2f dst_vertices[4]; dst_vertices[0] = Point(0, 0); dst_vertices[1] = Point(0, box.boundingRect().width-1); dst_vertices[2] = Point(0, box.boundingRect().height-1); dst_vertices[3] = Point(box.boundingRect().width-1, box.boundingRect().height-1); Mat warpMatrix = getPerspectiveTransform(src_vertices, dst_vertices); cv::Mat rotated; warpPerspective(src, rotated, warpMatrix, rotated.size(), INTER_LINEAR, BORDER_CONSTANT); imwrite("rotated.jpg", rotated); return 0; } </code></pre> <p>Can someone help me fix this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload