This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolourquery.cc
149 lines (92 loc) · 3.51 KB
/
colourquery.cc
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Example : query colour elements in an image
// usage: prog <image_name>
// Author : Toby Breckon, toby.breckon@cranfield.ac.uk
// Copyright (c) 2006 School of Engineering, Cranfield University
// License : LGPL - http://www.gnu.org/licenses/lgpl.html
#include "cv.h" // open cv general include file
#include "highgui.h" // open cv GUI include file
#include <stdio.h>
/******************************************************************************/
void colourQueryMouseCallBack(int event, int x, int y, int flags, void* img)
{
// get number of channels in image (3 for RGB / 1 for grayscale)
// (N.B. more efficient to access and store once rather than
// on every (!) event)
int imageChannels = ((IplImage*) img)->nChannels;
switch (event)
{
case CV_EVENT_LBUTTONDOWN :
// left button prints colour information at click location to stdout
printf("Colour information at image location (%i, %i) = ( ", x, y);
for(int i = 0; i < imageChannels; i++){
// here we use the CV_IMAGE_ELEM macro to access image elements
// N.B. An image is Row * Column * NChannels array
printf("%i ",
CV_IMAGE_ELEM((IplImage*) img, uchar, y, (x * imageChannels) + i));
}
printf(")\n");
;
break;
case CV_EVENT_RBUTTONDOWN :
// right button sets colour information at click location to white
printf("Colour information at image location (%i, %i) set to white\n"
, x, y);
for(int i = 0; i < imageChannels; i++){
// use CV_IMAGE_ELEM to access image elements
// set each channel to 255 (i.e. white)
CV_IMAGE_ELEM((IplImage*) img, uchar, y,
(x * imageChannels) + i) = 255;
}
;
break;
// defaults:
// all other events are ignored (other buttons,
// ;
// break;
}
}
/******************************************************************************/
int main( int argc, char** argv )
{
IplImage* img; // image object
char key;
bool keepProcessing = true;
char const * windowName = "OPENCV: colour query"; // window name
// check that command line arguments are provided and image reads in OK
if( argc == 2 && (img = cvLoadImage( argv[1], 1)) != 0 )
{
// create window object
cvNamedWindow(windowName, 1 );
// set function to be executed everytime the mouse is clicked/moved
cvSetMouseCallback(windowName, (CvMouseCallback) colourQueryMouseCallBack,
img);
// print out some helpful information about the image
printf("Image : (width x height) = (%i x %i)\n", img->width, img->height);
printf("\t Colour channels : %i\n", img->nChannels);
// loop so that events are processed and the image constantly redisplayed
// (N.B. A more efficient method is to only redisplay when an event has
// changed the image - this is left as an exercise)
while (keepProcessing){
// display image in window
cvShowImage( windowName, img );
// start event processing loop (very important,in fact essential for GUI)
key=cvWaitKey(20);
// get any keyboard input given by the user and process it
if (key == 'x'){
// if user presses "x" then exit
printf("Keyboard exit requested : exiting now - bye!\n");
keepProcessing = false;
}
}
// destroy window object
// (triggered by event loop *only* window is closed)
cvDestroyWindow( windowName );
// destroy image object
cvReleaseImage( &img );
// all OK : main returns 0
return 0;
}
// not OK : main returns -1
return -1;
}
/******************************************************************************/