How to convert IplImage to a matrix in OpenCV


Getting a digital image into numbers can be very handy for your image processing. In this post i will brief just that. I am a beginner in OpenCV and i found it very difficult to find material to do this. I was looking for storing a 8-bit image, 3 channel image into a 3D matrix. That's the method i am going to explain in this post.

Before i get on with the implementation, i would like to brief about the matrix structure that we are going to use. Its a 3D matrix; the row and column values represent the respective X and Y co-ordinates of the image. That is each value in the matrix represent a pixel. I am considering a 3 channeled image, so each pixel will have 3 channel values i.e for BGR. For more info on the matrix basic you can refer this link.

Following is the method I used..

First load a 8-Bit image with 3 channels RGB and create define a matrix structure accordingly.

          IplImage *img = cvLoadImage( "cataract.jpg");
    CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );

Next use the cvConvert function. cvConvert is a Macro provided by OpenCV which is the same as cvConvertScale() but is conventionally used when the scale and shift arguments will be let at their default values. 

           cvConvert( img, mat );

That does the work! Now your IplImage is stored into the matrix. You can retrieve the matrix values just by specifying the corresponding X,Y co-ordinate values in the cvGet2D function. I have used a for loop to print the RGB values of a small portion of the image.


        for(int i=0;i<10;i++)
        {
          for(int j=0;j<10;j++)
            {
               CvScalar scal = cvGet2D( mat,j,i);
               printf( "(%.f,%.f,%.f)  ",scal.val[0], scal.val[1], scal.val[2] );
            }
          printf("\n");
        }

cvScalar function is used to define a variable that stores the RBG value retrieved. You can check the OpenCV Reference links for some more tutorial links on OpenCV.

Please leave a comment so that know if this post is helpful or not. Also mention if any changes is required. It could help others :)



OpenCV Tutorial References


OpenCV is a well defined set of library function used in the field of Computer Vision.

It is a computer vision library originally developed by Intel but now supported by Willow Garage.The library is open source, cross platform mainly focusing on real time image processing.

Example applications of the OpenCV library are Human-Computer Interaction (HCI); Object Identification, Segmentation and Recognition; Face Recognition; Gesture Recognition; Motion Tracking, Ego Motion, Motion Understanding; Structure From Motion (SFM); Stereo and Multi-Camera Calibration and Depth Computation; Mobile Robotics. 

OpenCV library provides very well advanced functions for image storage, manipulation and object identification making things simpler. Eg: Just 2 simple functions cvHaarDetectObjects () and cvGetSeqElem () are sufficient to perform face detection!

I have went through many web links for my initial preparations on OpenCV. There are many tutorials out there, but i manage to pick a few of the better ones and thought of posting them here. I would brief on the web links so that you can choose your tutorial accordingly.

Web Links
  1.  Tutorial by Noah Kuntz
    • I found this OpenCV tutorial best for a beginner. He has touched upon all the details along with sample programs. 
    • He has categorized the tutorial into 11 segments and each segment is explained very well.
    • In case you are not through with C programming language there is a tutorial for that also.
    • Click here for the tutorial
     2.   Tutorial on Sourceforge.net
    • This tutorial makes use of Flowdesigner plug in along with OpenCV. You can download the plugin from here as its open source.
    • Tutorial provides a detailed explanation for all the basic library functions.
    • Best part is that the library functions are explained along with Image samples. That can really help in better understanding of the concept. 
    • Click here for the tutorial

Category: , 3 comments
Reactions: 

Recent Posts