The code below was wrtting to access the elements of 3 RGB images converted to matrices and place them in a bigger matrix. However, I have know realised that CV_MAT_ELEM(mat, type, row, col) is used only for accessing elements of single channel matrices, whereas my images are all 3 channel. Would would I go about fixing this code then, to access elements of a 3 channel matrix instead of a single channel matrix?
#include "cv.h"
#include "highgui.h"
#include "iostream"
using namespace std;
void cvDoubleMatPrint (const CvMat* mat)
{
int i, j;
for (i = 0; i < mat->rows; i++)
{
for (j = 0 ; j < mat->cols; j++)
{
printf ( "%f ", cvGet2D (mat, i , j));
}
printf ( "\n" );
}
}
int main( int argc, char* argv )
{
CvMat *img0, *img1, *img2, *img0_mat, *img1_mat, *img2_mat, *col0, *col1, *col2, *superMat = NULL;
img0 = cvLoadImageM("C:\\small\\walk mii.jpg", CV_LOAD_IMAGE_UNCHANGED);
img1 = cvLoadImageM("C:\\small\\wave mii.jpg", CV_LOAD_IMAGE_UNCHANGED);
img2 = cvLoadImageM("C:\\small\\fantasy.jpg", CV_LOAD_IMAGE_UNCHANGED);
CvMat img0_header ,img1_header, img2_header;
col0 = cvReshape(img0, &img0_header, 0, 4800);
col1 = cvReshape(img1, &img1_header, 0, 4800);
col2 = cvReshape(img2, &img2_header, 0, 4800);
superMat = cvCreateMat(4800, 3, CV_8UC1);
cvSetZero(superMat);
for(int i=0; i<col0->height; i++)
{
CV_MAT_ELEM( *superMat, double, i, 0 ) = CV_MAT_ELEM( *col0, double, i, 0 );
}
for(int j=0; j<col1->height; j++)
{
CV_MAT_ELEM( *superMat, double, j, 1 ) = CV_MAT_ELEM( *col1, double, j, 0 );
}
for(int k=0; k<col2->height; k++)
{
CV_MAT_ELEM( *superMat, double, k, 2 ) = CV_MAT_ELEM( *col2, double, k, 0 );
}
cvDoubleMatPrint(superMat);
cvWaitKey(0);
return 0;