使⽤OpenCV从图像上读取像素坐标位置测试跟踪算法的时候,经常需要⼀个初始的跟踪框,⼿动标定这个位置就⽐较⿇烦了
可以采⽤Matlab或者VS来读取对应点的坐标位置
在这说⼀个OpenCV的⼩程序,可以显⽰点击的坐标位置
原理很简单:出发⿏标响应的函数读取了点击的位置,然后直接显⽰在点击位置的旁边
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
rectangle函数opencv
IplImage* src = 0;
IplImage* dst = 0;
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre_pt = {-1,-1};
static CvPoint cur_pt = {-1,-1};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
char temp[16];
if( event == CV_EVENT_LBUTTONDOWN )
{
cvCopy(dst,src);
sprintf(temp,"(%d,%d)",x,y);
pre_pt = cvPoint(x,y);
cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvShowImage( "src", src );
cvCopy(src,dst);
}
else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(dst,src);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvShowImage( "src", src );
}
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
{
cvCopy(dst,src);
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "src", src );
}
else if( event == CV_EVENT_LBUTTONUP )
{
sprintf(temp,"(%d,%d)",x,y);
cur_pt = cvPoint(x,y);
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
cvShowImage( "src", src );
cvCopy(src,dst);
}
}
}
int main()
{
src=cvLoadImage("lena.jpg",1);
dst=cvCloneImage(src);
cvNamedWindow("src",1);
cvSetMouseCallback( "src", on_mouse, 0 );
cvShowImage("src",src);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&src);
cvReleaseImage(&dst);
return 0;
}
效果图如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。