简体   繁体   中英

Insufficient memory opencv

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).

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM