#include <cv.h>
#include <highgui.h>
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
const char *source_img="source image";//const就是用来修饰指针所指向的变量,即指针指向为常量;
IplImage *source,*source_copy;
IplImage *img0;
int pt_num=0;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
CvFont font;
uchar* ptr;
char label[30];
img0 = (IplImage*) param;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, .85, .85, 0, 1, 8);
ptr = cvPtr2D(img0, y, x, NULL);
sprintf(label, " (%d, %d, %d, %d, %d)",x,y, ptr[0], ptr[1], ptr[2]);
cvRectangle(
img0,
cvPoint(x+5, y - 13),
cvPoint(x + 200, y + 8),
CV_RGB(255, 255, 255),
CV_FILLED,
8, 0
);
cvPutText(
img0,
label,
cvPoint(x, y),
&font,
CV_RGB(0, 0, 0)
);
cvShowImage(source_img, img0);
}
}
int main(int argc,char *argv[])
{
IplImage *source=cvLoadImage("E:/3.jpg",1);
if (!source)
{
cout<<"can not load the image!"<<endl;
return -1;
}
source_copy=cvCreateImage(cvSize(source->width,source->height),8,3);
cvCopy(source,source_copy,0);
cvNamedWindow(source_img,0);
cvSetMouseCallback( source_img, mouseHandler,(void *)source_copy);
cvShowImage(source_img,source_copy);
cvWaitKey(0);
return 0;
}
评论0