ios – 转换从CIFaceFeature返回的CGPoint结果

ios – 转换从CIFaceFeature返回的CGPoint结果,第1张

概述我试图弄清楚如何转换从CIFaceFeature返回的CGPoint结果,以便在CALayer中绘制它们.以前我已经将我的图像标准化为0旋转以使事情变得更容易,但这会导致使用横向模式下的设备拍摄的图像出现问题. 我一直在这方面工作一段时间没有成功,我不确定我对任务的理解是否不正确,或者我的方法是否不正确,或两者兼而有之.这是我认为正确的: 根据CIDetector featuresInImage: 我试图弄清楚如何转换从CIFaceFeature返回的CGPoint结果,以便在CALayer中绘制它们.以前我已经将我的图像标准化为0旋转以使事情变得更容易,但这会导致使用横向模式下的设备拍摄的图像出现问题.

我一直在这方面工作一段时间没有成功,我不确定我对任务的理解是否不正确,或者我的方法是否不正确,或两者兼而有之.这是我认为正确的:

根据CIDetector featuresInImage:options:方法的文档

A dictionary that specifIEs the orIEntation of the image. The detection is adjusted to account for the image orIEntation but the coordinates in the returned feature objects are based on those of the image.

在下面的代码中,我试图旋转CGPoint,以便通过覆盖UIImageVIEw的CAShape图层绘制它.

我正在做什么(……或者我认为我在做……)是将左眼CGPoint平移到视图的中心,旋转90度,然后将点转换回原来的位置.这不正确但我不知道我哪里出错了.我的方法是错误的还是我实施的方式?

#define degrees_TO_radians(angle) ((angle) / 180.0 * M_PI)

– leftEyeposition是一个CGPoint

CGAffinetransform  transRot = CGAffinetransformMakeRotation(degrees_TO_radians(90));float x = self.center.x;float y = self.center.y;CGAffinetransform tCenter = CGAffinetransformMakeTranslation(-x,-y);CGAffinetransform tOffset = CGAffinetransformMakeTranslation(x,y);leftEyeposition = CGPointApplyAffinetransform(leftEyeposition,tCenter);leftEyeposition = CGPointApplyAffinetransform(leftEyeposition,transRot);leftEyeposition = CGPointApplyAffinetransform(leftEyeposition,tOffset);

从这篇文章:https://stackoverflow.com/a/14491293/840992,我需要根据imageOrIEntation进行旋转

OrIEntation

Apple/UIImage.imageOrIEntation Jpeg/file
kCGImagePropertyOrIEntation

06003

消息由skinnyTOD在2013年2月1日下午4:09编辑

解决方法 我需要弄清楚完全相同的问题. Apple样本“SquareCam”直接在视频输出上运行,但我还需要来自UIImage的结果.所以我用一些转换方法扩展了CIFaceFeature类,以获得关于UIImage及其UIImageVIEw(或UIVIEw的CALayer)的正确点位置和边界.完整的实施发布在这里: https://gist.github.com/laoyang/5747004.您可以直接使用.

这是CIFaceFeature中一个点的最基本转换,返回的CGPoint根据图像的方向转换:

- (CGPoint) pointForImage:(UIImage*) image fromPoint:(CGPoint) originalPoint {    CGfloat imageWIDth = image.size.wIDth;    CGfloat imageHeight = image.size.height;    CGPoint convertedPoint;    switch (image.imageOrIEntation) {        case UIImageOrIEntationUp:            convertedPoint.x = originalPoint.x;            convertedPoint.y = imageHeight - originalPoint.y;            break;        case UIImageOrIEntationDown:            convertedPoint.x = imageWIDth - originalPoint.x;            convertedPoint.y = originalPoint.y;            break;        case UIImageOrIEntationleft:            convertedPoint.x = imageWIDth - originalPoint.y;            convertedPoint.y = imageHeight - originalPoint.x;            break;        case UIImageOrIEntationRight:            convertedPoint.x = originalPoint.y;            convertedPoint.y = originalPoint.x;            break;        case UIImageOrIEntationUpMirrored:            convertedPoint.x = imageWIDth - originalPoint.x;            convertedPoint.y = imageHeight - originalPoint.y;            break;        case UIImageOrIEntationDownMirrored:            convertedPoint.x = originalPoint.x;            convertedPoint.y = originalPoint.y;            break;        case UIImageOrIEntationleftMirrored:            convertedPoint.x = imageWIDth - originalPoint.y;            convertedPoint.y = originalPoint.x;            break;        case UIImageOrIEntationRightmirrored:            convertedPoint.x = originalPoint.y;            convertedPoint.y = imageHeight - originalPoint.x;            break;        default:            break;    }    return convertedPoint;}

以下是基于上述转换的类别方法:

// Get converted features with respect to the imageOrIEntation property- (CGPoint) leftEyepositionForImage:(UIImage *)image;- (CGPoint) rightEyepositionForImage:(UIImage *)image;- (CGPoint) mouthpositionForImage:(UIImage *)image;- (CGRect) boundsForImage:(UIImage *)image;// Get normalized features (0-1) with respect to the imageOrIEntation property- (CGPoint) normalizedleftEyepositionForImage:(UIImage *)image;- (CGPoint) normalizedRightEyepositionForImage:(UIImage *)image;- (CGPoint) normalizedMouthpositionForImage:(UIImage *)image;- (CGRect) normalizedBoundsForImage:(UIImage *)image;// Get feature location insIDe of a given UIVIEw size with respect to the imageOrIEntation property- (CGPoint) leftEyepositionForImage:(UIImage *)image inVIEw:(CGSize)vIEwSize;- (CGPoint) rightEyepositionForImage:(UIImage *)image inVIEw:(CGSize)vIEwSize;- (CGPoint) mouthpositionForImage:(UIImage *)image inVIEw:(CGSize)vIEwSize;- (CGRect) boundsForImage:(UIImage *)image inVIEw:(CGSize)vIEwSize;

(需要注意的另一件事是在从UIImage方向提取面部特征时指定正确的EXIF方向.相当令人困惑……这就是我所做的:

int exifOrIEntation;switch (self.image.imageOrIEntation) {    case UIImageOrIEntationUp:        exifOrIEntation = 1;        break;    case UIImageOrIEntationDown:        exifOrIEntation = 3;        break;    case UIImageOrIEntationleft:        exifOrIEntation = 8;        break;    case UIImageOrIEntationRight:        exifOrIEntation = 6;        break;    case UIImageOrIEntationUpMirrored:        exifOrIEntation = 2;        break;    case UIImageOrIEntationDownMirrored:        exifOrIEntation = 4;        break;    case UIImageOrIEntationleftMirrored:        exifOrIEntation = 5;        break;    case UIImageOrIEntationRightmirrored:        exifOrIEntation = 7;        break;    default:        break;}NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh };CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions];NSArray *features = [faceDetector featuresInImage:[CIImage imageWithCGImage:self.image.CGImage]                                          options:@{CIDetectorImageOrIEntation:[NSNumber numberWithInt:exifOrIEntation]}];

)

@H_419_74@ 总结

以上是内存溢出为你收集整理的ios – 转换从CIFaceFeature返回的CGPoint结果全部内容,希望文章能够帮你解决ios – 转换从CIFaceFeature返回的CGPoint结果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1064515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存