9

Working on Face Detection and Recognition, and after successfully detecting a face, I just want to crop the face and save it somewhere in the drive to give it for the recognition code. I am having hard time doing the saving the Region of Interest as a new image. I have got some codes online but it is written in the previous version of OpenCV that uses IplImage*. I am using OpenCV 2.4.2 that uses cv::Mat.
Heeeelp!!!
I will post my codes(Face detection and Recognition per se) if you guys want it.

#include <cv.h>
#include <highgui.h>
#include <math.h>

// alphablend <imageA> <image B> <x> <y> <width> <height>
// <alpha> <beta>

IplImage* crop( IplImage* src,  CvRect roi)
{

  // Must have dimensions of output image
  IplImage* cropped = cvCreateImage( cvSize(roi.width,roi.height), src->depth, src->nChannels );

  // Say what the source region is
  cvSetImageROI( src, roi );

  // Do the copy
  cvCopy( src, cropped );
  cvResetImageROI( src );

  cvNamedWindow( "check", 1 );
  cvShowImage( "check", cropped );
  cvSaveImage ("style.jpg" , cropped);

  return cropped;
}

int main(int argc, char** argv)
{
  IplImage *src1, *src2;

  CvRect myRect;

  // IplImage* cropped ;
  src1=cvLoadImage(argv[1],1);
  src2=cvLoadImage(argv[2],1);

  {
    int x = atoi(argv[3]);
    int y = atoi(argv[4]);
    int width = atoi(argv[5]);
    int height = atoi(argv[6]);

    double alpha = (double)atof(argv[7]);
    double beta = (double)atof(argv[8]);

    cvSetImageROI(src1, cvRect(x,y,width,height));
    cvSetImageROI(src2, cvRect(100,200,width,height));

    myRect = cvRect(x,y,width,height) ;

    cvAddWeighted(src1, alpha, src2, beta,0.0,src1);

    cvResetImageROI(src1);
    crop (src1 , myRect);

    cvNamedWindow( "Alpha_blend", 1 );
    cvShowImage( "Alpha_blend", src1 );

    cvWaitKey(0);
  }

  return 0;
}

Thanks. Peace

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Sisay
  • 681
  • 7
  • 16
  • 31
  • 1
    In the opencv tutorial folder there is one code about face detection. Consider that one. You will have to use `cv::imwrite` to save the file. – Acorbe Apr 15 '13 at 11:57
  • Do you still have the code of recognition, cropping and detection? I have the exact same project! – Hassen Ch. May 05 '15 at 23:00

2 Answers2

25

Using cv::Mat objects will make your code substantially simpler. Assuming the detected face lies in a rectangle called faceRect of type cv::Rect, all you have to type to get a cropped version is:

cv::Mat originalImage;
cv::Rect faceRect;
cv::Mat croppedFaceImage;

croppedFaceImage = originalImage(faceRect).clone();

Or alternatively:

originalImage(faceRect).copyTo(croppedImage);

This creates a temporary cv::Matobject (without copying the data) from the rectangle that you provide. Then, the real data is copied to your new object via the clone or copy method.

sansuiso
  • 9,259
  • 1
  • 40
  • 58
  • I already did the cropping with IplImage*, what i want to do is to save all the cropped faces separately and as a new image on the hard drive of my computer.The aim of saving these images is to feed their path for another recognition code.The hardship for me here is, after detecting multiple faces, how to assign a particular name for each detected faces(using cvNamedWindow()) and then save these images, recalling and feed the path of these saved images to the recognition code. – Sisay Apr 17 '13 at 21:13
-2

For cropping the region, the ROI(Region of interest) is used. The opencv2 does the job quite easily. You can check the link: http://life2coding.blogspot.com/search/label/cropping%20of%20image

Hanif
  • 1