OpenCV Mat旋转得到错误的结果 [英] OpenCV Mat rotation gets wrong result
问题描述
我想将图片旋转90度。我的代码如下:
int main(int argc,const char * argv []){
Mat img = imread(/ Users / chuanliu / Desktop / src4 / p00.JPG);
resize(img,img,Size(1024,683));
imwrite(/ Users / chuanliu / Desktop / resize.jpg,img);
Mat dst;
Mat rot_mat = getRotationMatrix2D(Point(img.cols / 2.0,img.rows / 2.0),90,1);
warpAffine(img,dst,rot_mat,Size(img.rows,img.cols));
imwrite(/ Users / chuanliu / Desktop / roatation.jpg,dst);
return 0;
}
但结果如下所示:
:
>
轮播后:
看起来旋转中心sth错了。但我不认为我设置错误的中心。是否有任何人可以告诉我有什么错误?
中心是根据源图像的尺寸 Point(img.cols / 2.0,img.rows / 2.0)
但是你不仅仅是旋转图像,而是在调用 warpAffine时交换输出大小的宽度和高度
:
大小(img.rows,img.cols)
,因此您可能需要根据输出图像坐标指定中心;例如。 点(rows / 2,cols / 2)
。
更新: p>
不,不是解决方案。实际上有一个非常简单和有效的方法旋转图像90度:使用 cv :: transpose()
函数:
int main()
{
cv :: Mat img = cv :: imread(5syfi.jpg);
cv :: Mat img_rotated;
cv :: transpose(img,img_rotated);
cv :: imwrite(out.jpg,img_rotated);
return 0;
}
使用 cv :: transpose
(旋转)和 cv :: flip()
(垂直和水平镜像),您可以非常快速地执行旋转90,180和270
使用 warpAffine()
更灵活,但也更昂贵) 计算。所以如果你只需要旋转90deg的倍数,使用 cv :: transpose
。如果需要旋转任意角度,请使用 warpAffine / warpPerspective
函数。 @ Micka的回答给出了一个很好的例子。
I want to rotate an image by 90 degrees. My code is like following:
int main(int argc, const char * argv[]) {
Mat img = imread("/Users/chuanliu/Desktop/src4/p00.JPG");
resize(img, img, Size(1024, 683));
imwrite("/Users/chuanliu/Desktop/resize.jpg", img);
Mat dst;
Mat rot_mat = getRotationMatrix2D(Point(img.cols / 2.0, img.rows / 2.0), 90, 1);
warpAffine(img, dst, rot_mat, Size(img.rows, img.cols));
imwrite("/Users/chuanliu/Desktop/roatation.jpg", dst);
return 0;
}
But the result is like following:
Before rotation:
After rotation:
It seems that the center of rotation has sth wrong. But I don't think I set a wrong center. Is there anyone can tell me what is wrong?
The centre is specified in terms of the source image's dimensions Point(img.cols / 2.0, img.rows / 2.0)
but you are not only rotating the image but swapping the width and height in the output size when calling warpAffine
:
Size(img.rows, img.cols)
so it looks like you might need to specify the centre in terms of the output image coordinates; eg. Point(rows/2, cols/2)
.
Update:
Nope, that's not the solution. There is actually a very simple and efficient method for rotating an image by 90 degrees: using the cv::transpose()
function:
int main()
{
cv::Mat img = cv::imread("5syfi.jpg");
cv::Mat img_rotated;
cv::transpose(img, img_rotated);
cv::imwrite("out.jpg", img_rotated);
return 0;
}
Using a combination of cv::transpose()
(to rotate) and cv::flip()
(to mirror vertically and horizontally) you can very quickly perform rotations by 90, 180 and 270 degrees.
Using warpAffine()
is much more flexible, but it is also much more expensive (ie. slower) to calculate. So if you only need to rotate by a multiple of 90deg, use cv::transpose
. If you need to rotate by an arbitrary angle, use the warpAffine/warpPerspective
functions. @Micka's answer gives a great example of how to do that.
这篇关于OpenCV Mat旋转得到错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!