Out of memory opencv
I am getting started with OpenCV and C ++. I am trying to write PCA code for face recognition using 3 different faces. To do this, each image (size d = mxn) is transformed into a column vector of d elements.
typedef vector<Mat> Row;
Row img1v(36000,img1);
Row img2v(36000,img2);
Row img3v(36000,img3);
I am calculating the vector of average 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 am getting an error:
OpenCV error: Out of memory (unable to allocate 144004 bytes) in OutOfMemoryError
My image size is 180x200. I do not know what to do? Also how can I form a string vector in opencv using C ++? (To compute the covariance, I need to multiply the difference vector with its traspose).
source to share
I don't know OpenCV and its types. But yours typedef
looks suspicious.
I am guessing the error occurs when instantiating imgXv
Row
. For each call Row imgXv(36000,img1);
, a vector is created of 36,000 instances Mat
, which are copies of the instances imgX
. See Constructor 2) std::vector::vector
at cppreference.com :
vector( size_type count, const T& value, const Allocator& alloc = Allocator());
(2)2) Creates a container with countable copies of items with a value.
So, you are trying to store 108003 images in memory. Each of your images consists of 36,000 pixels. If each pixel is represented by at least 1 byte, this would take up at least 3.6 GB of memory.
From what I get from your approach, you don't want this, but rather typedef vector<float> Row;
andRow imgXv(36000);
source to share