CvScalar clrPoint=cvScalar(255,0,0,0);
CvScalar clrText=cvScalar(255, 255, 255, 0);
if( event == CV_EVENT_MOUSEMOVE )
{
cvCopy(dst,src);
x=bound(x,0,src->width-1);
y=bound(y,0,src->height-1);
pt = cvPoint(x,y);
cvCircle( src, pt, 2,clrPoint ,CV_FILLED, CV_AA, 0 );
sprintf(temp,"%d (%d,%d)",n+1,x,y);
cvGetTextSize(temp,&font,&text_size,&baseline);
tmp_pt.x = bound(pt.x,0,src->width-text_size.width);
tmp_pt.y = bound(pt.y,text_size.height+baseline,src->height-1-baseline);
cvPutText(src,temp, tmp_pt, &font, clrText);
cvShowImage( "src", src );
}
else if( event == CV_EVENT_LBUTTONDOWN)
{
pt = cvPoint(x,y);
points.push_back(pt); n++;
cvCircle( src, pt, 2, clrPoint ,CV_FILLED, CV_AA, 0 );
sprintf(temp,"%d (%d,%d)",n,x,y);
cvGetTextSize(temp,&font,&text_size,&baseline);
tmp_pt.x = bound(pt.x,0,src->width-text_size.width);
tmp_pt.y = bound(pt.y,text_size.height+baseline,src->height-1-baseline);
cvPutText(src,temp, tmp_pt, &font, clrText);
cvCopy(src,dst);
cvShowImage( "src", src );
}
else if( event == CV_EVENT_RBUTTONDOWN )
{
if(!points.empty())
{
cvCopy(dst,src);
pt=points.back();
points.pop_back();
cvCircle( src, pt, 2, getInverseColor(clrPoint),CV_FILLED, CV_AA, 0 );
sprintf(temp,"%d (%d,%d)",n,pt.x,pt.y); --n;
cvGetTextSize(temp,&font,&text_size,&baseline);
tmp_pt.x = bound(pt.x,0,src->width-text_size.width);
tmp_pt.y = bound(pt.y,text_size.height+baseline,src->height-1-baseline);
cvPutText(src,temp, tmp_pt, &font, getInverseColor(clrText));
cvCopy(src,dst);
cvShowImage( "src", src );
}
}
}
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);
ofstream file("sample.txt");
if(!file)
{
cout << "open file error!";
return 1;
}
vector::iterator it=points.begin();
for(;it!=points.end();++it)
{