Iphone 人脸检测在调整大小的图像上不能正常工作,特别是在设备中,为什么?
下面是我用来从图像中检测人脸的代码:Iphone 人脸检测在调整大小的图像上不能正常工作,特别是在设备中,为什么?,iphone,ios,ipad,ios-simulator,face-detection,Iphone,Ios,Ipad,Ios Simulator,Face Detection,下面是我用来从图像中检测人脸的代码: - (void)detectFaces:(UIImageView *)photo { CIImage *coreImage = [CIImage imageWithCGImage:photo.image.CGImage]; CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context
- (void)detectFaces:(UIImageView *)photo
{
CIImage *coreImage = [CIImage imageWithCGImage:photo.image.CGImage];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
forKey:CIDetectorAccuracy]];
NSArray* features = [detector featuresInImage:coreImage];
for(CIFaceFeature* faceFeature in features)
{
NSLog(@"self.view %@",NSStringFromCGRect(self.view.frame));
NSLog(@"self.view %@",NSStringFromCGRect(self.view.bounds));
NSLog(@"self.vounds %@",NSStringFromCGRect(faceFeature.bounds));
CGFloat faceWidth = faceFeature.bounds.size.width;
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [[UIColor redColor] CGColor];
[self.view addSubview:faceView];
if(faceFeature.hasLeftEyePosition)
{
UIView* leftEyeView = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15, faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
[leftEyeView setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
[leftEyeView setCenter:faceFeature.leftEyePosition];
leftEyeView.layer.cornerRadius = faceWidth*0.15;
[self.view addSubview:leftEyeView];
}
if(faceFeature.hasRightEyePosition)
{
UIView* leftEye = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15, faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
[leftEye setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]];
[leftEye setCenter:faceFeature.rightEyePosition];
leftEye.layer.cornerRadius = faceWidth*0.15;
[self.view addSubview:leftEye];
}
if(faceFeature.hasMouthPosition)
{
UIView* mouth = [[UIView alloc] initWithFrame:CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2, faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)];
[mouth setBackgroundColor:[[UIColor greenColor] colorWithAlphaComponent:0.3]];
[mouth setCenter:faceFeature.mouthPosition];
mouth.layer.cornerRadius = faceWidth*0.2;
[self.view addSubview:mouth];
}
}
}
这是我用来调整图像大小的代码:
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
最后我调用detect face方法,如下所示:
UIImageView *inputImage = [[UIImageView alloc] initWithImage:[self imageWithImage:[UIImage imageNamed:@"facedetectionpic.jpg"] scaledToSize:CGSizeMake(320, 460)]];
[self.view addSubview:inputImage];
[inputImage setTransform:CGAffineTransformMakeScale(1, -1)];
[self.view setTransform:CGAffineTransformMakeScale(1, -1)];
[self performSelectorInBackground:@selector(detectFaces:) withObject:inputImage];
它在模拟器中正常工作,但在设备中不能正常工作。谁能帮我一下吗。
模拟器:
设备:
当我更改
UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0)中的选项时
toUIGraphicsBeginImageContextWithOptions(newSize,NO,1.0)代码>它甚至在设备中也开始工作。解决了问题。更改UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0)中的选项代码>到
UIGraphicsBeginImageContextWithOptions(newSize,NO,1.0)
它会起作用。我相信你必须在这个问题上使用convertRect
。即使使用convertRect,我也没有得到正确的位置。下面是我用来在屏幕上移动红色矩形的代码CGRect interactRect=[faceView CONVERTREC:[faceView frame]toView:self.view];[faceView setFrame:interactRect];