highgui的拓展

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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值