c++ opencv 实现鼠标点击图片,标记位置显示

在直接打开图片无法显示图片像素点位置,有时候需要对像素点位置进行编辑,而目前比较多的是python代码实现,故此利用c++实现鼠标点击图片,标记位置显示。

效果图:

实现代码:

#include<opencv2/imgcodecs.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
using namespace cv;
using namespace std;
  获取鼠标点击的坐标   /

string path = "Learn-OpenCV-cpp-in-4-Hours-main\\Resources\\cards.jpg";
static Mat img = imread(path);
// 回调函数:鼠标点击输出点击的坐标
//(事件(鼠标移动、左键、右键),横坐标,纵坐标,组合键,setMouseCallback的userdata用于传参)
void mouse_callback(int event, int x, int y, int flags, void* param) {
    // 如果鼠标左键点击,则输出横坐标和纵坐标
    if (event == EVENT_LBUTTONDOWN) {
        cout << x << "  " << y << endl;
        //在图像上绘制点
        circle(img, Point(x, y), 2, Scalar(0, 69, 255), -1);//厚度10,填满 FILLED
        //(图像,文本内容,坐标点,字体类型,字体大小,颜色,字体粗细)
        string str = "(" + to_string(x) + "," + to_string(y) + ")";
        putText(img, str, Point(x, y), FONT_HERSHEY_DUPLEX, 0.75, Scalar(0, 0, 0), 2);
        imshow("Point Coordinates", img);
    }

}

void main() {
    // 创建窗口
    namedWindow("Point Coordinates");
    // 将回调函数绑定到窗口
    setMouseCallback("Point Coordinates", mouse_callback);
    //显示图像
    imshow("Point Coordinates", img);
    waitKey(0);
    destroyAllWindows();
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
您好!对于使用C++OpenCV实现RTSP视频流鼠标缩放的问题,您可以按照以下步骤进行操作: 1. 首先,您需要通过OpenCV的VideoCapture函数连接到RTSP视频流。 2. 然后,您可以创建一个名为“mouseCallback”的回调函数,用于捕获鼠标事件并实现缩放操作。 3. 在回调函数中,您可以使用cv::resize函数来实现缩放操作。您需要根据鼠标事件的位置计算出要缩放的图像区域,并将其传递给cv::resize函数。 4. 最后,您需要使用cv::imshow函数显示缩放后的图像。在主函数中,您可以使用cv::setMouseCallback函数将“mouseCallback”回调函数与图像窗口绑定,以便在鼠标事件发生时调用它。 以下是一个简单的示例代码,用于演示如何在OpenCV实现RTSP视频流鼠标缩放: ```c++ #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; bool isDragging = false; Rect dragRect; Point startPoint; void mouseCallback(int event, int x, int y, int flags, void* userdata) { if (event == EVENT_LBUTTONDOWN) { isDragging = true; startPoint = Point(x, y); dragRect = Rect(x, y, 0, 0); } else if (event == EVENT_MOUSEMOVE) { if (isDragging) { dragRect.width = x - dragRect.x; dragRect.height = y - dragRect.y; } } else if (event == EVENT_LBUTTONUP) { isDragging = false; if (dragRect.width < 0) { dragRect.x += dragRect.width; dragRect.width *= -1; } if (dragRect.height < 0) { dragRect.y += dragRect.height; dragRect.height *= -1; } Mat* img = (Mat*)userdata; Mat roi(*img, dragRect); resize(roi, roi, Size(roi.cols * 2, roi.rows * 2)); imshow("RTSP Video Stream", *img); } } int main() { // Open RTSP stream VideoCapture cap("rtsp://example.com/stream"); if (!cap.isOpened()) { cerr << "Failed to open RTSP stream!" << endl; return -1; } Mat frame; namedWindow("RTSP Video Stream"); while (true) { // Capture frame from RTSP stream cap.read(frame); if (frame.empty()) { cerr << "Failed to capture frame from RTSP stream!" << endl; break; } // Bind mouse callback function to window setMouseCallback("RTSP Video Stream", mouseCallback, &frame); // Display frame imshow("RTSP Video Stream", frame); // Check for key press int key = waitKey(1); if (key == 27) { break; } } // Release resources cap.release(); destroyAllWindows(); return 0; } ``` 在这个示例代码中,回调函数“mouseCallback”用于实现鼠标缩放操作。当用户按下鼠标左键时,回调函数将标记“isDragging”设置为true,并记录鼠标位置作为缩放区域的起点。随着鼠标移动,回调函数将根据鼠标位置更新缩放区域的大小。当用户释放鼠标左键时,回调函数将标记“isDragging”设置为false,并根据缩放区域的大小和位置计算出要缩放的图像区域,然后使用cv::resize函数将其缩放为原来的两倍,并显示在窗口中。 请注意,这只是一个简单的示例代码,您需要根据实际需求进行修改和优化。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值