广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    OPENCV入门教程九:图像旋转任意角度

    来源:网络收集  点击:  时间:2024-04-25
    【导读】:
    介绍如何学习旋转图片。工具/原料morevisual studio 2015opencv2.4.13方法/步骤1/5分步阅读

    在OpenCV中,没有现成的函数直接用来实现图像旋转,它是用仿射变换函数warpAffine来实现的,此函数目前支持4种插值算法,最近邻、双线性、双三次、兰索斯插值。函数原型:

    void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar borderValue=Scalar())

    2/5

    打开VS2015选择文件,然后新建项目,选择新建一个Win32控制台应用程序,并选择空项目并在源文件中添加一个名为rotate的CPP文件

    3/5

    在该rotate.cpp文件中输入一下代码

    #include cv.h // OpenCV 文件头

    #include highgui.h

    #include cvaux.h

    #include cxcore.h

    #include opencv2/opencv.hpp

    #include opencv2/imgproc.hpp

    #include iostream

    #include string

    using namespace cv;

    using namespace std;

    Mat rotateImage1(Mat img, int degree)

    {

    degree = -degree;

    double angle = degree * CV_PI / 180.; // 弧度

    double a = sin(angle), b = cos(angle);

    int width = img.cols;

    int height = img.rows;

    int width_rotate = int(height * fabs(a) + width * fabs(b));

    int height_rotate = int(width * fabs(a) + height * fabs(b));

    //旋转数组map

    // ===

    // ===

    float map;

    Mat map_matrix = Mat(2, 3, CV_32F, map);

    // 旋转中心

    CvPoint2D32f center = cvPoint2D32f(width / 2, height / 2);

    CvMat map_matrix2 = map_matrix;

    cv2DRotationMatrix(center, degree, 1.0, map_matrix2);

    map += (width_rotate - width) / 2;

    map += (height_rotate - height) / 2;

    Mat img_rotate;

    //对图像做仿射变换

    //CV_WARP_FILL_OUTLIERS - 填充所有输出图像的象素。

    //如果部分象素落在输入图像的边界外,那么它们的值设定为 fillval.

    //CV_WARP_INVERSE_MAP - 指定 map_matrix 是输出图像到输入图像的反变换,

    warpAffine(img, img_rotate, map_matrix, Size(width_rotate, height_rotate), 1, 0, 0);

    return img_rotate;

    }

    int main(int argc, char *argv)

    {

    int degree;

    Mat m_SrcImg;

    m_SrcImg = imread(C:\\Users\\lidabao\\Desktop\\Lena1.bmp);

    namedWindow(原图像, 1);

    imshow(原图像, m_SrcImg);

    cout 请输入旋转的度数:;

    cin degree;

    Mat m_ResImg = rotateImage1(m_SrcImg, degree);

    namedWindow(旋转后图像, 1);

    imshow(旋转后图像, m_ResImg);

    waitKey(0);

    }

    4/5

    旋转角度设为30度程序运行如下图:

    5/5

    旋转角度设为120度程序运行如下图:

    注意事项

    注意opencv的正确配置

    warpAffine()函数的正确使用

    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_594903.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-05 17:13:20  耗时:0.025
    0.0254s