問題描述
我正在嘗試使用 OpenCV 的 C++ API 將 1296x968 圖像90 度旋轉(zhuǎn),并且我遇到了一些問題.
I'm trying to rotate a 1296x968 image by 90 degrees using the C++ API of OpenCV and I'm facing a few problems.
輸入:
旋轉(zhuǎn):
如您所見,旋轉(zhuǎn)后的圖像存在一些問題.首先,它具有與原始大小相同的大小,即使我專門創(chuàng)建了目標(biāo) Mat
與原始大小相反的大小.結(jié)果,目標(biāo)圖像被裁剪.
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 Mat
with the inverted size of the original. As a result, the destination image gets cropped.
我懷疑這是因為我正在調(diào)用 warpAffine()
并傳遞原始 Mat
的大小而不是目標(biāo) Mat的大小代碼>.但我這樣做是因為我遵循了這個答案,但現(xiàn)在我懷疑答案可能是錯誤的.所以這是我的第一個疑問/問題.
I suspect this is happening because I'm calling warpAffine()
and passing the size of the original Mat
instead of the size of destination Mat
. But I'm doing this because I followed this answer, but now I suspect that the answer may be wrong. So this is my first doubt/problem.
第二個,是 warpAffine()
正在在某個偏移量處寫入目標(biāo)(可能是將旋轉(zhuǎn)后的數(shù)據(jù)復(fù)制到圖像的中間)和這個操作會在圖像周圍留下可怕的大黑色邊框.
The second, is that warpAffine()
is writing to the destination at a certain offset (probably to copy the rotated data to the middle of the image) and this operation leaves a horrible and large black border around the image.
我該如何解決這些問題?
我在下面分享源代碼:
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
void rotate(Mat& 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 << "!!! Couldn't load " << argv[1] << endl;
return -1;
}
rotate(orig_image, 90);
return 0;
}
推薦答案
我找到了一個解決方案,它不涉及warpAffine()
.
I've found a solution that doesn't involve warpAffine()
.
但在此之前,我需要聲明(以供將來參考)我的懷疑是正確的,您需要在調(diào)用 warpAffine()
:
But before that, I need to state (for future references) that my suspicion was right, you needed to pass the size of the destination when calling warpAffine()
:
warpAffine(image, rotated_img, rot_matrix, rotated_img.size());
據(jù)我所知,此函數(shù)繪制的黑色邊框(由在偏移處寫入引起)似乎是標(biāo)準(zhǔn)行為.我已經(jīng)注意到 C 接口以及在 Mac 和 Linux 上運行的 OpenCV 的 C++ 接口,使用版本 2.3.1a 和 2.3.0.
As far as I can tell, the black border (caused by writing at an offset) drawed by this function seems to be it's standard behavior. I've noticed this with the C interface and also with the C++ interface of OpenCV running on Mac and Linux, using the versions 2.3.1a and 2.3.0.
我最終使用的解決方案比所有這些扭曲簡單得多.您可以使用 cv::transpose()
和 cv::flip()
將圖像旋轉(zhuǎn) 90 度.這是:
The solution I ended up using is much simpler than all this warp thing. You can use cv::transpose()
and cv::flip()
to rotate an image by 90 degrees. Here it is:
Mat src = imread(argv[1], 1);
cv::Mat dst;
cv::transpose(src, dst);
cv::flip(dst, dst, 1);
imwrite("rotated90.jpg", dst);
----I>
這篇關(guān)于使用 cv::warpAffine 偏移量目標(biāo)圖像旋轉(zhuǎn) cv::Mat的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!