You are on page 1of 8

Lections on Image analysis, OpenCV

2. Configuring Visual Studio.


The first program in OpenCV

http://www.lisperati.com/haskell/hello_world.png

www.uralvision.blogspot.com perevalovds@gmail.com
USU / IMM Fall 2011
1. Creating a Project
Assume that OpenCV2.1 is already installed.

1. Run VS2008

2. Create a console project


File - New - Project - Win32 Console Application,
in the Name enter Project1, click OK.

3. Setup the paths


Alt + F7 - opens the project properties
Configuration Properties - C / C + + - General - Additional Include Directories,
where we put the value "C:\Program Files\OpenCV2.1\include\opencv";

Linker - General - Additional Library Directories, where we put the value of


C:\Program Files\OpenCV2.1\lib\

Linker - Input - Additional Dependencies -


cv210.lib cvaux210.lib cxcore210.lib cxts210.lib highgui210.lib for Release,
cv210d.lib cvaux210d.lib cxcore210d.lib cxts210.lib highgui210d.lib for Debug
2. Reading the image and display it
on the screen
1. Input:
file http://www.fitseniors.org/wp-content/uploads/2008/04/green_apple.jpg
write in C: \ green_apple.jpg

2. In Project1.cpp:
# Include "stdafx.h"
# Include "cv.h"
# Include "highgui.h"
using namespace cv;

int main (int argc, const char ** argv)


{
Mat image = imread ("C: \ \ green_apple.jpg"); // Load image from disk
imshow ("image", image); // Show image
waitKey (0); // Wait for keystroke
return 0;
}

3. Press F7 - compilation, F5 - run.

The program will show the image and exit by pressing any key.
3. Linear operations on images
replace the text in the main from the previous example to:

int main (int argc, const char ** argv)


{
Mat image = imread ("C: \ \ green_apple.jpg");

// Image1 pixel by pixel is equal to 0.3 * image


Mat image1 = 0.3 * image;
imshow ("image", image);
imshow ("image1", image1);
waitKey (0);
return 0;
}
4. Allocation of channels
Red, Blue, Green
replace the text in the main from the previous example to:

int main (int argc, const char ** argv)


{
Mat image = imread ("C: \ \ green_apple.jpg");

// Split the original image into three channels


// - Channels [0], channels [1], channels [2]
vector <Mat> channels;
split (image, channels);

// Show the channels in separate windows


// Note that the red channel - 2, not 0.
imshow ("Red", channels [2]);
imshow ("Green", channels [1]);
imshow ("Blue", channels [0]);
waitKey (0);
return 0;
}
4. Threshold
Replace the text in the main from the previous example to:

int main (int argc, const char ** argv)


{
Mat image = imread ("C: \ \ green_apple.jpg");

// Split the original image into three channels


// - Channels [0], channels [1], channels [2]
vector <Mat> channels;
split (image, channels);

// Threshold the blue channel


Mat image2;
threshold (channels [0], image2,
100, // Threshold
255, // Max. value of the result, after converting the values are 0 and 255
CV_THRESH_BINARY // Algorithm of binarization, (value> 100)? 255: 0;
);
imshow ("Thresholded", image2);

waitKey (0);
return 0;
}
5. Work with rectangular subdomains
image
replace the text in the main from the previous example to:

int main (int argc, const char ** argv)


{
Mat image = imread ("C: \ \ green_apple.jpg");

// Cut of the picture


Rect rect = Rect (100, 100, 200, 200); // Rectangle cut
Mat image3;
image (rect). copyTo (image3); // Copy of the image
imshow ("image3", image3);

// Change the part of the picture inside the picture


image (rect) *= 2;
imshow ("image changed", image);

waitKey (0);
return 0;
}
Homework

1. Write a program that combines images of apple (image) and


orange (image_orange) by the rule

image3 = 0.5 * image + 0.5 * image_orange;

To do this, find a picture of orange, and make it the same size


as the apple picture in any graphics editor (Photoshop or
Paint).

2. Send the result (Project.cpp + get the picture) on the


perevalovds@gmail.com

You might also like