图像缩放是一种比较简单的图像处理操作,这里给出opencv中的代码,
opencv的版本C语言接口
int resize_c() { const char *pstrImageName = "lena.jpg"; const char *pstrSaveImageName = "lena缩放图.jpg"; const char *pstrWindowsSrcTitle = "原图"; const char *pstrWindowsDstTitle = "缩放图 "; double fScale = 0.8; //缩放倍数 CvSize czSize; //目标图像尺寸 //从文件中读取图像 IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED); IplImage *pDstImage = NULL; //计算目标图像大小 czSize.width = pSrcImage->width * fScale; czSize.height = pSrcImage->height * fScale; //创建图像并缩放 pDstImage = cvCreateImage(czSize, pSrcImage->depth, pSrcImage->nChannels); cvResize(pSrcImage, pDstImage, CV_INTER_AREA); //创建窗口 cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE); cvNamedWindow(pstrWindowsDstTitle, CV_WINDOW_AUTOSIZE); //在指定窗口中显示图像 cvShowImage(pstrWindowsSrcTitle, pSrcImage); cvShowImage(pstrWindowsDstTitle, pDstImage); //等待按键事件 cvWaitKey(); //保存图片 cvSaveImage(pstrSaveImageName, pDstImage); cvDestroyWindow(pstrWindowsSrcTitle); cvDestroyWindow(pstrWindowsDstTitle); cvReleaseImage(&pSrcImage); cvReleaseImage(&pDstImage); return 0; }
C++版本
int resize_cpp() { string pstrImageName = "lena.jpg"; string pstrSaveImageName = "lena缩放图.jpg"; string pstrWindowsSrcTitle = "原图"; string pstrWindowsDstTitle = "缩放图 "; double fScale = 0.8; //缩放倍数 Size czSize; //目标图像尺寸 //从文件中读取图像 Mat pSrcImage = imread(pstrImageName); //计算目标图像大小 czSize.width = pSrcImage.cols * fScale; czSize.height = pSrcImage.rows * fScale; //创建图像并缩放 Mat pDstImage = Mat(czSize, CV_8UC3); resize(pSrcImage, pDstImage, czSize,CV_INTER_AREA); //创建窗口 namedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE); namedWindow(pstrWindowsDstTitle, CV_WINDOW_AUTOSIZE); //在指定窗口中显示图像 imshow(pstrWindowsSrcTitle, pSrcImage); imshow(pstrWindowsDstTitle, pDstImage); //等待按键事件 waitKey(); return 0; }
来源:https://www.cnblogs.com/adong7639/p/4435852.html