Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can I convert an image into a 1d vector in c++?

Hello everyone, I'm new to OpenCV , please bare with me. I am trying to train my network with different images (gray scaled) and for that I need to feed it using vectors of float values. (vector<float>). I searched and downloaded a database of images that were of type pgm and I need to load them as vectors.
I searched and came up with this code:

cv::Mat mat;
mat = cv::imread(fileName.toStdString().c_str());
cv::imshow(fileName.toStdString().c_str(),mat);
std::vector<float> array;
array.assign((float*)mat.datastart, (float*)mat.dataend);

The image dimensions are 19*19 (19 rows and 19 columns, verified it by rows and cols properties of mat), so the vector size should be 361, but it is 270! Is what I'm doing wrong here?