OpenCV鼠标画图例程,鼠标绘制矩形
鼠标画矩形:
// An example program in which the
// user can draw boxes on the screen.
//
/* License:
Oct. 3, 2008
Right to use this code in any way you want without warrenty, support or any guarentee of it working. BOOK: It would be nice if you cited it:
Learning OpenCV: Computer Vision with the OpenCV Library
by Gary Bradski and Adrian Kaehler
Published by O'Reilly Media, October 3, 2008 AVAILABLE AT:
http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
Or: http://oreilly.com/catalog/9780596516130/
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130 OTHER OPENCV SITES:
* The source code is on sourceforge at:
http://sourceforge.net/projects/opencvlibrary/
* The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
http://opencvlibrary.sourceforge.net/
* An active user group is at:
http://tech.groups.yahoo.com/group/OpenCV/
* The minutes of weekly OpenCV development meetings are at:
http://pr.willowgarage.com/wiki/OpenCV
*/
#include <cv.h>
#include <highgui.h>
#pragma comment(lib,"opencv_core2410d.lib")
#pragma comment(lib,"opencv_highgui2410d.lib") // Define our callback which we will install for
// mouse events.
//
void my_mouse_callback( int event, int x, int y, int flags, void* param ); CvRect box;
bool drawing_box = false; // A litte subroutine to draw a box onto an image
//
void draw_box( IplImage* img, CvRect rect )
{
cvRectangle
(
img,
cvPoint(box.x,box.y),
cvPoint(box.x+box.width,box.y+box.height),
cvScalar(0xff,0x00,0x00) /* red */
);
} int main( int argc, char* argv[] )
{ box = cvRect(-1,-1,0,0); IplImage* image = cvCreateImage(
cvSize(200,200),
IPL_DEPTH_8U,
3
);
cvZero( image );
IplImage* temp = cvCloneImage( image ); cvNamedWindow( "Box Example" ); // Here is the crucial moment that we actually install
// the callback. Note that we set the value ‘param’ to
// be the image we are working with so that the callback
// will have the image to edit.
//
cvSetMouseCallback(
"Box Example",
my_mouse_callback,
(void*) image
); // The main program loop. Here we copy the working image
// to the ‘temp’ image, and if the user is drawing, then
// put the currently contemplated box onto that temp image.
// display the temp image, and wait 15ms for a keystroke,
// then repeat…
//
while( 1 ) { cvCopyImage( image, temp );
if( drawing_box ) draw_box( temp, box );
cvShowImage( "Box Example", temp ); if( cvWaitKey( 15 )==27 ) break;
} // Be tidy
//
cvReleaseImage( &image );
cvReleaseImage( &temp );
cvDestroyWindow( "Box Example" );
} // This is our mouse callback. If the user
// presses the left button, we start a box.
// when the user releases that button, then we
// add the box to the current image. When the
// mouse is dragged (with the button down) we
// resize the box.
//
void my_mouse_callback(
int event, int x, int y, int flags, void* param )
{ IplImage* image = (IplImage*) param; switch( event ) {
case CV_EVENT_MOUSEMOVE: {
if( drawing_box ) {
box.width = x-box.x;
box.height = y-box.y;
}
}
break;
case CV_EVENT_LBUTTONDOWN: {
drawing_box = true;
box = cvRect( x, y, 0, 0 );
}
break;
case CV_EVENT_LBUTTONUP: {
drawing_box = false;
if( box.width<0 ) {
box.x+=box.width;
box.width *=-1;
}
if( box.height<0 ) {
box.y+=box.height;
box.height*=-1;
}
draw_box( image, box );
}
break;
}
}
OpenCV鼠标画图例程,鼠标绘制矩形的更多相关文章
- 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域
这是两个相关的程序,前者是后者的基础.实际上前一个程序也是在前面博文的基础上做的修改,请参考<在OpenCV中利用鼠标绘制直线> .下面贴出代码. 程序之一,在OpenCV中利用鼠标绘制矩 ...
- OpenCV 学习笔记(2) 使用鼠标绘制矩形并截取和保存矩形区域图像
http://www.cnblogs.com/lidabo/p/3437587.html 0 效果展示 1工程源码 #include <opencv2/core/core.hpp> # ...
- opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像
前言 好长时间没写博文了,今天偷偷懒写篇关于opencv2中鼠标响应操作的文章. 鼠标操作属于用户接口设计,以前一直使用Qt来做,但是如果只需要简单的鼠标,键盘操作,直接调用opencv库的函数也未尝 ...
- 用canvas实现鼠标拖动绘制矩形框
需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.mi ...
- Opencv在视频中静态、动态方式绘制矩形框ROI
Opencv视频处理中的目标跟踪经常用到要在视频上画一个矩形框ROI,标注出要跟踪的物体,这里介绍两种在视频中绘制矩形框的方法,一种是"静态的",一种是"动态的" ...
- Opencv 学习笔记之——鼠标,进度条操作
Opencv中提供一个鼠标调用的函数,SetMouseCallback()函数,它配合一个回调函数来实现鼠标操作的功能. 首先看一下SetMouseCallback的函数原型: c++: void ...
- 原生js实现用鼠标画图
代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- opencv::轮廓周围绘制矩形框和圆形框
基于RDP算法实现,目的是减少多边形轮廓点数 approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool ...
- HTML5绘制矩形和圆形并且还有获取在这个图层内的坐标的思路和代码 - feilong_12的专栏 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
随机推荐
- 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别
http://www.cnblogs.com/AngelLee2009/p/3540527.html
- Android四大组件之一Service介绍-android学习之旅(十二)
基本概念: service是android四大组件之一,运行在后台执行耗时操作,并不提供用户界面.其他组件如acticity可以通过startService启动该组件,也可以通过bindService ...
- 剑指Offer——网易笔试之不要二——欧式距离的典型应用
剑指Offer--网易笔试之不要二--欧式距离的典型应用 前言 欧几里得度量(euclidean metric)(也称欧氏距离)是一个通常采用的距离定义,指在m维空间中两个点之间的真实距离,或者向量的 ...
- Unity插件 - MeshEditor(五) 网格顶点动画(变形动画)
源码已上传至github,并持续更新,链接请看底部.(本帖跟随github持续更新) 网格顶点动画(变形动画)是针对于物体的形状可以随意变换并记录为关键帧的动画,虽然模型的顶点数据还是应该交给GPU绘 ...
- 05 Android强制设置横屏或竖屏/全屏
全屏 在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 requestWindowFeature(Window.FEATURE_NO_TIT ...
- 从二进制数据流中构造GDAL可以读取的图像数据
在很多时候,我们的图像数据往往都不是文件方式存储在磁盘上,而是可能从网络或者数据库中获取的是二进制的图像数据流.最简单的方式和最容易想到的方式就是将这个文件流保存到磁盘上形成一个文件,然后再使用GDA ...
- Linux上程序调试的基石(2)--GDB
3. GDB的实现 GDB是GNU发布的一个强大的程序调试工具,用以调试C/C++程序.可以使程序员在程序运行的时候观察程序在内存/寄存器中的使用情况.它的实现也是基于ptrace系统调用来完成的. ...
- 【一天一道LeetCode】#344. Reverse String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- linux后台运行程序(secureCRT断掉或关闭后继续运行程序)
secureCRT断掉后想继续运行程序.也就是程序会在后台一直执行到结束. secureCRT通过ssh联接到client端运行程序,如果关掉窗口或者断开连接时,任务会被kill. 为了程序能够后台执 ...
- JAVA 继承基本类、抽象类、接口
Java是一个面向对象的语言,java面向对象一般有三大特征:封装.继承.多态. 封装:就是把一些属性和方法封装到一个类里. 继承:就如子类继承父类的一些属性和方法. 多态:就如一个父类有多个不同特色 ...