opencv在图像上显示移动鼠标对应的RGB像素值
作者:互联网
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
#include<vector>
using namespace std;
using namespace cv;
void mouseHandler(int event, int x, int y, int flags, void *p)
{
IplImage *img0, *img1;
img0 = (IplImage*)p;
img1 = cvCloneImage(img0);
CvFont font;
uchar *ptr;
char label[20];
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 0.8, 0.8, 0, 1, 8);
if (event == CV_EVENT_LBUTTONDOWN)
{
ptr = cvPtr2D(img1, y, x, NULL);
sprintf(label, "(%d, %d, %d)", ptr[0], ptr[1], ptr[2]);
cvRectangle(img1, cvPoint(x, y - 12), cvPoint(x + 100, y + 4),
CV_RGB(255, 0, 0), CV_FILLED, 8, 0);
cvPutText(img1, label, cvPoint(x, y), &font, CV_RGB(255, 255, 255));
cvShowImage("src", img1);
}
}
int main()
{
IplImage *src;
src = cvLoadImage("111.jpg");
assert(src);
cvNamedWindow("src", 1);
cvSetMouseCallback("src", mouseHandler, (void*)src);
mouseHandler(0, 0, 0, 0, src);
cvShowImage("src", src);
cvWaitKey(0);
return 0;
}
标签:src,int,ptr,像素,opencv,RGB,img1,include,CV 来源: https://blog.csdn.net/Vertira/article/details/123589303