1 | initial version |
if you got enough memory, rather dynamically allocate your result: Mat trainmat; // leave empty
for (int i=0; i<numimages; ++i)="" {="" cap="" >>="" image_mat;="" if(image_mat.empty())="" if="" you="" check="" gray.empty(),="" it's="" too="" late="" already,="" because="" cvtcolor="" will="" crash.="" {="" cout="" <<="" "end="" of="" sequence"="" <<="" std::endl;="" return="" -1;="" }="" cvtcolor(image_mat,gray,cv_bgr2gray);<="" p="">
trainmat.push_back( gray.reshape(1,1) );
}
2 | No.2 Revision |
if you got enough memory, rather dynamically allocate your result:
result:
Mat trainmat; // leave emptyempty
for (int i=0; i<numimages; ++i)="" {="" cap="" >>="" image_mat;="" if(image_mat.empty())="" if="" you="" check="" gray.empty(),="" it's="" too="" late="" already,="" because="" cvtcolor="" will="" crash.="" {="" cout="" <<="" "end="" of="" sequence"="" <<="" std::endl;="" return="" -1;="" }="" cvtcolor(image_mat,gray,cv_bgr2gray);<="" p="">
i<numImages; ++i)
{
cap >> image_mat;
// if you check gray.empty(), it's too late already, because cvtColor will crash.
if(image_mat.empty()) {
cout << "end of sequence" << std::endl;
return -1;
}
cvtColor(image_mat,gray,CV_BGR2GRAY);
trainmat.push_back( gray.reshape(1,1) );); // flat 1d
}
}
3 | No.3 Revision |
if you got enough memory, rather dynamically allocate your result:
Mat trainmat; // leave empty
for (int i=0; i<numImages; ++i)
{
cap >> image_mat;
// if you check gray.empty(), it's too late already, because cvtColor will crash.
if(image_mat.empty())
{
cout << "end of sequence" << std::endl;
return -1;
}
cvtColor(image_mat,gray,CV_BGR2GRAY);
gray.convertTo(gray,CV_32F);
trainmat.push_back( gray.reshape(1,1) ); // flat 1d
}