【opencv学习笔记】1、opencv缩放图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tanzui/article/details/53872258
#include <opencv2\opencv.hpp>
using namespace std;
int main()
{
    //定义原图窗口标题
    const char *windowsTitle = "原图";
    //定义缩放后窗口标题
    const char *theZoomWindowsTitle = "缩放后图片";
    //读入原图地址
    const char *ImageFilePath = "C:\\Users\\Administrator\\Documents\\visual studio 2013\\Projects\\opencvTest\\Debug\\a.jpg";
    //要保存的缩放后地址
    const char *theZoomImageFilePath = "C:\\Users\\Administrator\\Documents\\visual studio 2013\\Projects\\opencvTest\\Debug\\b.jpg";
    //要缩放的倍数
    double ZoomMultiples = 0.314;
    //创建opencv长宽结构图变量
    CvSize imageSize;
    
    //读入原图
    IplImage *test = cvLoadImage(ImageFilePath);//图片路径
    //缩放后图暂时为空
    IplImage *theZoomTest = NULL;
    //读取原图宽高并乘以缩放比,得到缩放后的长宽
    imageSize.width = test->width * ZoomMultiples;
    imageSize.height = test->height * ZoomMultiples;
    //创建缩放后的图像数据,参数1-图像大小,参数2-图像深度,这里从原图获取,参数3-图像通道数,这里依然从原图获取
    theZoomTest = cvCreateImage(imageSize,test->depth,test->nChannels);
    /*图像大小变换,参数1-输入图像,参数2-输出图像,参数3-插值方法
    插值方法有四:
    CV_INTER_NN - 最近邻插值
    CV_INTER_LINEAR - 双线性插值 (缺省使用)
    CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
    CV_INTER_CUBIC - 立方插值.
    这个函数在功能上与Win32 API中的StretchBlt()函数类似。
    */
    cvResize(test, theZoomTest, CV_INTER_AREA);
    //创建窗口,CV_WINDOW_AUTOSIZE自适应大小
    cvNamedWindow(windowsTitle, CV_WINDOW_AUTOSIZE);
    cvNamedWindow(theZoomWindowsTitle, CV_WINDOW_AUTOSIZE);
    //在指定窗口显示图像
    cvShowImage(windowsTitle, test);
    cvShowImage(theZoomWindowsTitle, theZoomTest);
    //等待按键
    cvWaitKey(0);
    //保存缩放后图片,参数1-路径,参数2-图像数据
    cvSaveImage(theZoomImageFilePath,theZoomTest);
    //销毁窗口
    cvDestroyWindow(windowsTitle);
    cvDestroyWindow(theZoomWindowsTitle);
    //释放图片
    cvReleaseImage(&test);
    cvReleaseImage(&theZoomTest);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/tanzui/article/details/53872258