Domanda

I want to do the following:

Lets say I have 100 images. I want to convert the matrix of each image to one vector and build a new matrix (of 100 rows), where each row is the vector that we got from the images. What is the best way to do that?

È stato utile?

Soluzione

Assume all of your image is of same size,

then using Mat::push_back() you can store each image row by row to a new Mat, but you need to reshape your source image to a single row using Mat::reshape(). Later you can access each row(will be your source image) using Mat::row but again you need to reshape to your source size.

See below example.

Load source image of same size and channel.

   Mat src1=imread("a.jpg",1);
   Mat src2=imread("b.jpg",1);
   Mat src3=imread("c.jpg",1);

Push back each source Mat to a new Mat row by row, here you need to reshape your source to a single row Mat, as each row in the result Mat should represent each source image.

   Mat A;
   A.push_back(src1.reshape(0,1)); //0 makes channel unchanged and 1 makes single row 
   A.push_back(src2.reshape(0,1));
   A.push_back(src3.reshape(0,1));

Later access each row from A, using A.row(index).clone() and reshape to original size,

   Mat B;
   B = A.row(0).clone();
   imshow("src1",B.reshape(0,src1.rows));

   B = A.row(1).clone();
   imshow("src2",B.reshape(0,src1.rows));

   B = A.row(2).clone();
   imshow("src3",B.reshape(0,src1.rows));

Altri suggerimenti

vector<Mat> images;
// fill it with data
Mat matVec(images.size(), images[0].cols*images[0].rows, images[0].type());
for(unsigned int i=0; i<images.size(); i++)
     (images[i].reshape(0,1)).copyTo(matVec.row(i));

This solution is based on solution that was proposed by Haris, but it is somewhat less time consuming.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top