Ask Your Question

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

asked 2015-10-08 02:00:28 -0500

coderx7 gravatar image

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());
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?

edit retag flag offensive close merge delete


  • you probably want to convert to grayscale, when reading: cv::imread(fileName, cv::IMREAD_GRAYSCALE)

  • imread() returns uchar images, you cannot just cast it to float and expect to get the pointer arithm right. also, try to avoid messing with internal datastart,dataend members.

  • why do you think, you need a std::vector<float> ? opencv's ml algorithms work on cv::Mat
  • Mat flat = img.reshape(1,1); is probably, what you want. maybe you need a convertTo(CV_32F), too.
berak gravatar imageberak ( 2015-10-08 02:19:03 -0500 )edit

May be like this :

Mat mg = Mat::zeros(40,2,CV_32FC1);
cout << mg.size() << "\n";

std::vector<float> array;
if (mg.isContinuous())
cout <<array.size()<<"\n";
LBerger gravatar imageLBerger ( 2015-10-08 02:31:53 -0500 )edit

@berak: I created the whole neural networks library, its not vectorized yet and I am experimenting the differences, thats why I am not allowed to use opencvs ml algorithms at the moment. By the way doing : cv::Mat mat = cv::imread(fileName, cv::IMREAD_GRAYSCALE);


std::vector<float> array(;

fails as well, complains about the conversion from uchar* to const std::allocator<_Ty>&.

coderx7 gravatar imagecoderx7 ( 2015-10-08 02:56:53 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2015-10-08 02:16:43 -0500

thdrksdfthmn gravatar image

updated 2015-10-08 02:17:03 -0500

The constructor shall do it: std::vector< float > array(;. But if your mat is not float, but uchar 3 channels, then you should transform it before: mat.convertTo(mat, CV_32F);

edit flag offensive delete link more


Thanks but the standard constructor fails with error message: cant convert argument 1 uchar* to const std::allocator<_Ty>& before that I tried to convert the image to grayslcale using cv::imread(fileName, cv::IMREAD_GRAYSCALE) So it doesnt compile now

coderx7 gravatar imagecoderx7 ( 2015-10-08 02:58:41 -0500 )edit

grayscale... yes, I missed it from the question

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-08 04:14:26 -0500 )edit

answered 2015-10-08 03:38:42 -0500

updated 2015-10-08 03:39:24 -0500

It should be:

cv::Mat mat = cv::imread(fileName, cv::IMREAD_GRAYSCALE);
std::vector<float> array((float*), (float*) + mat.rows * mat.cols);
edit flag offensive delete link more


This is in my opinion the correct solution :) Pleas people, if you are able to give the solution, do it by answers and not by comments :) Keeps the forum more organized!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-10-08 06:20:30 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-10-08 02:00:28 -0500

Seen: 3,842 times

Last updated: Oct 08 '15