Runtime ROI Selection using Mouse
来源:互联网 发布:设计师导航源码 编辑:程序博客网 时间:2024/05/04 12:38
http://nashruddin.com/Runtime_ROI_Selection_using_Mouse
Listing 1: Runtime ROI Selection using Mouse
- #include <stdio.h>
- #include "cv.h"
- #include "highgui.h"
- IplImage* img0, * img1;
- CvPoint point;
- int drag = 0;
- void
- mouseHandler(int event, int x, int y, int flags, void* param)
- {
- /* user press left button */
- if (event == CV_EVENT_LBUTTONDOWN && !drag)
- {
- point = cvPoint(x, y);
- drag = 1;
- }
- /* user drag the mouse */
- if (event == CV_EVENT_MOUSEMOVE && drag)
- {
- img1 = cvClone(img0);
- cvRectangle(
- img1,
- point,
- cvPoint(x, y),
- CV_RGB(255, 0, 0),
- 1, 8, 0
- );
- cvShowImage("img", img1);
- }
- /* user release left button */
- if (event == CV_EVENT_LBUTTONUP && drag)
- {
- img1 = cvClone(img0);
- cvSetImageROI(
- img1,
- cvRect(
- point.x,
- point.y,
- x - point.x,
- y - point.y
- )
- );
- cvNot(img1, img1); // or do whatever with the ROI
- cvResetImageROI(img1);
- cvShowImage("img", img1);
- drag = 0;
- }
- /* user click right button: reset all */
- if (event == CV_EVENT_RBUTTONUP)
- {
- cvShowImage("img", img0);
- drag = 0;
- }
- }
- int
- main(int argc, char** argv)
- {
- if (argc != 2)
- {
- fprintf(stderr, "Usage: %s <image>\n", argv[0]);
- return 1;
- }
- img0 = cvLoadImage(argv[1], 1);
- cvNamedWindow("img", 1);
- cvSetMouseCallback("img", mouseHandler, NULL);
- cvShowImage("img", img0);
- cvWaitKey(0);
- cvDestroyWindow("img");
- cvReleaseImage(&img0);
- cvReleaseImage(&img1);
- return 0;
- }
Some final notes: you define the ROI by dragging the mouse from the upper left corner to the bottom right corner. Otherwise it generates errors. I will leave you to fix the bugs.
Update on April 14th, 2010:
Compiling the code above using C++ compiler generates error:
error C2440: '=' : cannot convert from 'void *' to 'IplImage *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
This is because cvClone()
at line 22 and 38 returns void*
while img1
is type of IplImage*
. To fix this bug, simply replace:
img1 = cvClone(img0)
with:
img1 = cvCloneImage(img0)
Thanks to sycluap for reporting this.
- Runtime ROI Selection using Mouse
- Mouse Selection & Highlighting
- Simple Mouse Trail using VB Script
- Using the Runtime APIs
- Mouse
- mouse
- mouse
- Mouse
- mouse
- Multiple selection using DropDownList in DataGrid
- OpenGL Selection Using Unique Color IDs
- Terrain Selection using a Mesh Decal
- ROI
- roi
- ROI
- Change the BLACK background of datawindow on column moving and mouse selection
- Change the BLACK background on datawindow on column moving and mouse selection to system color
- OGRE中级教程三 Mouse Picking(3D Object Selection) and SceneQuery Masks
- a far address VS a far pointer
- OpenCV Circular ROI
- 《花开半夏》--3 调查(2)
- ini_set用法
- 简单的socket连接,实现服务端可以获得多个客户端连接(备份,方便以后查询)
- Runtime ROI Selection using Mouse
- GPRS公网通讯
- 588创业网的网站备案通过了!
- 网络驱动具体干点什么?
- C++Builder 的一些高级编程技巧
- VirtualAlloc函数使用总结
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
- 手机移动视频监控系统方案
- 《花开半夏》--4 生死之间的吻(1)