opencv lections: 2. configuring visual studio. the first program in opencv

8
Lections on Image analysis, OpenCV 2. Configuring Visual Studio. The first program in OpenCV USU / IMM Fall 2011 www.uralvision.blogspot.com [email protected] http://www.lisperati.com/haskell/hello_world.png

Upload: denis-perevalov

Post on 05-Apr-2015

876 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

Lections on Image analysis, OpenCV

2. Configuring Visual Studio. The first program in OpenCV

USU / IMM Fall 2011www.uralvision.blogspot.com [email protected]

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

Page 2: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

1. Creating a ProjectAssume that OpenCV2.1 is already installed.

1. Run VS2008

2. Create a console projectFile - New - Project - Win32 Console Application,in the Name enter Project1, click OK.

3. Setup the pathsAlt + F7 - opens the project propertiesConfiguration 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 ofC:\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

Page 3: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

2. Reading the image and display iton the screen

1. Input:file http://www.fitseniors.org/wp-content/uploads/2008/04/green_apple.jpgwrite 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 diskimshow ("image", image); // Show imagewaitKey (0); // Wait for keystrokereturn 0;}

3. Press F7 - compilation, F5 - run.

The program will show the image and exit by pressing any key.

Page 4: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

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

Page 5: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

4. Allocation of channels Red, Blue, Greenreplace 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;}

Page 6: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

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 channelMat image2;threshold (channels [0], image2, 100, // Threshold255, // Max. value of the result, after converting the values are 0 and 255CV_THRESH_BINARY // Algorithm of binarization, (value> 100)? 255: 0;);imshow ("Thresholded", image2);

waitKey (0);return 0;}

Page 7: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

5. Work with rectangular subdomains imagereplace 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 pictureRect rect = Rect (100, 100, 200, 200); // Rectangle cutMat image3;image (rect). copyTo (image3); // Copy of the image imshow ("image3", image3);

// Change the part of the picture inside the pictureimage (rect) *= 2;imshow ("image changed", image);

waitKey (0);return 0;}

Page 8: OpenCV Lections: 2. Configuring Visual Studio. The first program in OpenCV

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 [email protected]