I am using OpenCV to detect faces in several images and then I save the face images(rectangles) to jpg files. But as I have a lot of images, I get the OpenCV error that I am running out of memory. I know this is because of the use of IplImage
which doesn't release itself and I should use cv::Mat
instead, but I cannot find a solution for what I want to do with the use of cv::Mat
. Can anybody help please?
This is my code
int main (int argc, const char* argv[] )
{
cv::CascadeClassifier face_cascade;
face_cascade.load("haarcascade_frontalface_alt.xml");
cv::Mat captureFrame;
cv::Mat grayscaleFrame;
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\images\\")) != NULL) {
int counter = 1;
while ((ent = readdir (dir)) != NULL) {
printf ("%s\n", ent->d_name);
std::string fullPath = std::string("c:\\images\\") + ent->d_name;
captureFrame = cv::imread(fullPath);
cvtColor(captureFrame, grayscaleFrame, CV_BGR2GRAY);
equalizeHist(grayscaleFrame, grayscaleFrame);
std::vector<cv::Rect> faces;
face_cascade.detectMultiScale(grayscaleFrame, faces, 1.2,2,CV_HAAR_DO_CANNY_PRUNING, cv::Size(60, 60));
for(int i = 0; i < faces.size(); i++)
{
IplImage* img = cvCloneImage(&(IplImage)captureFrame);
cvSetImageROI(img, cvRect(faces.at(i).x, faces.at(i).y, faces.at(i).width, faces.at(i).height));
IplImage *img2 = cvCreateImage(cvGetSize(img),
img->depth,
img->nChannels);
cvCopy(img, img2, NULL);
cvResetImageROI(img);
std::stringstream sstm;
sstm << "faces\\" <<counter << ".jpg";
string result = sstm.str();
cvSaveImage (result.c_str() , img2);
counter++;
}
}
closedir (dir);
}
return 1;
}
good idea to get rid of the iplimages !
see, it gets much easier, too :
for(size_t i = 0; i < faces.size(); i++)
{
Mat roi(captureFrame, faces[i]);
std::stringstream sstm;
sstm << "faces\\" <<counter << ".jpg";
string result = sstm.str();
imwrite(result.c_str() , roi);
counter++;
}