Note that there are some explanatory texts on larger screens.

plurals
  1. PORotate cv::Mat using cv::warpAffine offsets destination image
    primarykey
    data
    text
    <p>I'm trying to <strong>rotate</strong> a <strong>1296x968</strong> image by <strong>90 degrees</strong> using the <strong>C++ API</strong> of OpenCV and I'm facing a few problems.</p> <p><strong>Input</strong>: <img src="https://i.stack.imgur.com/Hyel9.jpg" alt="input"></p> <p><strong>Rotated</strong>: <img src="https://i.stack.imgur.com/NDSFw.jpg" alt="output"></p> <p>As you can see, the rotated image has a few problems. First, it has the same size of the original, even though I specifically create the destination <code>Mat</code> with the inverted size of the original. As a result, the destination image gets cropped. </p> <p>I suspect this is happening because I'm calling <code>warpAffine()</code> and passing the size of the original <code>Mat</code> instead of the size of destination <code>Mat</code>. But I'm doing this because <strong>I followed <a href="https://stackoverflow.com/questions/2289690/opencv-how-to-rotate-iplimage/2300459#2300459">this answer</a></strong>, but now I suspect that the answer may be wrong. So this is my first doubt/problem.</p> <p>The second, is that <code>warpAffine()</code> is <strong>writing to the destination at a certain offset</strong> (probably to copy the rotated data to the middle of the image) and this operation leaves a horrible and <strong>large black border</strong> around the image. </p> <p><strong>How do I fix these issues?</strong></p> <p>I'm sharing the source code below:</p> <pre><code>#include &lt;cv.h&gt; #include &lt;highgui.h&gt; #include &lt;iostream&gt; using namespace cv; using namespace std; void rotate(Mat&amp; image, double angle) { Point2f src_center(image.cols/2.0F, image.rows/2.0F); Mat rot_matrix = getRotationMatrix2D(src_center, angle, 1.0); Mat rotated_img(Size(image.size().height, image.size().width), image.type()); warpAffine(image, rotated_img, rot_matrix, image.size()); imwrite("rotated.jpg", rotated_img); } int main(int argc, char* argv[]) { Mat orig_image = imread(argv[1], 1); if (orig_image.empty()) { cout &lt;&lt; "!!! Couldn't load " &lt;&lt; argv[1] &lt;&lt; endl; return -1; } rotate(orig_image, 90); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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