gpt4 book ai didi

c++ - 使用 OpenCV 在图像上悬停时打印鼠标指针的像素坐标

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:35 34 4
gpt4 key购买 nike

#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <highgui.h>

using namespace cv;
using namespace std;

Mat image;
char window_name[20]="Get coordinates";

static void onMouse( int event, int x, int y, int f, void* ){
cout << x << " " << y << endl;
//putText(image, "point", Point(x,y), CV_FONT_HERSHEY_PLAIN, 1.0, CV_RGB(255,0,0));
}

int main() {
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
image = imread("image.png");
imshow( window_name, image );
setMouseCallback( window_name, onMouse, 0 );
waitKey(0);
return 0;
}

这是打印鼠标指针坐标值的代码,同时悬停在图像上,在控制台上。如果我想在图像上打印类似的坐标,我应该怎么做?

最佳答案

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>

using namespace cv;

Mat img;

void onMouse(int event, int x, int y, int flags, void* param)
{
char text[100];
Mat img2, img3;

img2 = img.clone();

if (event == CV_EVENT_LBUTTONDOWN)
{
Vec3b p = img2.at<Vec3b>(y,x);
sprintf(text, "R=%d, G=%d, B=%d", p[2], p[1], p[0]);
}
else if (event == CV_EVENT_RBUTTONDOWN)
{
cvtColor(img, img3, CV_BGR2HSV);
Vec3b p = img3.at<Vec3b>(y,x);
sprintf(text, "H=%d, S=%d, V=%d", p[0], p[1], p[2]);
}
else
sprintf(text, "x=%d, y=%d", x, y);

putText(img2, text, Point(5,15), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0));
imshow("image", img2);
}

int main(int argc, char** argv)
{
img = imread(argc == 2 ? argv[1] : "lena.jpg");
if (img.empty())
return -1;

namedWindow("image");
setMouseCallback("image", onMouse, 0);
imshow("image", img);
waitKey(0);

return 0;
}

应该做的工作..

关于c++ - 使用 OpenCV 在图像上悬停时打印鼠标指针的像素坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19136474/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com