Ask Your Question

Revision history [back]

click to hide/show revision 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="" &gt;&gt;="" image_mat;="" if(image_mat.empty())="" if="" you="" check="" gray.empty(),="" it's="" too="" late="" already,="" because="" cvtcolor="" will="" crash.="" {="" cout="" &lt;&lt;="" "end="" of="" sequence"="" &lt;&lt;="" std::endl;="" return="" -1;="" }="" cvtcolor(image_mat,gray,cv_bgr2gray);<="" p="">

trainmat.push_back( gray.reshape(1,1) );

}

if you got enough memory, rather dynamically allocate your result: result:

Mat  trainmat; // leave empty

empty

for (int i=0; i<numimages; ++i)="" {="" cap="" &gt;&gt;="" image_mat;="" if(image_mat.empty())="" if="" you="" check="" gray.empty(),="" it's="" too="" late="" already,="" because="" cvtcolor="" will="" crash.="" {="" cout="" &lt;&lt;="" "end="" of="" sequence"="" &lt;&lt;="" 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 }

}

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
}