1.highgui模块中有可以大量处理图像的函数 运用这些函数可以使程序对于键盘和鼠标做出响应 也可以在图像上绘制轮廓或写入文本
2.在图像上点击
通过编程实现鼠标置于图像窗口上时运行特定的指令 需要定义一个适当的回调函数【不会被显式的调用,但是会影响特定的事件】
ps :回调函数应该有特定的签名,并且必须注册 对于鼠标事件处理函数 回调函数必须有:
void onMouse(int event,int x,int y,int flags,void *param);
void onMouse(int event,int x,int y,int flags,void *param)
{
cv::Mat *im = reinterpret_cast<cv::Mat*>(param);
switch(event)
{
case CV_EVENT_LBUTTONDOWN://注册响应鼠标左键
cout<<"at ("<<x<<","<<y<<") value is: "<<static_cast<int>(im->at<uchar>(cv::Point(x,y))) <<endl;
break;
}
}
在主函数内的调用
cv::setMouseCallback("Original Image",onMouse,reinterpret_cast<void*>(&image));
其中:
reinterpret_cast 是一种 c++标准转换运算符
reinterpret_cast 运算符是用来处理无关类型之间的转换;它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位
代码实例
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;
void onMouse(int event ,int x,int y,int flag,void *param);
void onMouse(int event ,int x,int y,int flag,void *param)
{
Mat *im=reinterpret_cast<Mat*>(param);
switch(event)
{
case CV_EVENT_LBUTTONDOWN:
cout << "at (" << x << "," << y << ") value is: " << static_cast<int>(im->at<uchar>(cv::Point(x,y))) << std::endl;//只是用来的到一个点的坐标
break;
}
}
int main()
{
Mat image;
image=imread("lena.jpg",0);
if(image.empty())
return 0;
namedWindow("hello");
imshow("hello",image);
setMouseCallback("hello", onMouse, reinterpret_cast<void*>(&image));
circle(image, // destination image
cv::Point(299,307), // center coordinate
65, // radius
0, // color (here black)
3);
imshow("hello",image);
putText(image, // destination image
"she is a women", // text
cv::Point(200,481), // text position
cv::FONT_HERSHEY_PLAIN, // font type
2.0, // font scale
255, // text color (here white)
2);
imshow("hello",image);
waitKey(0);
image.release();
destroyAllWindows();
return 0;
}