opencv图像处理7打开大图

显示大图片,当图片较大时,可以通过鼠标拖动图片上下移动

先看效果:




由于opencv没有滑动条,无法像VC或者java那样直接滑动浏览,但是在图片上直接拖动也是一样的。

拖动时通过捕获鼠标事件,记下按下和抬起的点,
CvPoint first = cvPoint(0,0);
CvPoint  last = cvPoint(0,0);

void on_mouse(int event ,int x, int y,int flags, void* 


param){
switch(event){
case CV_EVENT_LBUTTONDOWN:
{
first.x = x;
first.y = y;
 
}
case CV_EVENT_LBUTTONUP:
{
last.x = x;

last.y = y;


}


鼠标按下和抬起时的点作差得出移动的位移,从而求出拖动后的图片位置。
src_x -= last.x - first.x;
src_y -= last.y - first.y;

源代码:

// Image_ScrollBar.cpp : Defines the entry point for the console application.
//
#include "highgui.h"
#include "cv.h"

#include <iostream>
#define default_width 640
#define default_height 480 //图片超过次界限时认为是大图片
const char * filename= "big.jpg"; 

CvPoint first = cvPoint(0,0);
CvPoint  last = cvPoint(0,0);//鼠标按下和抬起时的点

CvRect rect;//显示的图像在原图中的区域

int src_x = 0;
int src_y = 0;//ROI左上角点

IplImage * img;
int img_width,img_height;//读入的图片的宽高

int window_height ;
int window_width  ;//窗口的宽高
IplImage * dst_image;//显示的图片

using namespace std;
void on_mouse(int event ,int x, int y,int flags, void* param){
	switch(event){
	case CV_EVENT_LBUTTONDOWN:
		{
		first.x = x;
		first.y = y;
							  }
	case CV_EVENT_LBUTTONUP:
		{
			last.x = x;
			last.y = y;
			
			src_x -= last.x - first.x;
			src_y -= last.y - first.y;
			
			if(src_x < 0)src_x = 0;
			else if(src_x > img_width - window_width)src_x = img_width - window_width;
			else ;
			if(src_y < 0 ) src_y = 0;
			else if(src_y > img_height - window_height)src_y = img_height - window_height;
			else;
			cout<<"左上角点:"<<endl;
			cout<<"x:"<<src_x<<endl;
			cout<<"y:"<<src_y<<endl;
			rect = cvRect(src_x, src_y,window_width , window_height);

			cvSetImageROI(img , rect);
			//将矩形区内的图像复制出来
			cvCopy(img,dst_image);
			cvResetImageROI(img);
			cvShowImage("image", dst_image);
		}
	}
}

int main(){
	img = cvLoadImage(filename,1);
	img_width = img->width;
	img_height = img->height;
	//如果图片没有大于默认大小,则可以直接调用opencv的显示函数
		if(img_height <= default_height && img_width <= default_width){
		cvNamedWindow("image",CV_WINDOW_AUTOSIZE);
		cvShowImage("image",img);
		cvWaitKey(0);
		cvReleaseImage(&img);
		cvDestroyAllWindows();
		return 0;
	}
	//如果存在长或宽大于默认值,则需要添加滚动条
	//如果不添加滚动条,直接拖动也可以实现
	//记录鼠标的拖动
		else{
		 window_height = default_height;
		 window_width  = default_width;

		//假如列或者宽小于默认值,则需要调整

		if(img_height < default_height){window_height = img_height;}
		if(img_width  < default_width){ window_width  = img_width; }

		//设置ROI,将ROI内的区域拷贝出来
		 dst_image = cvCreateImage(cvSize(window_width,window_height),img->depth,img->nChannels);
		 rect = cvRect(src_x, src_y,window_width,window_height);

		cvSetImageROI(img , rect);
		//将矩形区内的图像复制出来
		cvCopy(img,dst_image);
		cvResetImageROI(img);
		cvNamedWindow("image",CV_WINDOW_NORMAL);
		cvResizeWindow("image",window_width,window_height);
		cvShowImage("image", dst_image);
    
	//下面开始处理鼠标事件
	//鼠标buttondown和buttonup分别记下其位置,然后由此可以计算出移动的距离
	    cvSetMouseCallback("image",on_mouse ,0);
		cvWaitKey(0);
		cvReleaseImage(&img);
		cvDestroyAllWindows();
	}
	return 0 ;
}






原文地址:https://www.cnblogs.com/libing64/p/2878753.html