#include <opencv2/opencv.hpp>
#include <iostream>
cv::Mat image;
void onMouse(int event, int x, int y, int flags, void* userdata) {
if (event == cv::EVENT_MOUSEMOVE) {
cv::Vec3b pixel = image.at<cv::Vec3b>(y, x);
std::cout << "鼠标位置 (" << x << ", " << y << ") 的像素值: ("
<< static_cast<int>(pixel[0]) << ", "
<< static_cast<int>(pixel[1]) << ", "
<< static_cast<int>(pixel[2]) << ")" << std::endl;
}
}
int main() {
image = cv::imread("path_to_your_image.jpg");
if (image.empty()) {
std::cout << "无法读取图像!" << std::endl;
return -1;
}
cv::namedWindow("Image", cv::WINDOW_NORMAL);
cv::setMouseCallback("Image", onMouse, NULL);
cv::imshow("Image", image);
cv::waitKey(0);
return 0;
}