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
|
// Example : query colour elements in an image
// usage: prog <image_name>
// Author : Toby Breckon, toby.breckon@cranfield.ac.uk
// Copyright (c) 2010 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 <iostream> // standard C++ I/O
using namespace cv; // OpenCV API is in the C++ "cv" namespace
/******************************************************************************/
void colourQueryMouseCallBack(int event, int x, int y, int flags, void* img)
{
int row = y; // y-axis is image rows (down the side)
int col = x; // x-axis is image columns (along the top)
switch (event)
{
case CV_EVENT_LBUTTONDOWN :
// left button prints colour information at click location to stdout
std::cout << "Colour information at image location (" << x << ","
<< y << "): ";
for(int channel = 0; channel < 3; channel++){ // three channels (B,G,R)
// note that variable img is now a pointer to the image object
// in this case (as it was passed by reference)
// IN GENERAL: pixel access is img.at<Vec3b>(row,col)[channel] for
// a 3 channel image (3 bytes per pixel) and img.at<uchar>(row,col) for
// a single channel image (1 byte per pixel)
std::cout <<
(unsigned int) ((Mat*) img)->at<Vec3b>(row,col)[channel] << " ";
}
std::cout << std::endl;
;
break;
case CV_EVENT_RBUTTONDOWN :
// right button sets colour information at click location to white
std::cout << "Colour information at image location (" << x << "," << y
<< ") set to white.";
for(int channel = 0; channel < 3; channel++){ // three channels (B,G,R)
((Mat*) img)->at<Vec3b>(row,col)[channel] = 255;
}
std::cout << std::endl;
;
break;
}
}
/******************************************************************************/
int main( int argc, char** argv )
{
Mat img; // image object
int key;
bool keepProcessing = true;
const string windowName = "OPENCV: colour query"; // window name
// check that command line arguments are provided and image reads in OK
if ((argc == 2) && !(img = imread( argv[1], CV_LOAD_IMAGE_COLOR)).empty())
{
// create window object
namedWindow(windowName, 0 );
// set function to be executed everytime the mouse is clicked/moved
// (note: this uses the older cvXXX function naming style from the
// OpenCV C interface)
cvSetMouseCallback("OPENCV: colour query",
(CvMouseCallback) colourQueryMouseCallBack, &img);
// print out some helpful information about the image
std::cout << "Image : (width x height) = (" << img.cols << " x "
<< img.rows << ")" << std::endl;
std::cout << " Colour channels = " << img.channels() << std::endl;
// loop so that events are processed and the image constantly redisplayed
while (keepProcessing){
// display image in window
imshow(windowName, img );
// start event processing loop (very important,in fact essential for GUI)
key=waitKey(20);
// get any keyboard input given by the user and process it
if (key == 'x'){
// if user presses "x" then exit
std::cout << "Keyboard exit requested : exiting now - bye!" << std::endl;
keepProcessing = false;
}
}
// all OK : main returns 0
return 0;
}
// not OK : main returns -1
return -1;
}
/***************************************************************************** |
Partager