旋转cv :: Mat使用cv :: warpAffine偏移目标图像 [英] Rotate cv::Mat using cv::warpAffine offsets destination image

查看:582
本文介绍了旋转cv :: Mat使用cv :: warpAffine偏移目标图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenCV的 C ++ API 旋转 1296x968 图片, 90度我遇到了一些问题。

I'm trying to rotate a 1296x968 image by 90 degrees using the C++ API of OpenCV and I'm facing a few problems.

输入

旋转

如您所见,旋转后的图片有几个问题。首先,它具有与原始大小相同的大小,即使我专门创建了目标 Mat 与原始的倒置大小。因此,目标图像被裁剪。

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.

我怀疑这是因为我调用 warpAffine(),并传递原始的大小 Mat 而不是目标的大小 Mat 。但我这样做是因为我遵循此答案 ,但现在我怀疑答案可能是错误的。所以这是我的第一个疑问/问题。

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()到某个偏移的目的地(可能要将旋转的数据复制到图像的中间),此操作会在图像周围留下一个可怕的和大的黑色边框

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().

但在此之前,我需要说明(以后参考)我的怀疑是正确的,你需要通过调用 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());

据我所知,黑色边框功能似乎是它的标准行为。我注意到这与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.

解决方案我最终使用比所有这些 warp事情更简单。您可以使用 cv :: transpose() cv :: flip() 将图像旋转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>

这篇关于旋转cv :: Mat使用cv :: warpAffine偏移目标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆