Ask Your Question
0

How to load multiple images using videocapture and then store it in 1D matrices?

asked 2015-02-22 08:41:05 -0600

Biswesh gravatar image

updated 2015-02-22 08:44:18 -0600

berak gravatar image

I have tried to use the following code to do the above task: Can you say what did I do wrong?

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ml/ml.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
VideoCapture cap("/home/bapi/workspace/%03d.jpg");
int img_files= 2;
int img_area=250000;
int k=0;

Mat trainmat(img_files,img_area,CV_32FC1);

Mat image_mat;
Mat gray;

for(;;)
{
cap >> image_mat;
cvtColor(image_mat,gray,CV_BGR2GRAY);
if(gray.empty())

{
cout << "end of sequence" << std::endl;
return -1;
}

int ii=0;

for (int i=0; i<gray.rows; i++)

{
   for (int j=0; j<gray.cols; j++)
{
   trainmat.at<float>(k,ii++) = gray.at<uchar>(i,j);


}

}

k=k+1;
}
cout << trainmat << endl;

}
edit retag flag offensive close merge delete

Comments

int img_area=250000; // how did you get this number ?

berak gravatar imageberak ( 2015-02-22 08:47:49 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-02-22 08:53:51 -0600

berak gravatar image

updated 2015-02-22 09:00:00 -0600

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
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-02-22 08:41:05 -0600

Seen: 109 times

Last updated: Feb 22 '15