滑动条缩放图片


#include "opencv/cv.h"
#include "opencv/highgui.h"
#include 

int slider_position = 0;	//滚动条的位置变量
double  fScale = 1.0;		//缩放比例 

CvSize imageSize0;	 	//原始图像尺寸	
CvSize imageSize1;		//目标图像尺寸

IplImage* dst_image = NULL;
IplImage* src_image = NULL;

void onTrackbarSlider(int pos);//回调函数


int main(int argc,char** argv)
{
	//读取文件
	src_image = cvLoadImage(argv[1]);
	
	//初始化图像大小
	imageSize0.width = src_image->width;
	imageSize0.height = src_image->height;
	
		//创建窗口
	cvNamedWindow("Image",1);
	
		//在指定窗口显示图像
	cvShowImage("Image",src_image);
	
	cvCreateTrackbar(
			"Scale",
			"Image",
			&slider_position,
			120,
			onTrackbarSlider
			);	
	
	//等待ESC按键事件
	while(1){
		if(cvWaitKey(15) == 27) break;
	}

	//摧毁窗口并释放内存
	cvReleaseImage(&src_image);
	cvReleaseImage(&dst_image);
	cvDestroyWindow("Image");

	return 0;
}


void onTrackbarSlider(int pos)//回调函数
{
	//由pos(0~120)映射到fScale(2~8 )
	fScale = pos /20.0+ 2;	
	
	//计算目标图像的大小
	imageSize1.width = imageSize0.width * fScale;
	imageSize1.height =imageSize0.height * fScale;	
	
	//释放资源并重新创建图像
	cvReleaseImage(&dst_image);
	dst_image = cvCreateImage(imageSize1,src_image->depth,src_image->nChannels);
	
	//缩放图像
	cvResize(src_image,dst_image,CV_INTER_AREA);
	
	//在指定窗口显示图像
	cvShowImage("Image",dst_image);
	
	printf("TrackbarPos = %d\n",pos);
	printf("fScale = %.1f\n",fScale);
	
}




效果图:

滑动条缩放图片_第1张图片



























你可能感兴趣的:(OpenCV)