这次示例程序分别演示了用resize、pyrUp、pyrDown来让源图像进行放大缩小操作
#include<opencv2/opencv.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> #include<iostream> using namespace std; using namespace cv; //==================================== // 定义一些辅助宏 //===================================== #define WINDOW_NAME "【程序窗口】" //==================================== // 全局变量声明 //===================================== Mat g_srcImage, g_dstImage, g_tmpImage; //操作说明函数 void op() { cout << "\t\t 按键操作说明" << endl; cout << "\t\t键盘按下【1】或者【w】—进行基于【resize】函数的图片放大" << endl; cout << "\t\t键盘按下【2】或者【s】—进行基于【resize】函数的图片缩小" << endl; cout << "\t\t键盘按下【3】或者【a】—进行基于【pyrUp】函数的图片放大" << endl; cout << "\t\t键盘按下【4】或者【d】—进行基于【pyrDown】函数的图片缩小" << endl; } int main() { system("color 5F"); op(); //载入原始图 g_srcImage = imread("E:\\Pec\\绿巨人.jpg"); //创建显示窗口 namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE); namedWindow("【原图】", WINDOW_AUTOSIZE); imshow("【原图】", g_srcImage); //参数赋值 g_tmpImage = g_srcImage; g_dstImage = g_tmpImage; int key = 0; //轮询获取按键信息 int i = 1; while (1) { key = waitKey(9); switch (key) { case 27://按ESC return 0; break; case 'q': return 0; break; //================图片放大按键处理==================== case 'a'://按下a,调用pyrUp函数 pyrUp(g_tmpImage, g_dstImage, Size(g_tmpImage.cols * 2, g_tmpImage.rows * 2)); cout<<i << "、检测到【a】,图片放大,图片尺寸x2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case 'w'://按下w,调用resize函数 resize(g_tmpImage, g_dstImage, Size(g_tmpImage.cols * 2, g_tmpImage.rows * 2)); cout <<i<< "、检测到【w】,图片放大,图片基于【resize】函数放大尺寸x2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case '1': resize(g_tmpImage, g_dstImage, Size(g_tmpImage.cols * 2, g_tmpImage.rows * 2)); cout <<i<< "、检测到【1】按下,图片放大,图片基于【resize】函数放大尺寸x2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case '3'://调用pyrUp函数 pyrUp(g_tmpImage, g_dstImage, Size(g_tmpImage.cols * 2, g_tmpImage.rows * 2)); cout << i<<"、检测到【3】按下,图片放大,图片尺寸x2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; //================图片缩小按键处理==================== case 'd'://按下d,调用pyrDown函数 pyrDown(g_tmpImage, g_dstImage, Size(g_tmpImage.cols / 2, g_tmpImage.rows /2)); cout <<i<< "、检测到【d】按下图片缩小,图片尺寸/2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case 's'://按下s,调用resize函数 resize(g_tmpImage, g_dstImage, Size(g_tmpImage.cols / 2, g_tmpImage.rows / 2)); cout <<i<< "、检测到【s】按下,图片缩小,图片基于【resize】函数缩小尺寸/2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case '2': resize(g_tmpImage, g_dstImage, Size(g_tmpImage.cols / 2, g_tmpImage.rows / 2), (0,0),(0,0),2); cout <<i<< "、检测到【2】按下,图片缩小,图片基于【resize】函数缩小尺寸/2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; case '4'://调用pyrUp函数 pyrDown(g_tmpImage, g_dstImage, Size(g_tmpImage.cols / 2, g_tmpImage.rows / 2)); cout <<i<< "、检测到【4】按下,图片缩小,图片尺寸/2" << endl; i++; if (i >= 15) { i = 0; system("cls"); op(); } break; } //经过操作后显示变化后的图 imshow(WINDOW_NAME, g_dstImage); //将g_dstImage赋给g_tmpImage,方便下次循环 g_tmpImage = g_dstImage; } return 0; }
原图显示:
通过不断放大缩小得到如下: