利用OpenCV写一个链接库在图像显示窗口接收鼠标操作

projrct/src/main.cpp

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <sub.h>
using namespace std;
using namespace cv;

int main ( int argc, char** argv ){
    if ( argc != 2 ){
        cout<<"usage: executive img"<<endl;
        return 1;
    }
    Mat img=imread(argv[1],CV_LOAD_IMAGE_COLOR);
    char window_name[] = "Show";
    namedWindow(window_name, CV_WINDOW_NORMAL);
    setMouseCallback(window_name,mousecallback,(void*)&img);
    imshow(window_name,img);
    cvWaitKey(0);
    return 0;
}

project/src/sub.cpp

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

/*void mousecallback(int event,int x,int y,int flags,void* param){
	Mat& image = *(cv::Mat*) param;
	switch(event){
		case EVENT_LBUTTONDOWN:{
			cout<<"左键按下"<<endl;
			Point point=Point(x,y);
			circle(image, point, 3, cv::Scalar(255, 255, 0), -1);
			imshow("Show",image);waitKey(1);//在这指定相同的窗口名
		}
		break;
	}
}*/

void mousecallback(int event,int x,int y,int flags,void* param){
	Mat& image = *(cv::Mat*) param;
	switch(event){
		case EVENT_MOUSEMOVE:{
			cout<<"鼠标移动"<<endl;	
		}
		break;
		case EVENT_LBUTTONDOWN:{
			cout<<"左键按下"<<endl;
		}
		break;
		case EVENT_LBUTTONUP:{
			cout<<"左键抬起"<<endl;
		}
		break;
		case EVENT_LBUTTONDBLCLK:{
			cout<<"左键双击"<<endl;
		}
		break;
		case EVENT_MBUTTONDOWN:{
			cout<<"中键按下"<<endl;
		}
		break;
		case EVENT_MBUTTONUP:{
			cout<<"中键抬起"<<endl;
		}
		break;
		case EVENT_MBUTTONDBLCLK:{
			cout<<"中键双击"<<endl;
		}
		break;
		case EVENT_RBUTTONDOWN:{
			cout<<"右键按下"<<endl;	
		}
		break;
		case EVENT_RBUTTONUP:{
			cout<<"右键抬起"<<endl;
		}
		break;
		case EVENT_RBUTTONDBLCLK:{
			cout<<"右键双击"<<endl;	
		}
		break;
	}
}

project/include/sub.h

#ifndef _SUB_H_
#define _SUB_H_

void mousecallback(int event,int x,int y,int flags,void* param);

#endif

project/CMakeLists.txt

cmake_minimum_required( VERSION 2.8 )
project(main)

find_package(OpenCV REQUIRED)

include_directories(
    include
    ${OpenCV_INCLUDE_DIRS}
)
add_library(sub
    src/sub.cpp
)
target_link_libraries(sub ${OpenCV_LIBS})
 
add_executable(main src/main.cpp )
target_link_libraries(main 
	${OpenCV_LIBS}
	sub
)

猜你喜欢

转载自blog.csdn.net/qq_26697045/article/details/84874277