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.
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 :)