我已使用OpenCv
成功检测到背景中包含其他内容的图像中的脸部。
现在我需要仅提取检测到的部分(即面部)并将其转换为某种图像格式,如jpeg
或gif
,以使面部数据库用于我的神经网络训练。
我该怎么做?
答案 0 :(得分:4)
一旦检测到面部,就会得到一个矩形的对角,用于在脸部周围绘制矩形。
现在您可以设置图像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
功能可用于将图像保存到文件中。
答案 1 :(得分: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);
}