2014-03-04 2 views
1

저는 Face Detection API를 사용하고 있으며, 큰 고해상도 이미지의 좌표를 UIImageView에 표시된 작은 이미지로 변환하는 방법을 알고 싶습니다. 지금까지 이미지와 컨테이너 뷰의 좌표계를 반전시켜 코어 이미지 좌표계와 일치시키고 고해상도 이미지와 이미지보기의 크기 사이의 높이 비율을 계산했습니다. 내가 얻는 좌표는 정확하지 않습니다. 내가 생각하기에 큰 이미지에서 작은 이미지로 포인트를 쉽게 변환 할 수 없다고 가정하고 있습니다. 누구든지 내 실수를 지적 할 수 있습니까?얼굴 인식 ios7 좌표 배율 문제

[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

이미지보기에서 변환을 제거하면됩니다. 이미 이미지보기에 이미지가 이미 표시된 방향으로 표시되도록하십시오. 이렇게하면 계산이 훨씬 쉬워집니다.

이제 CIFaceFeature은 이미지 좌표로 기능을 출력합니다. 하지만 imageView는 더 작거나 클 수 있습니다. 먼저, 간단히 유지하고 imageView의 콘텐츠 모드를 왼쪽 상단으로 설정하십시오.

imageView.contentMode = UIViewContentModeTopLeft; 

이제 전혀 좌표를 확장 할 필요 없다.

만족 스러우면 contentMode를 aspect와 같은 더 합리적인 것으로 설정하십시오.

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); 

마지막으로 회전을 다시 추가하고 싶습니다. 가능한 경우이를 피하십시오. 그들이 처음부터 똑바로되도록 이미지를 수정하십시오.