鼠标画矩形:

// 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鼠标画图例程,鼠标绘制矩形的更多相关文章

  1. 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域

    这是两个相关的程序,前者是后者的基础.实际上前一个程序也是在前面博文的基础上做的修改,请参考<在OpenCV中利用鼠标绘制直线> .下面贴出代码. 程序之一,在OpenCV中利用鼠标绘制矩 ...

  2. OpenCV 学习笔记(2) 使用鼠标绘制矩形并截取和保存矩形区域图像

    http://www.cnblogs.com/lidabo/p/3437587.html 0 效果展示 1工程源码 #include <opencv2/core/core.hpp>   # ...

  3. opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像

    前言 好长时间没写博文了,今天偷偷懒写篇关于opencv2中鼠标响应操作的文章. 鼠标操作属于用户接口设计,以前一直使用Qt来做,但是如果只需要简单的鼠标,键盘操作,直接调用opencv库的函数也未尝 ...

  4. 用canvas实现鼠标拖动绘制矩形框

    需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.mi ...

  5. Opencv在视频中静态、动态方式绘制矩形框ROI

    Opencv视频处理中的目标跟踪经常用到要在视频上画一个矩形框ROI,标注出要跟踪的物体,这里介绍两种在视频中绘制矩形框的方法,一种是"静态的",一种是"动态的" ...

  6. Opencv 学习笔记之——鼠标,进度条操作

    Opencv中提供一个鼠标调用的函数,SetMouseCallback()函数,它配合一个回调函数来实现鼠标操作的功能.   首先看一下SetMouseCallback的函数原型: c++: void ...

  7. 原生js实现用鼠标画图

    代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. opencv::轮廓周围绘制矩形框和圆形框

    基于RDP算法实现,目的是减少多边形轮廓点数 approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool ...

  9. HTML5绘制矩形和圆形并且还有获取在这个图层内的坐标的思路和代码 - feilong_12的专栏 - 博客频道 - CSDN.NET

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

随机推荐

  1. UE4联机多人游戏基本设置

    UE4自带网络联机功能,但是似乎只有蓝图接口,而真正写功能的时候不能用C++,让人感觉相当诡异 还是作一个简单记录 1.建一个第三人称模板,为什么会用他呢,因为它自带模板的很多组件,直接支持联机功能, ...

  2. (一一九)通过CALayer实现阴影、圆角、边框和3D变换

    在每个View上都有一个CALayer作为父图层,View的内容作为子层显示,通过layer的contents属性决定了要显示的内容,通过修改过layer的一些属性可以实现一些华丽的效果. [阴影和圆 ...

  3. ubuntu和mac OS X下另一种使用QQ的方法

    在ubuntu可以到pidgin官网下载http://www.pidgin.im,然后再安装插件 pidgin-lwqq即可,步骤为: sudo add-apt-repository ppa:lain ...

  4. Android中利用Camera与Matrix实现3D效果详解

    本文行文目录: 一.Camera与Matrix初步认识 二.Camera与Matrix旋转效果拆分介绍 三.Camera与Matrix实现立体3D切换效果 [csdn地址:http://blog.cs ...

  5. 所谓 Spinner 组件

    Spinner组件在平常的开发过程中应该算是比较常用的隶属于Input Control的android控件了,所以我们很有必要掌握这一个知识点,下面我就来谈一谈我对这个组件的一些认识. 是什么? 下拉 ...

  6. Sublime Text 3 使用MarkDown编写带预览的文本

    看到别人使用一个叫Markdown的标记语言来完成编码,心里就有点小激动,毕竟简短的几个符号,就可以写出如此精美的界面,实在是让人感到心旷神怡啊.于是我就在网上搜索了一些相关项的设置,于是便有了下面的 ...

  7. 《java入门第一季》之HashSet存储自定义对象问题以及注意事项

    上一篇http://blog.csdn.net/qq_32059827/article/details/51578158 写到存储字符串类型的时候出现了无序,而且这个无序不是随机那种无序,它是有一定存 ...

  8. 固定宽高的DIV绝对居中示例

    看了一些代码,然后自己试验了一番,分享如下示例: 实现点: 如果元素的宽高固定,那么,css指定样式为top:50%;left:50%; 而margin-top和 margin-left 指定为负数, ...

  9. SQL Server扫盲系列——镜像篇

    为方便查看,并以专题形式展示,所以我会把一些文章整合起来.本部分为SQL Server镜像系列: 本文出处:http://blog.csdn.net/dba_huangzj/article/detai ...

  10. 【UI 设计】PhotoShop基础工具 -- 移动工具

    还是学点美工的东西吧, 业余爱好   比学编程还难 PS版本 : PhotoShop CS6 1. 移动工具 (1) 工具栏和属性栏 工具栏 和 属性栏 : 左侧的是工具栏, 每选中一个工具, 在菜单 ...