基于open显示鼠标点击处像素的坐标和颜色值

//这是一个可以调用的头文件
#pragma once
#include
#include
using namespace std;
#include
using namespace cv;

void myMouseCallback(int event, int x, int y, int, void* param) {
	CvFont word;
	cvInitFont(&word, CV_FONT_HERSHEY_SCRIPT_COMPLEX, 1, 1, 0, 1, 8);
	uchar * ptr;
	char * info=new char[30];
	
	IplImage* img= (IplImage *)param;
	IplImage * img_c = cvCloneImage(img);
	if (event == CV_EVENT_LBUTTONDOWN) {
        cvNamedWindow("ans",0);
        
		ptr = cvPtr2D(img_c, y, x);
		sprintf(info, "(%d,%d):(BGR) %d %d %d", x, y, ptr[0], ptr[1], ptr[2]);
		cvPutText(img_c, info, CvPoint(x, y), &word, CvScalar(0, 0, 255));
		cvShowImage("ans", img_c);
		
		cvWaitKey(0);
		
	}
	
}
void opencv_4_2() {
	IplImage * img = cvLoadImage("..//..//ym_1.bmp");
	cvNamedWindow("opencv_4_2", 0);
	cvSetMouseCallback("opencv_4_2", myMouseCallback, (void *)img);
    cvShowImage("opencv_4_2", img);
	cvWaitKey(0);
	cvDestroyWindow("opencv_4_2");
	cvReleaseImage(&img);
}

 

你可能感兴趣的:(opencv学习之路)