为什么这个旋转方法会给图像死空间? OpenCV的 [英] Why does this rotate method give the image dead space? OpenCV

查看:140
本文介绍了为什么这个旋转方法会给图像死空间? OpenCV的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此方法旋转 cvMat ,每当我运行它时,我都会返回一个旋转的图像,但是下面有很多死区。

I am using this method to rotate a cvMat, whenever I run it I get back a rotated image however there is a lot of deadspace below it.

void rotate(cv::Mat& src, double angle, cv::Mat& dst)
{
    int len = std::max(src.cols, src.rows);
    cv::Point2f pt(len/2., len/2.);
    cv::Mat r = cv::getRotationMatrix2D(pt, angle, 1.0);

    cv::warpAffine(src, dst, r, cv::Size(len, len));

}

给出此图片时:

我得到这张图片:

图像已经旋转但是你可以看到添加了一些额外的像素,我怎么只能旋转原始图像而不添加任何额外的像素?

The image has been rotated but as you can see some extra pixels have been added, how can I only rotate the original image and not add any extra pixels?

方法调用:

rotate(src,skew,res);
res dst

推荐答案

由于 mayank-baddi 表示您必须使用与输入相同的输出图像大小来解决此问题,我的回答基于您的上述评论如何避免在wrapAffine之后添加黑色区域?

As mayank-baddi said you have to use output image size same as the input to resolve this, and my answer is based on your comment above How can I avoid adding the black area? after wrapAffine,

所以你必须这样做,


  • 创建比你的更大的白色图像rce,它将取决于你的倾斜角度,这里我使用50像素。

  • Create white image little bigger than your source, and it will depend on your skew angle, here I used 50 pixel.

int extend=50;
Mat tmp(src.rows+2*extend,src.cols+2*extend,src.type(),Scalar::all(255));


  • 使用投资回报率将来源复制到上面

  • Copy the source to above using ROI

    Rect ROI(extend,extend,src.cols,src.rows);
    src.copyTo(tmp(ROI));
    


  • 现在轮换 tmp 而不是 src

    rotate(tmp, skew, res); res being dst.
    


  • 使用相同的投资回报率从旋转结果中裁剪最终图像。

  • Crop back the final image from rotated result using the same ROI.

     Mat crop=res(ROI);  
     imshow("crop",crop);
    


  • 这篇关于为什么这个旋转方法会给图像死空间? OpenCV的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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