OpenCV学习之图像的旋转与缩放

利用公式将图形旋转,缩放。采用CvGetQuadrangleSubPix提取像素四边形,双线性插值,自动变换角度。

//图像的旋转与缩放
#include "cv.h"
#include "highgui.h"
#include "math.h"
int main(int argc, char** argv) {
    IplImage* src = cvLoadImage("Lena.tif", 1);
    IplImage* dst = cvCloneImage(src);
    int delta = 1;
    int angle = 0;
    int opt = 1;//1旋转加缩放,0旋转
    double factor;
    cvNamedWindow("src", 1);
    cvShowImage("src", src);
    while (1) {
        float m[6];
        CvMat M = cvMat(2, 3, CV_32F, m);
        int w = src->width;
        int h = src->height;
        if (opt)
            factor = (cos(angle * CV_PI / 180.) + 1.05) * 2;
        else
            factor = 1;
        m[0] = (float)(factor*cos(-angle * 2 * CV_PI / 180.));
        m[1] = (float)(factor*sin(-angle * 2 * CV_PI / 180.));
        m[3] = -m[1];
        m[4] = m[0];
        //将旋转中心移至图像中间
        m[2] = w*0.5f;
        m[5] = h*0.5f;

        //dst = A*src + b
        cvGetQuadrangleSubPix(src, dst, &M);//双线性插值,提取像素四边形
        cvNamedWindow("dst", 1);
        cvShowImage("dst", dst);
        if (cvWaitKey(5) == 27)
            break;
        angle = (int)(angle + delta) % 360;//自动变换角度
    }
    return 0;
}

你可能感兴趣的:(OpenCV学习笔记)