【问题标题】:Coordinate system for CIFaceFeatureCIFaceFeature 的坐标系
【发布时间】:2013-08-08 21:07:32
【问题描述】:

我正在使用 CIFeature 类参考进行人脸检测,但我对 Core Graphics 坐标和常规 UIKit 坐标有点困惑。这是我的代码:

    UIImage *mainImage = [UIImage imageNamed:@"facedetectionpic.jpg"];

CIImage *image = [[CIImage alloc] initWithImage:mainImage];
NSDictionary *options = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:options];
NSArray *features = [detector featuresInImage:image];

CGRect faceRect;

for (CIFaceFeature *feature in features)
{
    faceRect= [feature bounds];

}

这很标准。现在根据官方documentation 说:

bounds 包含已发现特征的矩形。 (只读)

讨论矩形在图像的坐标系中。

当我直接输出 FaceRect 时,我得到:get rect {{136, 427}, {46, 46}}。当我应用 CGAffineTransfer 以正确的方式翻转它时,我得到看起来不正确的负坐标。我正在使用的图像位于 ImageView 中。

那么这些坐标在哪个坐标系中呢?图片?图像视图?核心图形坐标?正则坐标?

【问题讨论】:

    标签: iphone ios face-detection


    【解决方案1】:

    我终于明白了。正如文档所指出的,由 CIFaceFeature 绘制的矩形位于 图像的坐标系中。这意味着矩形具有原始图像的坐标。如果您选中了 Autoresize 选项,这意味着您的图像将按比例缩小以适合 UIImageView。所以你需要将旧的图像坐标转换为新的图像坐标。

    我改编自 here 的这段漂亮的代码可以为你做到这一点:

    - (CGPoint)convertPointFromImage:(CGPoint)imagePoint {
    
     CGPoint viewPoint = imagePoint;
    
     CGSize imageSize = self.setBody.image.size;
     CGSize viewSize  = self.setBody.bounds.size;
    
     CGFloat ratioX = viewSize.width / imageSize.width;
     CGFloat ratioY = viewSize.height / imageSize.height;
    
     UIViewContentMode contentMode = self.setBody.contentMode;
    
      if (contentMode == UIViewContentModeScaleAspectFit)
      {
         if (contentMode == UIViewContentModeScaleAspectFill)
         {
             CGFloat scale;
    
             if (contentMode == UIViewContentModeScaleAspectFit) {
                scale = MIN(ratioX, ratioY);
             }
             else /*if (contentMode == UIViewContentModeScaleAspectFill)*/ {
                scale = MAX(ratioX, ratioY);
            }
    
            viewPoint.x *= scale;
            viewPoint.y *= scale;
    
            viewPoint.x += (viewSize.width  - imageSize.width  * scale) / 2.0f;
            viewPoint.y += (viewSize.height - imageSize.height * scale) / 2.0f;
    
        }
     }
    return viewPoint;
    }
    

    【讨论】:

    • 我正在努力解决同样的问题。你能给我你的代码的完整链接吗?我不明白在哪里使用这种方法以及如何正确应用面部矩形。提前致谢。
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2012-07-16
    • 2014-10-20
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多