以下是详细的代码和注释

发布时间:2025-06-24 06:55:54  作者:北方职教升学中心  阅读量:681


#pragma once#include <iostream>#include <opencv2//opencv.hpp>using namespace cv;using namespace std;// 处理鼠标事件的回调函数void onMouse(int event, int x, int y, int flags, void* param) {    // 将参数转换为图像矩阵    Mat* im = reinterpret_cast<Mat*>(param);    // 处理鼠标事件    switch (event) {        case EVENT_LBUTTONDOWN: // 点击左键事件            // 检查图像是否为单通道            if (static_cast<int>(im->channels()) == 1)             {                // 处理单通道图像(灰度图)                switch (im->type())                {                ////Mat每个格子中的数据格式---------- - Mat定义				//Mat_<uchar>-------- - CV_____#xff08;0-255)				//Mat<char>---------- - CV_____;-128-127)				//Nat_<short>-------- - CV_16S(-32768-32767)				//Mat_<ushort>--------CV_16U(0-65535)				//Mat_<int>---------- - CV_32S(-2147483648-2147483647)				//Mat_<float>----------CV_32F(-FLT_MAX…FLT_MAX,INF,NAN)				//Mat_<double>--------CV_64F(-DBL_MAX…DBL_MAX,INF,NAN)                    case CV_8U:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<uchar>(Point(x, y))) << endl;                         break;                    case CV_8S:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<char>(Point(x, y))) << endl;                         break;                    case CV_16U:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<ushort>(Point(x, y))) << endl;                         break;                    case CV_16S:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<short>(Point(x, y))) << endl;                         break;                    case CV_32S:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<int>(Point(x, y))) << endl;                         break;                    case CV_32F:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<float>(Point(x, y))) << endl;                         break;                    case CV_64F:                        cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<double>(Point(x, y))) << endl;                         break;                }            }            else             {                // 处理多通道图像(颜色图)                cout << "at (" << x << ", " << y << ")"                     << "  B value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[0])                     << "  G value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[1])                     << "  R value is: " << static_cast<int>(im->at<Vec3b>(Point(x, y))[2])                     << endl;            }            break;    }}。使用。我们需要定义鼠标事件处理函数,当用户点击图像时,

:该函数根据鼠标事件的类型处理用户点击。代码分为两部分:一个是回调函数࿰,

首先,主程序。

结论。
flags:与事件相关的标志,例如鼠标按钮状态等。鼠标事件处理函数。函数将图像窗口与鼠标事件处理函数联系起来。

二、代码说明。用于鼠标事件处理c;另一个是主程序。

2. main.cpp。

#include "onMouse.h"using namespace cv;using namespace std;int main(){ // 读取图像 Mat img1 = imread("D:/123.jpg"); if (img1.empty()) { cout << "图像无法打开൪ << endl; return -1; } // 显示图像 imshow("image1"#;, img1); // 设置鼠标回调函数,监控图像窗口鼠标事件 setMouseCallback("image1"#;, onMouse, reinterpret_cast<void*>(&img1)); // 等待用户按钮 waitKey(0); return 0;}。

  • 三、

    • 主程序。#xff0c;回调函数会触发,打印当前像素的坐标和像素值。三、
      param:用户自定义的参数,这里用来传输图像数据(指向 Mat 对象指针)。
      x、

    • onMouse函数。主程序。函数加载图像󿀌检查加载是否成功。

      void onMouse(int event, int x, int y, int flags, void* param);
      这是处理鼠标事件的回调函数,参数说明:
      event:鼠标事件类型(如左键点击、

      在这里插入图片描述

      在这个博客中,我们将介绍如何在C+中使用OpenCV库;+鼠标点击操作,获取图像的像素坐标和像素值。

waitKey(0)。根据图像的通道数(单通道或多通道)获取不同的像素值。

主程序部分负责加载图像并设置鼠标回调函数。

一、y:图像窗口中鼠标的坐标。
  • setMouseCallback。
  • imread。使用。程序将在这里暂停。当用户点击图像时,该功能广泛应用于图像处理和分析,点击查看图像特定点的值。以下是详细的代码和注释。通过上述代码󿀌您可以很容易地获得图像的像素坐标和像素值。
  • imshow。
  • 二、该函数将被调用。代码说明。右键点击等)。

    使用OpenCV(C++)图像的像素坐标和像素值通过鼠标点击操作获得。使用。

    用于等待用户按钮,在用户按下任何按钮之前,图像显示函数。࿰在左键点击事件中c;根据图像的通道类型和数量,获取并打印出像素值。

    1. onMouse.h。