0
votes

I have 3 images which I converted to matrices of dimensions 4800 x 1. I have to create a super matrix which is a matrix of dimensions 4800 x 3 by stacking these smaller matrices next to one another. i.e. 1st image would be 1st column in supermatrix, 2nd image would be 2nd column in supermatrix and 3rd image would be 3rd column in supermatrix. The output of my code shows a super matrix of the correct dimensions. However, I think the output is wrong because 2 of the 3 images in the matrix are mainly black and white so will have a lot of pixels of value 0. In the output, I see large pixel values which do not at all correspond to the loaded images. Below is the code and I have uploaded the 3 images I used in the code so you all will be able to see what I mean. I would really appreciate if someone could help me out with this please.

#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;

wavingcolourwalking}

1
How many channels do the images have? I'm guessing they're RGB, i.e. have 3 channels each.Robinson
@Robinson yes all 3 are RGB even though 2 appear to be grayscalesue-ling

1 Answers

0
votes

I'm not familiar with openCV but noticed a few potential issues.

  • When you call cvReshape you should have a column value of '1' instead of '0' (see this question for example). According to the documentation if the col/row is '0' then the number of rows in the image is left unchanged.
  • You are also creating supermat in the CV_8UC1 format which "means 8-bit single-channel matrix" according to the documentation but you are accessing it as a double. Similarly for accessing the input images.