Ask Your Question
1

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

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

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

edit retag flag offensive close merge delete

Comments

2
  • 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 -0600 )edit
1

May be like this :

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

std::vector<float> array;
if (mg.isContinuous())
{
array.assign((float*)mg.ptr(0),(float*)(mg.ptr(mg.rows-1))+mg.cols);
cout <<array.size()<<"\n";
}
LBerger gravatar imageLBerger ( 2015-10-08 02:31:53 -0600 )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);

mat.convertTo(mat,CV_32F);

std::vector<float> array(mat.data);

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

Niessoh gravatar imageNiessoh ( 2015-10-08 02:56:53 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
4

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

thdrksdfthmn gravatar image

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

The constructor shall do it: std::vector< float > array(mat.data);. 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

Comments

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

Niessoh gravatar imageNiessoh ( 2015-10-08 02:58:41 -0600 )edit

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

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

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

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

It should be:

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

Comments

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 8,300 times

Last updated: Oct 08 '15