OpenCV之利用鼠标获取坐标 opencv 获取坐标

代码一:点击时显示坐标,鼠标移动时不显示。[cpp]#include #include #include IplImage* src=0; void on_mouse( int event, int x, int y, int flags, void* ustc){ CvFont font; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); if( event ==CV_EVENT_LBUTTONDOWN ) { CvPoint pt = cvPoint(x,y); char temp[16]; sprintf(temp,"(%d,%d)",pt.x,pt.y); cvPutText(src,temp, pt, &font, cvScalar(255,255, 255, 0)); cvCircle( src, pt, 2,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); cvShowImage( "src", src ); }} int main() { src=cvLoadImage("lena.jpg",1); cvNamedWindow("src",1); cvSetMouseCallback( "src",on_mouse, 0 ); cvShowImage("src",src); cvWaitKey(0); cvDestroyAllWindows(); cvReleaseImage(&src); return 0;} 代码二:鼠标移动时显示坐标,点击时不显示坐标[cpp]#include #include #include IplImage* src=0; IplImage* dst=0; void on_mouse( int event, int x, int y, int flags, void* ustc){ CvFont font; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); if( event ==CV_EVENT_MOUSEMOVE ) { cvCopy(dst,src); CvPoint pt = cvPoint(x,y); char temp[16]; sprintf(temp,"(%d,%d)",pt.x,pt.y); cvPutText(src,temp, pt, &font, cvScalar(255,255, 255, 0)); cvCircle( src, pt, 2,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); 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); return 0;} 代码三:鼠标移动和点击时均显示坐标[cpp] #include #include #include IplImage* src=0; IplImage* dst=0; void on_mouse( int event, int x, int y, int flags, void* ustc){ char temp[16]; CvPoint pt; CvFont font; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); if( event ==CV_EVENT_MOUSEMOVE ) { cvCopy(dst,src); sprintf(temp,"(%d,%d)",x,y); pt = cvPoint(x,y); cvPutText(src,temp, pt, &font, cvScalar(255,255, 255, 0)); cvCircle( src, pt, 2,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); cvShowImage( "src", src ); } else if( event ==CV_EVENT_LBUTTONDOWN ) { //cvCopy(dst,src); sprintf(temp,"(%d,%d)",x,y); pt = cvPoint(x,y); cvPutText(src,temp, pt, &font, cvScalar(255,255, 255, 0)); cvCircle( src, pt, 2,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); 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); return 0;} 代码四,上面的代码在边界处处理的不好,坐标值显示的不完全,下面是对代码三的改进。下面的代码还将点约束在图像的几何区域范围内。[cpp] #include #include #include #define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))//注意参数是有符号短整型,该函数的作用是使i限定为[a,b]区间内 int bound(short i,short a,short b) { returnmin(max(i,min(a,b)),max(a,b)); } IplImage* src=0; IplImage* dst=0; void on_mouse( int event, int x, int y, int flags, void* ustc){ char temp[16]; CvPoint pt; CvFont font; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); CvPoint tmp_pt = {-1,-1}; CvSize text_size; int baseline; 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,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); sprintf(temp,"(%d,%d)",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,cvScalar(255, 255, 255, 0)); cvShowImage( "src", src ); } else if( event ==CV_EVENT_LBUTTONDOWN ) { //cvCopy(dst,src); pt = cvPoint(x,y); cvCircle( src, pt, 2,cvScalar(255,0,0,0),CV_FILLED, CV_AA, 0 ); sprintf(temp,"(%d,%d)",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,cvScalar(255, 255, 255, 0)); 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); return 0;} 代码五,上面的代码不能撤销选定的坐标,下面的代码对此进行了改进,右击撤销最近选择的点,并用互补的颜色表示选择的点和撤销选定的点。除此之外还将选择的坐标输出到文件。在点的坐标前还有一个数表示点的选定次序。[cpp]#include #include #include #include #include using namespace std; #define max(a,b) (((a) > (b)) ? (a) : (b))#define min(a,b) (((a) < (b)) ? (a) : (b))//注意参数是有符号短整型,该函数的作用是使i限定为[a,b]区间内 int bound(short i,short a,short b) { returnmin(max(i,min(a,b)),max(a,b)); } CvScalar getInverseColor(CvScalar c) { CvScalar s; for(int i=0;i<=2;++i) { s.val[i]=255-c.val[i]; } return s;} IplImage* src=0; IplImage* dst=0; int n=0; vector points; void on_mouse( int event, int x, int y, int flags, void* ustc){ CvPoint pt; CvPoint tmp_pt = {-1,-1}; CvFont font; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX, 0.4, 0.4, 0, 1, CV_AA); char temp[16]; CvSize text_size; int baseline; CvScalarclrPoint=cvScalar(255,0,0,0); CvScalarclrText=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::iteratorit=points.begin(); for(;it!=points.end();++it) { file<<it->x<<','<<it->y<<endl; } file<<endl; file.close(); return 0;} 其的演示如下:sample.txt中的内容如下:[plain] 319,351 382,284 275,292 194,249 357,210 424,260 350,251 305,238 274,189 227,140 224,375

OpenCV之利用鼠标获取坐标 opencv 获取坐标

  

爱华网本文地址 » http://www.aihuau.com/a/25101010/37396.html

更多阅读

《侠盗飞车:罪恶都市》秘籍之无限钱获取秘籍

《侠盗飞车:罪恶都市》秘籍之无限钱获取秘籍——简介《侠盗飞车:罪恶都市》秘籍之无限钱获取秘籍《侠盗飞车:罪恶都市》秘籍之无限钱获取秘籍——工具/原料《侠盗飞车:罪恶都市》秘籍《侠盗飞车:罪恶都市》秘籍之无限钱获取秘籍——《

>连载系列二 推荐 百变小樱新连载第二话

<<论语与算盘>>  作者:涩泽荣一作者简介:涩泽荣一(1840~1931),近现代日本工商业发展影响最大的人之一,被誉为“日本商业之父”、“日本金融之王”、“日本资本主义之父”,甚至被推崇为“日本现代文明的创始人”,对日本近现代的经济现代化

PHP中array_slice的妙用 php array slice 多维

前几天我在做项目的时候,突然感觉有点看不到它调数据的源头,而且当时正在赶进度,于是我就想到了如果利用已经获取到的结果集进行分页。之后,我找到了array_slice函数。非常好用,分享一下:<?php//假定一个结果集二维数组: $arr =array(ar

余晓平和他的博客 余晓平 博联社

现在的大学生都喜欢上网,因此,2006年初我建了自己的博客,希望他们在上网的同时,不要忘记学习,学会如何利用网络获取更多的知识。建博后才发现,电子教案和习题答案中的普通化学方程式在这里都难以表达(无下角标),对于结构式等都需要用图片方式

声明:《OpenCV之利用鼠标获取坐标 opencv 获取坐标》为网友生命在继续分享!如侵犯到您的合法权益请联系我们删除