2

我正在使用 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];

}

这是相当标准的。现在根据官方文档,它说:

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

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

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

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

4

1 回答 1

3

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

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

- (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;
}
于 2013-08-09T10:46:27.640 回答