Sunteți pe pagina 1din 6

Systems Programming, Assignment 2

November 17, 2011

Appendix A- General Overview on digital images


Let us start with a quick overview of what is a gray level digital image. An image may be defined as a two-dimensional function I(x,y), where x and y are plane coordinates and the amplitude of I at any pair of coordinates (x,y) is called the intensity or gray level of the image at that point. When x,y, and the intensity values of f are all finite, discrete quantities, we call the image a digital image. Note that a digital image is composed of a finite number of elements, each of which has a particular location and value. These elements are called picture elements, or in short pixels. Usually pixels values pixel values are integers in the range 0-255. In our notation, an image is simply a two-dimensional matrix containing pixel values. An image of size (m+1) x (n+1) is given by: I(x,y) = I(0,0) I(0,1) I(1,0) I(1,1) I(m,0) I(0,n) I(1,n) I(m,n)

Systems Programming, Assignment 2


126 130 139 147 157 172 180 189 200 212 222 226 220 212 191 145 122 126 138 148 154 167 180 189 199 207 213 219 217 207 183 141 118 122 133 145 148 160 174 186 196 203 206 212 210 203 167 144 121 121 129 132 143 149 167 179 193 201 205 204 205 186 153 146 123 121 126 131 131 141 161 176 188 196 201 204 191 165 154 148 122 126 121 123 127 142 159 169 185 192 200 199 173 159 158 150 114 118 118 124 126 137 149 162 180 189 198 189 170 161 165 161 97 113 119 120 123 133 135 154 170 189 190 176 172 167 162 160 98 109 121 120 122 126 133 155 178 189 181 177 171 166 162 156 116 112 120 118 126 136 154 173 191 188 179 174 168 164 159 154 122 128 137 145 157 169 180 188 191 187 180 172 168 162 160 156 142 156 173 182 190 192 192 191 188 184 178 168 166 161 154 154

November 17, 2011


160 171 188 197 199 195 194 196 189 185 174 170 165 163 157 148 157 173 193 198 199 196 195 194 193 181 180 172 169 162 153 151 163 176 189 200 197 194 194 194 192 185 180 173 164 158 156 152 164 177 188 200 201 193 194 190 195 184 177 172 162 158 157 150

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.

Systems Programming, Assignment 2

November 17, 2011

Appendix B OpenCV Example


The following lines of code will read an image, and display it on screen.
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> int main(int argc, char **argv) { // read an image into an in-memory data structure cv::Mat image = cv::imread("Lenna.png"); // create an image window named "My image" cv::namedWindow("My image"); // show the image on window cv::imshow("My image", image); // wait for a key from the keyboard for 5000 ms cv::waitKey(5000); return 0; }

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);

Systems Programming, Assignment 2

November 17, 2011

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);

Compiling OpenCV Programs


In order to run your code with OpenCV, you need to tell the compiler where to find the header files (i.e., the files you include), and tell the linker where to find the shared libraries. We explain how to do so from the console, and in the Eclipse environment. You can read more about including header files, and linking with shared libraries in the following link.

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

This output depends on the specific installation directories of OpenCV

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.

Systems Programming, Assignment 2

November 17, 2011

Systems Programming, Assignment 2

November 17, 2011

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.

S-ar putea să vă placă și