由于缩放无法使用ImageView进行人脸检测和缩放?

达伦

再一次,我很接近,但是没有香蕉。

我正在尝试遵循一些有关面部识别的教程。几乎有以下代码,但我认为它在缩放和UIImageView在脸部周围放置边框方面缺少一些东西

我在“照片库”中的照片大小各异(出于某些无法解释的原因),因此我在想象中,他们CIDetector正在寻找面孔,正在应用CGAffineTransforms等,然后尝试将其放置在中UIImageView但是,从图像(也可以从下面看到)可以看出,它没有被绘制在正确的位置。

UIImageView尺寸为280x500,并设置为“填充比例”。

弄清楚正在发生的事情的任何帮助都会很棒!

-(void)detectFaces {

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *image = [CIImage imageWithCGImage:_imagePhotoChosen.image.CGImage options:nil];
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
    CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
    transform = CGAffineTransformTranslate(transform, 0, -_imagePhotoChosen.image.size.height);
    NSArray *features = [detector featuresInImage:image];
    NSLog(@"I have found %lu faces", (long unsigned)features.count);
    for (CIFaceFeature *faceFeature in features)
    {   
        const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform);
        NSLog(@"I have the original frame as: %@", NSStringFromCGRect(faceRect));
        const CGFloat scaleWidth = _imagePhotoChosen.frame.size.width/_imagePhotoChosen.image.size.width;
        const CGFloat scaleHeight = _imagePhotoChosen.frame.size.height/_imagePhotoChosen.image.size.height;

        CGRect faceFrame = CGRectMake(faceRect.origin.x * scaleWidth, faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);

        UIView *faceView = [[UIView alloc] initWithFrame:faceFrame];
        NSLog(@"I have the bounds as: %@", NSStringFromCGRect(faceFrame));
        faceView.layer.borderColor = [[UIColor redColor] CGColor];
        faceView.layer.borderWidth = 1.0f;

        [self.view addSubview:faceView];
    }

}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    _imagePhotoChosen.image = info[UIImagePickerControllerOriginalImage];
    //[_imagePhotoChosen sizeToFit];

    [self.view addSubview:_viewChosenPhoto];
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self detectFaces];

}

我一直在尝试解决数学错误的问题,所以我留在NSLog语句中,但是似乎看不出来是不是!而且我也是数学老师...

现在不是很正确吗?

再次感谢您为我指明正确方向所做的一切。

更新资料

响应人们想要知道我如何解决的问题……这对我来说确实是一个愚蠢的错误。

我正在将子视图添加到主窗口,而不是添加到UIImageView因此,我删除了这一行:

[self.view addSubview:faceView];

并替换为:

[_imagePhotoChosen addSubview:faceView];

这样就可以将框架放置在正确的位置。公认的解决方案为我提供了线索!因此,更新后的代码(从那时起,我继续前进了一点:

-(void)detectFaces:(UIImage *)selectedImage {

    _imagePhotoChosen.image = selectedImage;

    CIImage *image = [CIImage imageWithCGImage:selectedImage.CGImage options:nil];

    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
    CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
    transform = CGAffineTransformTranslate(transform, 0, -selectedImage.size.height);
    NSArray *features = [detector featuresInImage:image];
    int i = 0;
    for (CIFaceFeature *faceFeature in features)
    {

        const CGRect faceRect = CGRectApplyAffineTransform(faceFeature.bounds, transform);

        const CGFloat scaleWidth = _imagePhotoChosen.frame.size.width/_imagePhotoChosen.image.size.width;
        const CGFloat scaleHeight = _imagePhotoChosen.frame.size.height/_imagePhotoChosen.image.size.height;

        CGRect faceFrame = CGRectMake(faceRect.origin.x * scaleWidth, faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);

        UIView *faceView = [[UIView alloc] initWithFrame:faceFrame];
        faceView.layer.borderColor = [[UIColor redColor] CGColor];
        faceView.layer.borderWidth = 1.0f;
        faceView.tag = i;

        UITapGestureRecognizer *selectPhotoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPhoto)];
        selectPhotoTap.numberOfTapsRequired = 1;
        selectPhotoTap.numberOfTouchesRequired = 1;
        [faceView addGestureRecognizer:selectPhotoTap];

        [_imagePhotoChosen addSubview:faceView];
        i++;
    }

}
桑图

实际上,您所做的完全正确,只需将这一行替换为

    CGRect faceFrame = CGRectMake(_imagePhotoChosen.frame.origin.x+ faceRect.origin.x * scaleWidth,_imagePhotoChosen.frame.origin.y+ faceRect.origin.y * scaleHeight, faceRect.size.width * scaleWidth, faceRect.size.height * scaleHeight);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用opencv和python进行人脸检测

来自分类Dev

使用 Codename One 进行人脸检测

来自分类Dev

使用API14中的方法无法进行人脸检测

来自分类Dev

使用android.hardware.Camera2进行人脸检测

来自分类Dev

使用android.hardware.Camera2进行人脸检测

来自分类Dev

使用 Siamese Network 进行人脸检测演示

来自分类Dev

OpenCV和Python:使用haarcascades进行人脸检测可以检测到许多像眼睛一样的盒子。

来自分类Dev

使用欧几里德距离进行人脸识别

来自分类Dev

在OpenCV Python中使用Cascade分类器进行人脸检测

来自分类Dev

Caffe Net.Forward 尝试使用 OpenCV 进行人脸检测时出错

来自分类Dev

收集adaboost算法的负样本以进行人脸检测

来自分类Dev

使用opencv、tensorflow和python进行人体检测

来自分类Dev

使用神经网络进行人脸识别和分类未知数

来自分类Dev

UITableViewCell ImageView无法正确缩放?

来自分类Dev

在Matlab中使用计算机视觉工具箱(Viola Jones)进行人脸检测

来自分类Dev

使用setRotate重新缩放imageview

来自分类Dev

使用矩阵缩放Imageview

来自分类Dev

如何使用openbr对旋转的图像进行人脸识别?

来自分类Dev

使用PCA-matlab进行人脸识别

来自分类Dev

使用OpenCV进行人脸识别时出现属性错误

来自分类Dev

在Android中使用androidx Biometric API进行人脸验证

来自分类Dev

使用PCA-matlab进行人脸识别

来自分类Dev

使用IBM Watson Visual Recognition进行人脸识别

来自分类Dev

使用python在视频中进行人脸对齐

来自分类Dev

使用设备和AutoLayout iOS迅速进行DP和缩放

来自分类Dev

使用设备和AutoLayout iOS迅速进行DP和缩放

来自分类Dev

库,用于使用图像中人脸的视觉属性进行人脸识别

来自分类Dev

使用bxSlider进行平移缩放无法集中放大

来自分类Dev

如何训练阶段树分类器而不是OpenCV中的级联进行人脸检测?

Related 相关文章

  1. 1

    使用opencv和python进行人脸检测

  2. 2

    使用 Codename One 进行人脸检测

  3. 3

    使用API14中的方法无法进行人脸检测

  4. 4

    使用android.hardware.Camera2进行人脸检测

  5. 5

    使用android.hardware.Camera2进行人脸检测

  6. 6

    使用 Siamese Network 进行人脸检测演示

  7. 7

    OpenCV和Python:使用haarcascades进行人脸检测可以检测到许多像眼睛一样的盒子。

  8. 8

    使用欧几里德距离进行人脸识别

  9. 9

    在OpenCV Python中使用Cascade分类器进行人脸检测

  10. 10

    Caffe Net.Forward 尝试使用 OpenCV 进行人脸检测时出错

  11. 11

    收集adaboost算法的负样本以进行人脸检测

  12. 12

    使用opencv、tensorflow和python进行人体检测

  13. 13

    使用神经网络进行人脸识别和分类未知数

  14. 14

    UITableViewCell ImageView无法正确缩放?

  15. 15

    在Matlab中使用计算机视觉工具箱(Viola Jones)进行人脸检测

  16. 16

    使用setRotate重新缩放imageview

  17. 17

    使用矩阵缩放Imageview

  18. 18

    如何使用openbr对旋转的图像进行人脸识别?

  19. 19

    使用PCA-matlab进行人脸识别

  20. 20

    使用OpenCV进行人脸识别时出现属性错误

  21. 21

    在Android中使用androidx Biometric API进行人脸验证

  22. 22

    使用PCA-matlab进行人脸识别

  23. 23

    使用IBM Watson Visual Recognition进行人脸识别

  24. 24

    使用python在视频中进行人脸对齐

  25. 25

    使用设备和AutoLayout iOS迅速进行DP和缩放

  26. 26

    使用设备和AutoLayout iOS迅速进行DP和缩放

  27. 27

    库,用于使用图像中人脸的视觉属性进行人脸识别

  28. 28

    使用bxSlider进行平移缩放无法集中放大

  29. 29

    如何训练阶段树分类器而不是OpenCV中的级联进行人脸检测?

热门标签

归档