封装了opencv的旋转图像函数
阅读原文时间:2023年07月09日阅读:2

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

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章