Documente Academic
Documente Profesional
Documente Cultură
A gray level image is given above, with the pixel values in the red rectangle given below. A color image is a two-dimensional matrix, where each pixel is composed of three values, the Red, Green and Blue values. A combination of these 3 basic colors displayed on a single screen location can produce any desired color.
The first thing to note is that we include the core and the highgui headers, which are responsible for the core functionality and the cross-platform GUI in OpenCV. Next we note, that all the data-types and functions in OpenCV reside in namespace cv. The first line of code creates an image by a call to the reading function imread.
cv::Mat image = cv::imread("Lenna.png");
Note that an image is stored as a matrix as explained above. Now we are ready to use this image. However, in the code you submit, you should first check if the image has been correctly read (an error will occur if the file is not found or if the file is corrupted, or is not a recognizable format). The validity of the image is tested by:
if (!image.data) { // no image has been created }
The member variable data is in fact a pointer to the allocated memory block that will contain the image data. It is simply set to 0 when no image has been read. The first thing we might want to do with this image is to display it. We do it using the highgui module provided by OpenCV. We start by declaring the window on which we want to display images, and then we specify the image to be shown on this special window:
// create image window named "My image" cv::namedWindow("My image"); // show the image on window cv::imshow("My image", image);
Since it is a console window that will terminate at the end of the main function, we add an extra highgui method to wait for a user key, or 5 seconds before ending the program:
// wait key for 5000 ms cv::waitKey(5000);
Console Compilation
The example code given above can be compiled with the following line: g++ -g -Wall -o test test.cpp `pkg-config opencv --cflags --libs` The command "pkg-config opencv --cflags libs" tells the compiler where to find the header files and shared libraries. If you run it in the console you should get something like the following flags. Note that in the Linux command line above, this command is enclosed in backquotes (``) backquotes have a special meaning in Linux shells: they indicate the command should be replaced by its output.
-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_c -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calect -lopencv_contrib lopencv_legacy -lopencv_flann
Eclipse Compilation
You create a new project as explained in practical sessions 1 and 4. Then you click on Project->Properties You should define the include directories and shared libraries as in the following images.
Note that the actual directories may be different on your machine. You should use the directories given by the "pkg-config opencv --cflags libs" command.