ROS_Hydro_OpenCV2_用鼠标选择图像区域,并保存1,2,3...

41 篇文章 1 订阅

ImgCapMouse.cpp文件

#include <ros/ros.h>  

#include <image_transport/image_transport.h>  

#include <cv_bridge/cv_bridge.h>  

#include <sensor_msgs/image_encodings.h>  

#include <opencv2/imgproc/imgproc.hpp>  

#include <opencv2/highgui/highgui.hpp>  
#include "opencv2/video/tracking.hpp"  
#include <geometry_msgs/Twist.h>  
#include <std_msgs/Float32.h>  
#include <std_msgs/UInt8.h>  

using namespace cv;  
using namespace std;  

//Store all constants for image encodings in the enc namespace to be used later.  
namespace enc = sensor_msgs::image_encodings;  

bool selectObject = false;  
 
static Point origin;  
static Rect selection;  

Mat image;   
static int CapImgNum=0;  //采集图像个数 

void ImgCapMouse(Mat inImg);  

static void onMouse( int event, int x, int y, int, void* )  
{  
	ROS_INFO("Mouse detected");  
	if( selectObject )  
	{  
	    selection.x = MIN(x, origin.x);  
	    selection.y = MIN(y, origin.y);  
	    selection.width = std::abs(x - origin.x);  
	    selection.height = std::abs(y - origin.y);  
           // rectangle(image, origin,Point(x,y),Scalar(0,0,255), 1, 8 );  //画矩形
	    selection &= Rect(0, 0, image.cols, image.rows);  //用于确保所选的矩形区域在图片范围内	    
	}  

	switch( event )  
	{  
	case CV_EVENT_LBUTTONDOWN:  
	    origin = Point(x,y);  
	    selection = Rect(x,y,0,0);  
	    selectObject = true;  
	    break;  
	case CV_EVENT_LBUTTONUP:  
	    selectObject = false;  
	    if( selection.width > 0 && selection.height > 0 )  
	    {
		
                //Mat img=image(Rect(selection.x,selection.y,selection.width,selection.height));
		Mat img(image, selection);
                bitwise_not(img, img);  
                CapImgNum++; 
                stringstream sss;      
                string name;  
                name="/home/hsn/catkin_ws/src/rosopencv/";  
            	sss.clear();      
            	sss<<name;      
            	sss<<CapImgNum;      
            	sss<<".jpg";      
            	sss>>name;  
	    	cout<<name<<"\n";
	    	imwrite(name, img); 
	    }
	    break;  
	}  

}    

//This function is called everytime a new image is published  
void imageCallback(const sensor_msgs::ImageConstPtr& original_image)  
{  
	//Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing  
	cv_bridge::CvImagePtr cv_ptr;  
	try  
	{  
	    //Always copy, returning a mutable CvImage  
	    //OpenCV expects color images to use BGR channel order.  
	    cv_ptr = cv_bridge::toCvCopy(original_image, enc::BGR8);  
	}  
	catch (cv_bridge::Exception& e)  
	{  
	    //if there is an error during conversion, display it  
	    ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());  
	    return;  
	}  
	ImgCapMouse(cv_ptr->image);  
}  


void ImgCapMouse(Mat inImg)  
{  
static bool paused = false;  

//If the image processing is not paused  
if( !paused )  
{  
	//cap >> frame;  
	if( inImg.empty() )  
	{  
	  ROS_INFO("Camera image empty");  
	  return;//break;  
	}  
}  

//Use the input image as the reference  
//Only a shallow copy, so relatively fast  
image = inImg;  


//Code to display an inverted image of the selected region  
//Remove this in the fall validation expt TBD  
if( selectObject && selection.width > 0 && selection.height > 0 )  
{  
  Mat roi(image, selection);  
  bitwise_not(roi, roi);  //反转图像灰度,即求图像的负片.将图像中每点的灰度变成(255-gray),其中gray是当前点的灰度。
}  
 

char c = (char)waitKey(1);  
if( c == 27 )  
  ROS_INFO("Exit boss");//break;  
switch(c)  
{    
case 'p':  //暂停检测或继续
  paused = !paused;  
  break;  
default:  
  break;  
}  
setMouseCallback( "ObjCap", onMouse, 0 );//   
imshow( "ObjCap", image );  
}  

/** 
* This is ROS node to track the destination image 
*/  
int main(int argc, char **argv)  
{  

ros::init(argc, argv, "image_processor");  
ROS_INFO("-----------------");  

ros::NodeHandle nh;  
//Create an ImageTransport instance, initializing it with our NodeHandle.  
image_transport::ImageTransport it(nh);  

//OpenCV HighGUI call to create a display window on start-up.  

namedWindow( "ObjCap", 0 );  


image_transport::Subscriber sub = it.subscribe("camera/rgb/image_raw", 1, imageCallback);
//OpenCV HighGUI call to destroy a display window on shut-down.  
//destroyWindow(WINDOW);  
 
destroyWindow("ObjCap");  


/** 
* In this application all user callbacks will be called from within the ros::spin() call. 
* ros::spin() will not return until the node has been shutdown, either through a call 
* to ros::shutdown() or a Ctrl-C. 
*/  
ros::spin();  

//ROS_INFO is the replacement for printf/cout.  
ROS_INFO("tutorialROSOpenCV::main.cpp::No error.");  
}  

.launch文件

<launch>
<include file="/opt/ros/hydro/share/openni_launch/launch/openni.launch" /> 
<node name="ImgCapMouse" pkg="rosopencv" type="ImgCapMouse" output="screen" >
 </node>
</launch>


注解:Opencv中响应鼠标消息

每当鼠标在视讯窗口接口点击一下的时候,都会有固定三个动作
1.
点击(Click)
2.
放开(Down)3.滑动(move)
因此,程序执行鼠标在点击的时候onMouse()都会连续跑三次,代表鼠标在点击的时候连续的三个事件,
void onMouse(int Event,int x,int y,int flags,void* param )
子程序的自变量分成四个不同的分类,分别为
1.
事件回传代号(int Event)
2.
坐标(int x,int y)
3.flags
代号(int flags) 4.Mouse事件的代号名称(param)
Event
代表的是鼠标回传的事件号码,每当鼠标有动作,Event就会回传一个整数讯息到onMouse(),也顺便回传鼠标移动的坐标,flags代表的是拖曳事件,param则是自己定义onMouse()事件的ID,就跟GUI接口的窗口接口ID一样(cvGetWindowHandle()),不过这边是自己给的编号,而窗口接口的ID则是系统自动随机分配的ID,而鼠标事件的执行可以细分的分类为
Event:
#define CV_EVENT_MOUSEMOVE 0                  
滑动
#define CV_EVENT_LBUTTONDOWN 1          
左键点击
#define CV_EVENT_RBUTTONDOWN 2          
右键点击
#define CV_EVENT_MBUTTONDOWN 3          
中键点击
#define CV_EVENT_LBUTTONUP 4                
左键放开
#define CV_EVENT_RBUTTONUP 5                
右键放开
#define CV_EVENT_MBUTTONUP 6                
中键放开
#define CV_EVENT_LBUTTONDBLCLK 7        
左键双击
#define CV_EVENT_RBUTTONDBLCLK 8        
右键双击
#define CV_EVENT_MBUTTONDBLCLK 9        
中键双击
flags:
#define CV_EVENT_FLAG_LBUTTON 1          
左键拖曳
#define CV_EVENT_FLAG_RBUTTON 2          
右键拖曳
#define CV_EVENT_FLAG_MBUTTON 4          
中键拖曳
#define CV_EVENT_FLAG_CTRLKEY 8     (8~15)
Ctrl不放事件
#define CV_EVENT_FLAG_SHIFTKEY 16   (16~31)
Shift不放事件
#define CV_EVENT_FLAG_ALTKEY 32       (32~39)
Alt不放事件

上面的#defineOpenCV自行定义的参数,要做事件捕捉的时候,可以用参数,亦可以用纯数字表示.简单的介绍mouse相关的东西.

cvSetMouseCallback()
鼠标事件呼叫函式,需要给它一个Handler,也就是事件驱动的子程序名称,Handler必须要符合void xxx(int event,int x,int y,int flags,void* param )格式.
cvSetMouseCallback("
窗口名称",自行定义子程序名称,自行定义子程序名称的ID);

onMouse()自行定义的鼠标事件名称,可以接受鼠标讯息做相关控制.
void xxx(
事件讯息整数代号,鼠标x轴坐标,鼠标y轴坐标,拖曳事件讯息整数代号,自行给予xxx这子程序的ID编号)



评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值