• 封装了opencv的旋转图像函数


    void ljb_cv_rotate_buf_size(IplImage *imgSrc, double degree, int *w_dst, int *h_dst)
    {    
        double angle, a, b;
        int    w_src, h_src;
    
        angle = degree  * CV_PI / 180.;
        a = sin(angle), b = cos(angle); 
    
        w_src = imgSrc->width;
        h_src = imgSrc->height;
    
        *w_dst = (int)(h_src * fabs(a) + w_src * fabs(b));
        *h_dst = (int)(w_src * fabs(a) + h_src * fabs(b));
    }
    
    void ljb_cv_rotate(IplImage *imgSrc, IplImage *imgDst, double degree)
    {    
        double angle, a, b;
        int    w_src, h_src, w_dst, h_dst;
        double map[6];
        CvMat  map_matrix = cvMat(2, 3, CV_64FC1, map);
        CvPoint2D32f pt = {0};
    
        angle = degree  * CV_PI / 180.; 
        a = sin(angle), b = cos(angle); 
    
        w_src = imgSrc->width;
        h_src = imgSrc->height;
    
        w_dst = imgDst->width;
        h_dst = imgDst->height;
    
        pt = cvPoint2D32f(w_src / 2, h_src / 2);
        cv2DRotationMatrix(pt, degree, 1.0, &map_matrix);//旋转中心,角度,尺度,生成2*3旋转矩阵
    
        // Adjust rotation center to dst's center,
        // otherwise you will get only part of the result
        map[2] += (w_dst - w_src) / 2;
        map[5] += (h_dst - h_src) / 2;
    
        cvWarpAffine(
            imgSrc, 
            imgDst,
            &map_matrix,
            CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS,
            cvScalarAll(0)
            );  
    }
    
        IplImage* img = cvLoadImage("E:\bgtest.bmp", CV_LOAD_IMAGE_GRAYSCALE);     
        IplImage* dst = NULL;
        int dstWidth, dstHeight = 0;
        IplImage* imgRotate = NULL;
        CvSize cvsize = {0};
        
        ljb_cv_rotate_buf_size(img, 45, &dstWidth, &dstHeight);
        cvsize.width = dstWidth;
        cvsize.height = dstHeight;
        imgRotate = cvCreateImage(cvsize, IPL_DEPTH_8U, 1);
        cvZero(imgRotate);
        ljb_cv_rotate(img, imgRotate, 45);
         cvSaveImage("E:\saltsingle3000.bmp",imgRotate);
    

    理解可参考: 

    http://blog.csdn.net/xiaowei_cqu/article/details/7616044

    http://blog.csdn.net/augusdi/article/details/9022719

  • 相关阅读:
    解决移动端页面在苹果端滑不到底部的问题
    js点击事件在苹果端失效的问题
    小程序开发基本步骤
    css多行文本省略号(...)
    js判断pc端和移动端的方法
    主流浏览器css兼容问题的总结
    让ie6对png透明图片支持起来
    ajax的探究与使用
    css3实现逐渐变大的圆填充div背景的效果
    js中setTimeout()时间参数设置为0的探讨
  • 原文地址:https://www.cnblogs.com/faith0217/p/4982182.html
Copyright © 2020-2023  润新知