Я указал в моем блоге , что в учебнике что-то не так.
Деталь 5) Настройка системы координат : говорит, что вам нужно изменить координаты окна и изображения, но это не то, что вам следует делать. Вы не должны изменять свои представления / окна (в координатах UIKit), чтобы они соответствовали координатам CoreImage, как в учебном пособии, вы должны сделать наоборот.
Это часть кода, относящаяся к этому:
(Вы можете получить весь пример кода из моего сообщения в блоге или непосредственно из здесь . Он также содержит этот и другие примеры, использующие фильтры CIF: D)
// Create the image and detector
CIImage *image = [CIImage imageWithCGImage:imageView.image.CGImage];
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:...
options:...];
// CoreImage coordinate system origin is at the bottom left corner and UIKit's
// is at the top left corner. So we need to translate features positions before
// drawing them to screen. In order to do so we make an affine transform
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
transform = CGAffineTransformTranslate(transform,
0, -imageView.bounds.size.height);
// Get features from the image
NSArray *features = [detector featuresInImage:image];
for(CIFaceFeature* faceFeature in features) {
// Get the face rect: Convert CoreImage to UIKit coordinates
const CGRect faceRect = CGRectApplyAffineTransform(
faceFeature.bounds, transform);
// create a UIView using the bounds of the face
UIView *faceView = [[UIView alloc] initWithFrame:faceRect];
...
if(faceFeature.hasMouthPosition) {
// Get the mouth position translated to imageView UIKit coordinates
const CGPoint mouthPos = CGPointApplyAffineTransform(
faceFeature.mouthPosition, transform);
...
}
}
Как только вы получите положение рта (mouthPos
), вы просто кладете свою вещь на нее или рядом с ней.
Это определенное расстояние можно рассчитать экспериментально и должно быть относительно треугольника, образованного глазами и ртом. Я бы использовал много лиц, чтобы вычислить это расстояние, если это возможно (аватары в Twitter?)
Надеюсь, это поможет:)