In first three posts of image processing I explained how we can use Open CV along with C language to display the image. I already stated that Open CV is not limited to C language. We can use C++ language as well with Open CV.
In C++ wrapper of Open CV images are treated as the two dimensional arrays. In other words they are treated as matrices. In this post I will explain how we can use C++ with Open CV.
Fallowing is the code snippet to achieve the above mentioned
#include "StdAfx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
class ImageProcessing
{
private:
string path;
Mat image;
int choice,color;
public:
ImageProcessing()
{
cout<<"Enter the path for the Image\n";
cin>>path;
cout<<"Choose from the fallowing\n";
cout<<"1. To load as color image\n";
cout<<"2.To load as gray scale image\n";
cout<<"3. To load the image as unchanged\n";
cin>>color;
cout<<"Select the behaviour of window\n";
cout<<"1. For WINDOW_NORMAL\n";
cout<<"2. For WINDOW_AUTOSIZE\n";
cout<<"3. For WINDOW_OPENGL\n";
cin>>choice;
}
void loadImage()
{
switch(color)
{
case 1:
image=imread(path,CV_LOAD_IMAGE_COLOR);
break;
case 2:
image=imread(path,CV_LOAD_IMAGE_GRAYSCALE);
break;
case 3:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
break;
default:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
}
}
void displayImage()
{
switch(choice)
{
case 1:
namedWindow("Image",WINDOW_NORMAL);
break;
case 2:
namedWindow("Image",WINDOW_AUTOSIZE);
break;
case 3:
namedWindow("Image",WINDOW_OPENGL);
break;
default :
namedWindow("Image",WINDOW_NORMAL);
}
imshow("Image",image);
cout<<"Press ESC key to close the window";
if(cvWaitKey(1000)==27)
{
destroyWindow("Image");
}
cout<<"Press any key to exit";
}
};
int main()
{
ImageProcessing *ip=new ImageProcessing();
ip->loadImage();
ip->displayImage();
return 0;
}
The code starts with name spaces. We imported two name spaces they are cv and std. cv is name space containing all the declarations for the Open CV. std contains all declarations for the C++. Next we have class having two methods and constructor. Constructor is invoked when the object for the class is created .
The code inside the constructor gets executed. This initiates the variables of the class with the values provided by the user.
The subsequent methods are useful for loading the image and displaying it. Now closely observe the loadimage method. It has one function from the Open CV imread. Imread is used for loading the image from the memory typically from the secondary memory it initialises the Mat type variables with the images the second parameter is to control the colour of the image.
It's turn for the displayimage method. The code is almost same as the C code the only change is we left the cv term before the method names. But all these methods are defined in the cv namespace.
If you ommit the using namespace cv; command the compiler generates the error saying that I am unable to locate the method.
In C++ wrapper of Open CV images are treated as the two dimensional arrays. In other words they are treated as matrices. In this post I will explain how we can use C++ with Open CV.
Fallowing is the code snippet to achieve the above mentioned
#include "StdAfx.h"
#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace cv;
using namespace std;
class ImageProcessing
{
private:
string path;
Mat image;
int choice,color;
public:
ImageProcessing()
{
cout<<"Enter the path for the Image\n";
cin>>path;
cout<<"Choose from the fallowing\n";
cout<<"1. To load as color image\n";
cout<<"2.To load as gray scale image\n";
cout<<"3. To load the image as unchanged\n";
cin>>color;
cout<<"Select the behaviour of window\n";
cout<<"1. For WINDOW_NORMAL\n";
cout<<"2. For WINDOW_AUTOSIZE\n";
cout<<"3. For WINDOW_OPENGL\n";
cin>>choice;
}
void loadImage()
{
switch(color)
{
case 1:
image=imread(path,CV_LOAD_IMAGE_COLOR);
break;
case 2:
image=imread(path,CV_LOAD_IMAGE_GRAYSCALE);
break;
case 3:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
break;
default:
image=imread(path,CV_LOAD_IMAGE_UNCHANGED);
}
}
void displayImage()
{
switch(choice)
{
case 1:
namedWindow("Image",WINDOW_NORMAL);
break;
case 2:
namedWindow("Image",WINDOW_AUTOSIZE);
break;
case 3:
namedWindow("Image",WINDOW_OPENGL);
break;
default :
namedWindow("Image",WINDOW_NORMAL);
}
imshow("Image",image);
cout<<"Press ESC key to close the window";
if(cvWaitKey(1000)==27)
{
destroyWindow("Image");
}
cout<<"Press any key to exit";
}
};
int main()
{
ImageProcessing *ip=new ImageProcessing();
ip->loadImage();
ip->displayImage();
return 0;
}
The code starts with name spaces. We imported two name spaces they are cv and std. cv is name space containing all the declarations for the Open CV. std contains all declarations for the C++. Next we have class having two methods and constructor. Constructor is invoked when the object for the class is created .
The code inside the constructor gets executed. This initiates the variables of the class with the values provided by the user.
The subsequent methods are useful for loading the image and displaying it. Now closely observe the loadimage method. It has one function from the Open CV imread. Imread is used for loading the image from the memory typically from the secondary memory it initialises the Mat type variables with the images the second parameter is to control the colour of the image.
It's turn for the displayimage method. The code is almost same as the C code the only change is we left the cv term before the method names. But all these methods are defined in the cv namespace.
If you ommit the using namespace cv; command the compiler generates the error saying that I am unable to locate the method.