Saturday, 7 November 2015

Create a Gray out or Blur images from the color image using OpenCv in c++

In this post we are going to see how to create a gray out image or blur image from the colored image, To do this we are going to use the Open CV software which is a open source, initially developed by Intel,now supporting by willow garage.

Open CV is a c++ library which is used for real time as well as for offline image processing, and also it can process the Video's to handle objects present in the Videos as a Frames.

Now we start the solution, First we have to download the OpenCV , then extract the Zip,After extraction you can find a 2GB file.In my application i am Extracting the OpenCV to the following Path.

Open CV installed path :  J:\opencv

Now we have to setup the Environment and Visual Studio for our programming.

Environment Setup
1. Right Click Computer and select the properties.
2. Click the Advanced System Settings
3.In the Advanced Tab , select the Environment Variables




4. Now click the New button under the System variable section
5. Give Variable name as  : OPENCV_DIR
6. Give Variable Value as your open cv installed directory , now here i am referencing the vc12 folder
       J:\opencv\build\x86\vc12
7. Click Ok, Now System variable is added.




8.  Next we have to add some value to the Path system variable,
9. Select the Path System Variable and Click Edit.
10. Add the following value at last .  %OPENCV_DIR%\bin




 System Environment variable's are added.

Now we have to configure the visual studio solution.
1. Create a new Win32 console Application in visual studio.
2. Right click on the Project and click the properties
3. Select the General option under the C/C++ category , in that section we have to give value in Additional Include Directories 
           $(OPENCV_DIR)\..\..\Include




4. Above refers the include directory from OPEN CV path .
5. Next click the General under the linker section


6. Give the value for Additional Library Directories
        $(OPENCV_DIR)\lib







7.Select the Input item , in the Additional dependencies , drop down the control and select the Edit to add the libraries.
in the Additional Independence text box add the following values, after adding we can see the values in evaluated value
opencv_ts300.lib
opencv_ts300d.lib
opencv_world300.lib

opencv_world300d.lib






8. Now click ok and start programming.



Now we are going to load an image and process that image to get a Blur image , Gray out image and Canny image.

To store the Image we have to use the Mat Type which is a n-dimensional array class. and also refer some base libraries


#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"

Let we start the coding 


// SampleConsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"

#include <iostream>
using namespace std;
using namespace cv;

int main()
{
Mat imageOriginal;
Mat imageGrayScale;
Mat imageBlurred;
Mat imageCanny;

imageOriginal = imread("E:/image.jpg");

if (imageOriginal.empty()) {
cout << "Error in reading image";
return(0);
}
cvtColor(imageOriginal, imageGrayScale, CV_BGR2GRAY);
GaussianBlur(imageOriginal, imageBlurred, cv::Size(5, 5), 1, 5);
Canny(imageBlurred, imageCanny, 100, 200);

imshow("imgOriginal", imageOriginal);
imshow("imgGray", imageGrayScale);
imshow("canny", imageCanny);
imshow("blurred", imageBlurred);

//namedWindow("imgOriginal", CV_WINDOW_AUTOSIZE);
waitKey(0);

destroyWindow("imgOriginal");
destroyWindow("imgGray");
destroyWindow("canny");
destroyWindow("blurred");

return 0;

}



We have to create window to display the images 


Output:



 Original Image


                                                                                                 
                                                                                                        Gray





Canny






Blurred



We can do lot of stuff using the OpenCV, tracking the objects or persons in Real Time Video or Images, Counting the number of persons in the video, Identify the persons in the video or image etc ...


From the post you can learn some basics about how to convert the images in to various modes using Open CV



No comments:

Post a Comment