Thursday, 31 March 2011

Reading Images Using C++

This had been a long way to perform Image processing using C++. In C++, it is not possible to even read an image without some libraries. The library for this is OpenCv.OpenCV (Open Source Computer Vision) is a library of programming functions for real time computer vision. In your C++ code , you need to include cv.h and highgui.h libraries. Here is the code for reading an image. Once reading is accomplished you can modify the code to do other image manipulations to your image. This is just the basic code:


#include<iostream>

#include<cv.h>

#include<highgui.h>

using namespace std;

int main(int argc,char **argv)

{

    IplImage *img = 0;

    unsigned char *data;

    int i,j,k;

    img=cvLoadImage(argv[1],-1);

    if(!img)

    {

        cout<<"Cannot Open File"<<endl;

    }

    /* Extracting the data members */

    int height    = img->height;

    int width     = img->width;

    int channels  = img->nChannels;

    int  step      = img->widthStep;

    int depth     = img->depth;

    data      = (unsigned char *)img->imageData;

    // creating the image pixels

    for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)

            data[i*step+j*channels+k]=data[i*step+j*channels+k];

    // defining the new image structure

    IplImage * new_img=cvCreateImage(cvSize(width,height),depth,channels);

    new_img->imageData=(char*)data;

    cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE);

    cvMoveWindow("Original Image", 100, 100);

    cvShowImage("Original Image", img);

    cvNamedWindow("Image Read", CV_WINDOW_AUTOSIZE);

    cvMoveWindow("Image Read", 550, 100);

    cvShowImage("Image Read", new_img);

    cvWaitKey(0);  

    cvDestroyWindow("Image Read");

    cvDestroyWindow("Original Image");

    return 0;

}



IplImage is a structure in OpenCv with the following data members :
|-- int  nChannels;     // Number of color channels (1,2,3,4)
|-- int depth; // Pixel depth in bits:
| // IPL_DEPTH_8U, IPL_DEPTH_8S,
| // IPL_DEPTH_16U,IPL_DEPTH_16S,
| // IPL_DEPTH_32S,IPL_DEPTH_32F,
| // IPL_DEPTH_64F
|-- int width; // image width in pixels
|-- int height; // image height in pixels
|-- char* imageData; // pointer to aligned image data
| // Note that color images are stored in BGR order
|-- int dataOrder; // 0 - interleaved color channels,
PL image:
| // 1 - separate color channels
| // cvCreateImage can only create interleaved images
|-- int origin; // 0 - top-left origin,
| // 1 - bottom-left origin (Windows bitmaps style)
|-- int widthStep; // size of aligned image row in bytes
|-- int imageSize; // image data size in bytes = height*widthStep
|-- struct _IplROI *roi;// image ROI. when not NULL specifies image
| // region to be processed.
|-- char *imageDataOrigin; // pointer to the unaligned origin of image data
| // (needed for correct image deallocation)
|
|-- int align; // Alignment of image rows: 4 or 8 byte alignment
| // OpenCV ignores this and uses widthStep instead
|-- char colorModel[4]; // Color model - ignored by OpenCV
 
cvCreateImage creates an image of width/height , using bit depth to represent the colour values, and with colour channels.
The depth is represented as
IPL_DEPTH_{U|S|F} where U, S and F stand for unsigned,signed and floating point. i.e.

IPL_DEPTH_8U
IPL_DEPTH_8S
IPL_DEPTH_16U
IPL_DEPTH_16S
IPL_DEPTH_32S
IPL_DEPTH_32F
IPL_DEPTH_64F


To compile this program ,you need to include some libraries as shown:


Opencv flags

The output of the above program is here :


Reading image in C++

2 comments:

  1. iam working on dev-c++ and its compiler so please how can i installl opencv library on it

    ReplyDelete
  2. @Emy first you need to install Opencv on your system.you can follow the link http://weissalissa.blogspot.com/2011/03/this-article-is-basically-for.html for installation.

    ReplyDelete