人脸检测ios7协调缩放问题

时间:2014-03-04 19:55:32

标签: ios image uiimageview scale face-detection

我正在使用人脸检测API,想知道如何将大型高分辨率图像的坐标转换为UIImageView上显示的较小图像。到目前为止,我已经颠倒了我的图像和容器视图的坐标系统,以便它与Core Image坐标系匹配,我还计算了我的高分辨率图像和图像视图的尺寸之间的高度比,但是我得到的坐标根本不准确。我假设我无法像我想象的那样轻松地将大图像中的点转换为小图像。任何人都可以指出我的错误吗?

[self.shownImageViewer setTransform:CGAffineTransformMakeScale(1,-1)];
[self.view setTransform:CGAffineTransformMakeScale(1,-1)];

//240 x 320
self.shownImageViewer.image = self.imageToShow;

yscale = 320/self.imageToShow.size.height;
xscale = 240/self.imageToShow.size.width;
height = 320;

CIImage *image = [[CIImage alloc] initWithCGImage:[self.imageToShow CGImage]];

CIContext *faceDetectionContext = [CIContext contextWithOptions:nil];
CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:faceDetectionContext options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];

NSArray * features = [faceDetector featuresInImage:image options:@{CIDetectorImageOrientation:[NSNumber numberWithInt:6]}];

for(CIFaceFeature *feature in features)
{
    if(feature.hasLeftEyePosition)
        self.leftEye = feature.leftEyePosition;
    if(feature.hasRightEyePosition)
        self.rightEye = feature.rightEyePosition;
    if(feature.hasMouthPosition)
        self.mouth = feature.mouthPosition;
}

NSLog(@"%g and %g",xscale*self.rightEye.x, yscale*self.rightEye.y);
NSLog(@"%g and %g",yscale*self.leftEye.x, yscale*self.leftEye.y);
NSLog(@"%g",height);
self.rightEyeMarker.center = CGPointMake(xscale*self.rightEye.x,yscale*self.rightEye.y);
self.leftEyeMarker.center = CGPointMake(xscale*self.leftEye.x,yscale*self.leftEye.y);

1 个答案:

答案 0 :(得分:1)

我首先从图像视图中删除变换。只需让图像视图显示其中已定位的图像。这将使计算更容易。

现在CIFaceFeature在图像坐标中输出其特征。但您的imageView可能更小或更大。首先,保持简单并将imageView的内容模式设置为左上角。

imageView.contentMode = UIViewContentModeTopLeft;

现在你根本不需要缩放坐标。

如果您对此感到满意,请将contentMode设置为更合理的方式。

imageView.contentMode = UIViewContentModeScaleAspectFit;

现在您需要通过将每个坐标乘以宽高比拟合来缩放x和y坐标。

 CGFloat xRatio = imageView.frame.size.width / image.size.width;
 CGFloat yRatio = imageView.frame.size.height / image.size.height;
 CGFloat aspectFitRatio = MIN(xRatio, yRatio);

最后,您想要重新添加旋转。尽可能避免这种情况,例如:修复你的图像,使它们一开始就是正直的。