Tag Archives: image

USING C++ CLASSES IN OPENCV

Authors:Abhishek Kumar Annamraju,AkashDeep Singh,Adhesh Shrivastava

Hi there,

We have been working till now with small basic opencv programs,so continuing with them I would like to add some tinge of classes of c++ to it.As you are well aware of the fact that class helps us to give a systematic path for presenting the code.

In the following code

1)I have defined two classes one to store an image in it and another class which gets that image from first class for further manipulation.

2)Also I have fetched some image properties in main,passed them to class store and used them in class grayimage.

So,any changes in main wont directly affect the images or data in classes.To make changes in them you need to call them(classes and its objects) personally.

Again the code is well commented for your understanding

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include<stdio.h>
using namespace cv;
using namespace std;

class store              
{
    public:             //Keep it public so that it can be used in other classes
        Mat image;
        int blue;
        int green;
        int red;



    public: 
    void getimage(Mat src) //storing image into a class variable named image
    {               

        if(! src.data )                              
        {
            cout <<  "src not filled" << endl ;
        }

        else
        {
            image = src.clone();
            cout << "got image" <<endl;
        }
    }

    void getdata(int bb,int gg,int rr)//storing data into class variables
    {
        blue =bb;
        green= gg;
        red=rr;
    }

    void showimage()//Display stored image using a class function
    {       
        if(! image.data )                              
        {
            cout <<  "Image not passed into this function" << std::endl ;

        }

        else
        {

            namedWindow( "Display window", WINDOW_AUTOSIZE );
            imshow( "Display window", image );
            cout << "image ready to get displayed" << endl;
        }   
    }

};  //first class ends here;

class grayimage   
//creating a class where i need to manipulate images and data obtained from another class
{

    private:
        Mat gray_image;
        int btrans,gtrans,rtrans;

    public:
        void setgray(Mat src) //converting the image to grayscale
        {
            if(! src.data )                              
            {
                cout <<  "src in second class not filled" << endl ;
            }

            else
            {
                cvtColor(src, gray_image, CV_BGR2GRAY );
                cout << "image conversion done" <<endl;
            }

        }

        void transferdata(int b,int g,int r)  //creating a transfer function
        {
            btrans = b;
            gtrans = g;
            rtrans = r;
        }       


        void showdata()
        {
            cout << "values of b,g,r before gray scale coversion are" << btrans << "," << gtrans << "," << rtrans << "," <<endl; 
        }

        void displaygray()
        {
            if(! gray_image.data )                              
            {
                cout <<  "Image not passed into this function" << std::endl ;

            }

            else
            {

                namedWindow( "gray image", WINDOW_AUTOSIZE );
                imshow( "gray image", gray_image );
                cout << "gray image ready to get displayed" << endl;
            }   
        }

};  

//main starts here

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat img;
    img = imread(argv[1], CV_LOAD_IMAGE_COLOR);    

    if(! img.data )                              
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    unsigned char *input = (unsigned char*)(img.data);
    int i,j,r,g,b;
    for(int i = 0;i < img.rows;i++)
    {
        for(int j = 0;j < img.cols;j++)
        {
                b = input[img.step * j + i ] ;
                g = input[img.step * j + i + 1];
                r = input[img.step * j + i + 2];
            }
    }

    store image1; //object to store image created
    image1.getimage(img);//image from main stored in object
    image1.getdata(b,g,r);//data from main stored in object
    image1.showimage();//displaying the stored image 

    grayimage image2;   //creating a manipulator object
    image2.setgray(image1.image); 
//fetches image from the first object that was stored and converts it into grayscale 
    image2.transferdata(image1.blue,image1.green,image1.red);
//fetches data from the first object that was stored 

    //This data transferred to the new object can be manupulated anytime later

    image2.showdata();
    image2.displaygray();          

    waitKey(0);                                         
    return 0;
}

This will help you to use classes in complex opencv c++ codes

Thank you 🙂

See also :- http://blindperception.wordpress.com/

Advertisements

CONTRAST AND BRIGHTNESS VARIATION OF IMAGES

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets see how do you change contrast and brightness in opencv

Open a c++ file named contrast.cpp and paste the following code in it

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.

**********************CODE STARTS HERE*******************************

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace cv;

double alpha; /**< Simple contrast control */
int beta;  /**< Simple brightness control */

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

Mat image = imread( argv[1] );
Mat new_image = Mat::zeros( image.size(), image.type() );

std::cout<<" Basic Linear Transforms "<<std::endl;
std::cout<<"-------------------------"<<std::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta;

for( int y = 0; y < image.rows; y++ )
{ for( int x = 0; x < image.cols; x++ )
{ for( int c = 0; c < 3; c++ )
{
new_image.at<Vec3b>(y,x)[c] =
saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );
}
}
}

namedWindow("Original Image", 1);
namedWindow("New Image", 1);

imshow("Original Image", image);
imshow("New Image", new_image);

return 0;
}

**************************CODE ENDS HERE*******************************

Compilation:

1. chmod +x contrast.cpp
2. g++ -ggdb `pkg-config --cflags opencv` -o `basename contrast.cpp .cpp` contrast.cpp `pkg-config --libs opencv`

Usage:

./contrast IMAGE.EXTENSION CONTRAST_INCREASE_VALUE BRIGHTNESS_ADDING_VALUE

(eg: – ./contrast cat.jpg 2 100)

OPENCV READ IMAGE AND VIDEO

AUTHORS:ABHISHEK KUMAR ANNAMRAJU,AKASH DEEP SINGH,ADHESH SHRIVASTAVA

Hi Friends,

Lets look into a way of opening image/video/camera with opencv

Reading an image:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

Click once somewhere on the code and press ctrl+A to select whole code.You may not see the whole code so its better to copy the code and paste it in your favourite text editor and then go through it.


#include &lt;highgui.h&gt;     //main header file

int main( int argc,char** argv)
{
IplImage* img = cvLoadImage(argv[1]);
//IplImage is an old way of reading,Mat can also be used

cvNamedWindow("example" ,CV_WINDOW_AUTOSIZE);
//naming a window to show image

cvShowImage("example,img);
//Tranferring the image read to the window

cvWaitKey(0);
//creating a waitkey to put up a stable window

cvReleaseImage( &amp;img);
cvDestroyWindow("example");
}


3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp

Note:FILENAME is the name given to the file,use the one you used

b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`
c)./FILENAME

Reading a video/camera input:-

1)Create a new  c++ file (eg-open_image.cpp )

2)Code(copy the code in the file):-

#include <highgui.h>

CvCapture* capture = NULL;
int main( int argc, char** argv )
{
cvNamedWindow( "example5", CV_WINDOW_AUTOSIZE );

if(argc == 1)
{
capture = cvCreateCameraCapture(0);
}
else
{
capture = cvCreateFileCapture(argv[1]);
}
assert(capture != NULL);
IplImage* frame;
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "example5", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "example5" );
}

3)Compiling and Executing:-
Open a terminal and change the directory to where the file is present and type

a)chmod +x FILENAME.cpp
Note:FILENAME is the name given to the file,use the one you used
b)g++ -ggdb `pkg-config --cflags opencv` -o `basename FILENAME.cpp .cpp` FILENAME.cpp `pkg-config --libs opencv`

c)./FILENAME (for video)

d)./FILENAME _____________ (for video,the video file name goes in the blank,eg:- ./FILENAME video.mkv and the video file should be in same folder of the cpp file otherwise whole path should me mentioned while execution eg:- ./FILENAME /home/USER/Desktop/video.mkv)

Thank you 🙂

See also :- http://blindperception.wordpress.com/