Insufficient memory opencv

834 views Asked by At

I am beginner to OpenCV and C++. I am trying to write PCA code for face recognition using 3 different faces. For this, each image(of size d=mxn) is reshaped to a column vector of d elements.

typedef vector<Mat> Row;
Row img1v(36000,img1);
Row img2v(36000,img2);
Row img3v(36000,img3);

I calculate the vector of mean of the images as follows:

Row meanImg;
 for(int i=0;i<img1v.size();++i)
{
  meanImg.push_back((img1v[i]+img2v[i]+img3v[i])/3);
}
cout<<meanImg.size()<<endl;

Here I get an error:

OpenCV Error: Insufficient memory (Failed to allocate 144004 bytes) in OutOfMemoryError

My image size is 180x200. I don't know what to do? Also how can I form a row vector in opencv using C++? (For calculating covariance I need to multiply difference vector with its traspose).

1

There are 1 answers

3
Peopleware On

I don't know OpenCV and its types. But your typedef looks suspicious.

My guess is that the error occurs while creating the imgXv instances of Row. For each call of Row imgXv(36000,img1); a vector is created that consists of 36000 instances of Mat that are all copies of the imgX instances. See constructor 2) of std::vector::vector in cppreference.com:

vector( size_type count, const T& value, const Allocator& alloc = Allocator()); (2)

2) Constructs the container with count copies of elements with value value.

So you`re trying to keep 108003 images in memory. Each of your images consists of 36000 pixels. If each pixel is represented by at least 1 byte, this would occupy a minimum of 3.6 GB of memory.

From what I get of your approach you don't want that, but rather a typedef vector<float> Row; and Row imgXv(36000);