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:image.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.x
和facefeature.bounds.origin.y
但有时候,它不是正确的坐标
我怎样才能找到坐标?
------------------ 2016年4月10日------------------ 这是我的问题ios x,y与c#
相反答案 0 :(得分: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
函数运行新创建的矩形以获得正确的坐标。