-
Notifications
You must be signed in to change notification settings - Fork 25
/
drawBoxs.cpp
86 lines (68 loc) · 2.01 KB
/
drawBoxs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// drawBoxs.cpp
// LearningOpenCV
//
// Created by YourtionGuo on 7/28/16.
// Copyright © 2016 Yourtion. All rights reserved.
//
#include <iostream>
#include <highgui.h>
#include <cv.h>
void my_mous_callback( int evetnt, int x, int y, int flag, void* param );
CvRect box;
bool drawing_box = false;
void draw_box( IplImage* img, CvRect rect ) {
cvRectangle(
img ,
cvPoint(rect.x, rect.y),
cvPoint(rect.x+rect.width, rect.y+rect.height),
cvScalar(0x00, 0x00, 0xff) // Red
);
}
int main(int argc, const 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" );
cvSetMouseCallback( "Box Example", my_mous_callback, (void *) image );
while (1) {
cvCopyImage( image, temp );
if (drawing_box) draw_box( temp, box );
cvShowImage( "Box Example", temp );
if ( cvWaitKey(15) == 27) break;
}
cvReleaseImage( &image );
cvReleaseImage( &temp );
cvDestroyWindow( "Box Example" );
return 0;
}
void my_mous_callback( int evetnt, int x, int y, int flag, void* param ) {
IplImage* image = (IplImage *) param;
switch (evetnt) {
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;
default:
break;
}
}