[英]How to extract face from an image?
我已經使用OpenCv
從背景中有其他東西的圖像中成功檢測出人臉。 現在,我只需要提取檢測到的部分(即臉部)並將其轉換為jpeg
或gif
這樣的圖像格式,以生成用於我的神經網絡訓練的臉部數據庫。
我怎樣才能做到這一點?
一旦檢測到臉部,就會獲得矩形的相對角,該矩形用於在臉部周圍繪制矩形。
現在,您可以設置圖像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
函數可用於將圖像保存到文件。
嘗試這個:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.