2014-03-04 33 views
1

我正在使用人臉檢測API,並想知道如何將座標從大型高分辨率圖像轉換爲顯示在UIImageView上的較小圖像。到目前爲止,我已經顛倒了我的圖像和容器視圖的座標系統,以便它與核心圖像座標系統相匹配,並且我還計算了高分辨率圖像和圖像視圖尺寸之間的高度比率,但是我得到的座標不準確。我假設我無法像我想的那樣輕鬆地將大圖像中的點轉換爲小圖像。任何人都可以請指出我的錯誤(S)?人臉檢測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設置爲更合理的方面,如方面適合。

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

最後要添加旋轉回。如果可能,嘗試避免這種情況,例如修復你的圖像,讓它們直立起來。