【问题标题】:Cropping out a face using CoreImage使用 CoreImage 裁剪人脸
【发布时间】:2013-02-09 17:56:21
【问题描述】:

我需要从给定图像中裁剪出一张脸/多张脸,并将裁剪后的人脸图像用于其他用途。我正在使用 CoreImage 的 CIDetectorTypeFace。问题是只包含检测到的面部的新 UIImage 需要更大的尺寸,因为头发被剪掉或下颚被剪掉。如何增加initWithFrame:faceFeature.bounds 的大小? 我正在使用的示例代码:

    CIImage* image = [CIImage imageWithCGImage:staticBG.image.CGImage];
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                          context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
    NSArray* features = [detector featuresInImage:image];

    for(CIFaceFeature* faceFeature in features)
    {
       UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
       faceView.layer.borderWidth = 1;
       faceView.layer.borderColor = [[UIColor redColor] CGColor];
       [staticBG addSubview:faceView];

       // cropping the face
       CGImageRef imageRef = CGImageCreateWithImageInRect([staticBG.image CGImage], faceFeature.bounds);
       [resultView setImage:[UIImage imageWithCGImage:imageRef]];
       CGImageRelease(imageRef);
    }

注意:我为显示检测到的面部区域而制作的红框与裁剪后的图像完全不匹配。也许我没有正确显示框架,但由于我不需要显示框架,我真的需要裁剪的脸,我不太担心。

【问题讨论】:

    标签: objective-c crop face-detection core-image


    【解决方案1】:

    不确定,但你可以试试

    CGRect biggerRectangle = CGRectInset(faceFeature.bounds, someNegativeCGFloatToIncreaseSizeForXAxis, someNegativeCGFloatToIncreaseSizeForYAxis);
    CGImageRef imageRef = CGImageCreateWithImageInRect([staticBG.image CGImage], biggerRectangle);
    

    https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html#//apple_ref/c/func/CGRectInset

    【讨论】:

    • 非常感谢...成功了!我还使用 CGRectOffset 使它更准确一些。 :)
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2020-09-27
    • 2018-05-08
    • 2012-10-25
    • 2022-01-07
    • 2019-08-27
    • 2018-03-07
    相关资源
    最近更新 更多