2

我已经成功地从背景中使用OpenCv. 现在我需要只提取检测到的部分(即人脸)并将其转换为某种图像格式,例如jpeg创建gif一个人脸数据库用于我的神经网络训练。

我怎样才能做到这一点?

4

2 回答 2

5

检测到面部后,您会得到一个矩形的对角,该矩形用于在面部周围绘制矩形。

现在您可以设置图像 ROI(感兴趣区域),裁剪 ROI 并将其保存为另一张图像。

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

以上代码取自http://nashruddin.com/OpenCV_Region_of_Interest_(ROI )

进一步cvSaveImage的功能可用于将图像保存到文件中。

于 2012-02-03T17:21:48.410 回答
1

试试这个:

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);
}
于 2012-08-10T10:26:02.790 回答