zoukankan      html  css  js  c++  java
  • 封装了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

  • 相关阅读:
    C# 按笔画排序
    WEB EXCEL OWC开发(老资料)
    JS操作Cookie
    汉字转拼音缩写取首字母
    javaScript通用数据类型校验
    嵌套Repeater 子层获得父层字段值 经典!!!
    不同于其他的provider: SQL 网络接口, error: 26 定位指定的服务器/实例时出错
    C# UDP 发送 接收
    Js 过滤空格
    高亮文本框
  • 原文地址:https://www.cnblogs.com/faith0217/p/4982182.html
Copyright © 2011-2022 走看看