问题描述
|
我正在开发用于检测人脸的openCV。我希望在检测到人脸后对其进行裁剪。直到现在我已经获得了人脸并在iPhone上标记了周围的矩形/椭圆形。
请帮我把脸剪成圆形/椭圆形
(UIImage *) opencvFaceDetect:(UIImage *)originalImage
{
cvSetErrMode(CV_ErrModeParent);
IplImage *image = [self CreateIplImageFromUIImage:originalImage];
// Scaling down
/*
Creates IPL image (header and data) ----------------cvCreateImage
CVAPI(IplImage*) cvCreateImage( CvSize size,int depth,int channels );
*/
IplImage *small_image = cvCreateImage(cvSize(image->width/2,image->height/2),IPL_DEPTH_8U,3);
/*SMOOTHES DOWN THYE GUASSIAN SURFACE--------:cvPyrDown*/
cvPyrDown(image,small_image,CV_GAUSSIAN_5x5);
int scale = 2;
// Load XML
Nsstring *path = [[NSBundle mainBundle] pathForResource:@\"haarcascade_frontalface_default\" ofType:@\"xml\"];
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad([path cStringUsingEncoding:NSASCIIStringEncoding],NULL,NULL);
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
NSLog(@\"ERROR: Could not load classifier cascade\\n\");
//return;
}
//Allocate the Memory storage
CvMemStorage* storage = cvCreateMemStorage(0);
// Clear the memory storage which was used before
cvClearMemStorage( storage );
CGColorSpaceRef colorSpace;
CGContextRef contextRef;
CGRect face_rect;
// Find whether the cascade is loaded,to find the faces. If yes,then:
if( cascade )
{
CvSeq* faces = cvHaarDetectObjects(small_image,cascade,storage,1.1f,3,cvSize(20,20));
cvReleaseImage(&small_image);
// Create canvas to show the results
CGImageRef imageRef = originalImage.CGImage;
colorSpace = CGColorSpaceCreateDeviceRGB();
contextRef = CGBitmapContextCreate(NULL,originalImage.size.width,originalImage.size.height,8,originalImage.size.width * 4,colorSpace,kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
//VIKAS
CGContextDrawImage(contextRef,CGRectMake(0,originalImage.size.height),imageRef);
CGContextSetlinewidth(contextRef,4);
CGContextSetRGBstrokeColor(contextRef,1.0,0.5);
// Draw results on the iamge:Draw all components of face in the form of small rectangles
// Loop the number of faces found.
for(int i = 0; i < faces->total; i++)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Calc the rect of faces
// Create a new rectangle for drawing the face
CvRect cvrect = *(CvRect*)cvGetSeqElem(faces,i);
// CGRect face_rect = CGContextConvertRectToDeviceSpace(contextRef,// CGRectMake(cvrect.x * scale,cvrect.y * scale,cvrect.width * scale,cvrect.height * scale));
face_rect = CGContextConvertRectToDeviceSpace(contextRef,CGRectMake(cvrect.x*scale,cvrect.y,cvrect.width*scale,cvrect.height*scale*1.25
));
facedetectapp=(FaceDetectAppDelegate *)[[UIApplication sharedApplication]delegate];
facedetectapp.grabcropcoordrect=face_rect;
NSLog(@\" FACE off %f %f %f %f\",facedetectapp.grabcropcoordrect.origin.x,facedetectapp.grabcropcoordrect.origin.y,facedetectapp.grabcropcoordrect.size.width,facedetectapp.grabcropcoordrect.size.height);
CGContextstrokeRect(contextRef,face_rect);
//CGContextFillEllipseInRect(contextRef,face_rect);
CGContextstrokeEllipseInRect(contextRef,face_rect);
[pool release];
}
}
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage],face_rect);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);
cvReleaseMemStorage(&storage);
cvReleaseHaarClassifierCascade(&cascade);
return returnImage;
}
}
谢谢
维卡斯
解决方法
有很多混合模式可供选择,其中一些对“遮罩”很有用。我相信这应该可以大致满足您的要求:
CGContextSaveGState(contextRef);
CGContextSetBlendMode(contextRef,kCGBlendModeDestinationIn);
CGContextFillEllipseInRect(contextRef,face_rect);
CGContextRestoreGState(contextRef);
\“大约\”,因为它每次都会掩盖整个上下文的内容,因此对一张以上的脸做错了。要处理这种情况,请在循环中使用CGContextAddEllipseInRect(),最后使用CGContextFillPath()。
您可能还需要查看CGContextBeginTransparencyLayerWithRect()。
, 以下是我在如何将UIImage裁剪为椭圆形或圆形时给出的答案?使图像转圈。这个对我有用..
从URL http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/下载支持存档文件
#import \"UIImage+RoundedCorner.h\"
#import \"UIImage+Resize.h\"
以下几行用于调整图像大小并转换为半径的圆角
UIImage *mask = [UIImage imageNamed:@\"mask.jpg\"];
mask = [mask resizedImage:CGSizeMake(47,47) interpolationQuality:kCGInterpolationHigh ];
mask = [mask roundedCornerImage:23.5 borderSize:1];
希望对您有所帮助。