《学习openCV》第四章练习题4-6,仅供参考。
#include "cv.h"
#include"highgui.h"
CvRect rect;
bool isEdit=false;
CvPoint point;
void myMouseCallback(int even, int x, int y, int flags, void* param);
void main()
{
char ch[]="Rididck"; //初始文本标签
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0f, 1.0f, 0, 1, 0);
IplImage* srcImg=cvCreateImage(cvSize(800, 600), IPL_DEPTH_8U, 3 );
cvZero(srcImg);
cvNamedWindow("Image", 1);
cvShowImage("Image", srcImg);
cvSetMouseCallback("Image", myMouseCallback, srcImg);
while(1)
{
cvZero(srcImg); //每次都重置图像
if (isEdit)
{
if (cvWaitKey(10)==27)
{
isEdit=false;
break;
}
else
{
if (cvWaitKey(10)==13)
{
puts("Enter the text(no more 10 char):"); //按下enter后,提示用户在同样的位置输入新的标签
scanf("%s", ch);
}
cvPutText(srcImg, ch, point, &font, cvScalar(255,0, 0));
cvShowImage("Image", srcImg);
}
}
else
{
if (cvWaitKey(40)==27)
{
break;
}
}
}
}
void myMouseCallback(int even, int x, int y, int flags, void* param)
{
switch (even)
{
case CV_EVENT_LBUTTONDOWN:
isEdit=true;
point=cvPoint(x, y);
break;
default:
break;
}
}