3
    UIImage* image = [UIImage imageNamed:@ "face.png" ];  
    UIImageView testImage = [[UIImageView alloc] initWithImage: image];  
    [testImage setTransform:CGAffineTransformMakeScale(1, -1)];  
    [[[UIApplication sharedApplication] delegate].window setTransform:  
CGAffineTransformMakeScale(1, -1)];  
    [testImage setFrame:CGRectMake(0, 0, testImage.image.size.width,  
testImage.image.size.height)];  
    [self.view addSubview:testImage];  

    CIImage* ciimage = [CIImage imageWithCGImage:imag​​e.CGImage];  
    NSDictionary* opts = [NSDictionary dictionaryWithObject:  
CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];  
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace  
context:nil options:opts];  
    NSArray* features = [detector featuresInImage:ciimage];  

    for  (CIFaceFeature *faceFeature in features)
    {  

        CGFloat faceWidth = faceFeature.bounds.size.width;  

        UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];                    

        faceView.layer.borderWidth = 1;  
        faceView.layer.borderColor = [[UIColor redColor] CGColor];  

        [self.view addSubview:faceView];          
    }  

我怎样才能找到面部坐标?

我尝试使用facefeature.bounds.origin.xfacefeature.bounds.origin.y

但有时,它不是正确的坐标

我怎样才能找到坐标?

------------------2016/04/10-----------------这是我的问题 ios x,y 是与c#相反

4

1 回答 1

2

这是它背后的基本思想,CIDetector 允许您从图像中推断出左眼、右眼和嘴巴的点。从中我们可以做一些基本的数学运算来创建一个跨越这些点的矩形,例如

for  (CIFaceFeature *faceFeature in features)
{
    CGPoint lefteye = faceFeature.leftEyePosition;
    CGPoint righteye = faceFeature.rightEyePosition;
    CGPoint mouth = faceFeature.mouthPosition;
    //Face Rectangle
    CGRect faceRectangle = CGRectMake(lefteye.x, lefteye.y, righteye.x - lefteye.x, mouth.y - righteye.y);
    //Face Center
    CGPoint faceCenter = CGPointMake(faceRectangle.origin.x + (faceRectangle.size.width / 2), faceRectangle.origin.y + (faceRectangle.size.height / 2));
    UIView* faceView = [[UIView alloc] initWithFrame:faceRectangle];

    faceView.layer.borderWidth = 1;
    faceView.layer.borderColor = [[UIColor redColor] CGColor];

    [self.view addSubview:faceView];          
}

请记住,我现在不是在计算机上为您测试这部分功能,但我相信探测器输出的坐标与输入图像的分辨率相符。当尝试使用 iOS 的点坐标系统将创建矩形应用到屏幕视图时,这会导致不准确。话虽如此,您所要做的就是通过convertRect函数运行新创建的矩形以获得正确的坐标。

于 2012-10-23T20:59:58.093 回答