【问题标题】:UIImage face detectionUIImage人脸检测
【发布时间】:2012-03-14 06:42:47
【问题描述】:

我正在尝试编写一个例程,该例程采用 UIImage 并返回一个仅包含面部的新 UIImage。这似乎很简单,但我的大脑在处理 CoreImage 和 UIImage 空间时遇到了问题。

这里是基础:

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    return newImage;
}


-(UIImage *)getFaceImage:(UIImage *)picture {
  CIDetector  *detector = [CIDetector detectorOfType:CIDetectorTypeFace 
                                             context:nil 
                                             options:[NSDictionary dictionaryWithObject: CIDetectorAccuracyHigh forKey: CIDetectorAccuracy]];

  CIImage *ciImage = [CIImage imageWithCGImage: [picture CGImage]];
  NSArray *features = [detector featuresInImage:ciImage];

  // For simplicity, I'm grabbing the first one in this code sample,
  // and we can all pretend that the photo has one face for sure. :-)
  CIFaceFeature *faceFeature = [features objectAtIndex:0];

  return imageFromImage:picture inRect:faceFeature.bounds;
}

返回的图像来自翻转的图像。我尝试使用类似这样的方式调整faceFeature.bounds

CGAffineTransform t = CGAffineTransformMakeScale(1.0f,-1.0f);
CGRect newRect = CGRectApplyAffineTransform(faceFeature.bounds,t);

...但这给了我图像之外的结果。

我确信有一些简单的方法可以解决这个问题,但是没有计算自下而下然后使用它作为 X 创建一个新的矩形,有没有“正确”的方法来做到这一点?

谢谢!

【问题讨论】:

    标签: ios xcode core-graphics face-detection


    【解决方案1】:

    没有简单的方法来实现这一点,问题是来自 iPhone 相机的图像始终处于纵向模式,并且使用元数据设置来使它们正确显示。如果您事先告诉它图像的旋转,您还将在面部检测调用中获得更高的准确性。只是为了让事情变得复杂,您必须将图像方向以 EXIF 格式传递给它。

    幸运的是,有一个名为Squarecam 的苹果示例项目涵盖了所有这些,我建议您查看详细信息

    【讨论】:

    • 是的,我已经适应了图像的旋转。我的问题与 UIImage 和 CG 例程的不同来源有关。
    【解决方案2】:

    由于似乎没有一个简单的方法可以做到这一点,我只是写了一些代码来做到这一点:

    CGRect newBounds = CGRectMake(faceFeature.bounds.origin.x, 
                                  _picture.size.height - faceFeature.bounds.origin.y - largestFace.bounds.size.height, 
                                  faceFeature.bounds.size.width, 
                                  faceFeature.bounds.size.height);
    

    这很有魅力。

    【讨论】:

    • 我正在研究同样的事情,你能解释一下你对 y 轴执行的计算吗?什么是“最大脸”?
    【解决方案3】:

    只使用 CIContext 从图像中裁剪你的脸要容易得多,也不那么混乱。像这样的:

    CGImageRef cgImage = [_ciContext createCGImage:[CIImage imageWithCGImage:inputImage.CGImage] fromRect:faceFeature.bounds];
    UIImage *croppedFace = [UIImage imageWithCGImage:cgImage];
    

    inputImage 是您的 UIImage 对象,faceFeature 对象是您从 [CIDetector featuresInImage:] 方法获得的 CIFaceFeature 类型。 p>

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 2013-09-24
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 2013-07-16
      • 2019-02-27
      • 2021-09-09
      • 2011-08-19
      相关资源
      最近更新 更多