How to extract face from an image?

浪尽此生 提交于 2019-12-04 18:13:35

Once you detect the faces, you get opposite corners of a rectangle, which is used to draw rectangles around the face.

Now you can set image ROI ( Region of Interest) , crop the ROI and save it as another image.

/* After detecting the rectangle points, Do as follows */     
/* sets the Region of Interest
   Note that the rectangle area has to be __INSIDE__ the image */
cvSetImageROI(img1, cvRect(10, 15, 150, 250));

/* create destination image
   Note that cvGetSize will return the width and the height of ROI */
IplImage *img2 = cvCreateImage(cvGetSize(img1),
                               img1->depth,
                               img1->nChannels);

/* copy subimage */
cvCopy(img1, img2, NULL);

/* always reset the Region of Interest */
cvResetImageROI(img1);

Above code is taken from http://nashruddin.com/OpenCV_Region_of_Interest_(ROI)

Further cvSaveImage function can be used to save image to a file.

try this:

for(i=0;i<(pFaceRectSeq?pFaceRectSeq->total:0);i++)
{
CvRect* r=(CvRect*)cvGetSeqElem(pFaceRectSeq,i);
int width=r->width;
int height=r->height;   
cvSetImageROI(pInpImg,*r);
IplImage* pCropImg=cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,3);
cvCopy(pInpImg,pCropImg,NULL);
cvShowImage("Cropped Window",pCropImg);
cvWaitKey(0);
cvResetImageROI(pInpImg);
cvReleaseImage(&pCropImg);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!