问题
I have to create a 4800 x 5 super matrix. This matrix will consist of 5 images of size 80 x 60 which I have already reshaped into matrices of 4800 x 1 using cvReshape. Therefore, I would now like to place these images next to the other to get a super matrix of dimensions 4800 x 5. How can I go about doing this using openCV? I have been trying all a long while now and this is due soon but I am no closer to achieving the creation of this matrix. I would really appreciate if someone could please help me. This is my code so far. However, it is not working, much less for giving the output I want.
#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, *img0_mat, *img1_mat, *col0, *col1, *superMat, *col0_mat, *col1_mat, *superMat_mat = NULL;
img0 = cvLoadImageM("C:\\small\\walk mii.jpg", CV_LOAD_IMAGE_UNCHANGED);
img1 = cvLoadImageM("C:\\small\\wave mii.jpg", CV_LOAD_IMAGE_UNCHANGED);
img0_mat = img0;
img1_mat = img1;//what does this do!!!
CvMat col0_header, col1_header, superMat_header, img0_header, img1_header;
col0 = cvReshape(img0_mat, &img0_header, 0, 4800);
col1 = cvReshape(img1_mat, &img1_header, 0, 4800);
col0_mat = col0;
col1_mat = col1;
superMat = cvCreateMat(4800, 2, CV_8UC1);
superMat_mat = superMat;
for(int i = 0; i < 2; i++)
{
cvGetCol(col0_mat, &col0_header, 1);
cvGetCol(superMat_mat, &superMat_header, 1);
cvCopy(col0_mat, superMat_mat);
}
cvDoubleMatPrint(superMat_mat);
cvWaitKey(0);
return 0;
}
回答1:
Just copy each image matrix row by row as illustrated in the code below:
cv::Mat superMat;
cv::Mat rowImg1, rowImg2, ..
superMat.create(5,rowImg.cols,rowImg1.type());
for (int i=0;i<5;i++){
rowImg1.copyTo(superMat.row(i));
}
回答2:
Create a new matrix of the same data type of size 4800x5 with cvCreateMat and load the 5 images into this "super matrix" row-by-row.
来源:https://stackoverflow.com/questions/9668951/creating-a-super-matrix-using-opencv