繁体   English   中英

使用OpenCV面部检测在iOS中裁剪图像

[英]Cropping an image in iOS using OpenCV face detection

我使用以下代码在我的面部检测代码中从图像裁剪面部。 但我没有得到正确的脸部图像,我得到的图像的一部分保存而不是脸部。 我的代码有什么问题?

_faceCascade.detectMultiScale(mat, faces, 1.1, 2, kHaarOptions, cv::Size(40, 40));

在displayfaces函数内部使用以下代码进行裁剪:

CGRect cropRect = CGRectMake(faces[i].x, faces[i].y, faces[i].width, faces[i].width);
CGImageRef cropped_img = CGImageCreateWithImageInRect(self.storeImage.CGImage, cropRect);
UIImage *img = [UIImage imageWithCGImage:cropped_img];
UIImageWriteToSavedPhotosAlbum( img, self,  nil,nil);

我得到了正确的faces[i]坐标faces[i] 但问题只在于裁剪和设定投资回报率。 有人可以帮我解决吗?

我也尝试使用下面的代码,再次获得相同的图像。 (即,我没有得到实际的脸部图像)

cv :: Mat image_roi;
cv::Rect roi(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
cv::Mat(testMat, roi).copyTo(image_roi);
UIImage *img = [CaptureViewController imageWithCVMat:image_roi ];
UIImageWriteToSavedPhotosAlbum( img, self,  nil,nil);

注意:我正在使用opencv facedetect检测实时视频流中的人脸。 我可以在脸部周围看到绿色矩形。 但我不能用脸部参数裁剪脸部。 我也尝试过设置faceroi以检测眼睛,即使失败也是如此。 要缩小问题范围,问题可能在于为图像设置ROI?

更新于2013年2月11日:

我有完成裁剪如下,但ROI设置不正确,图像没有很好地裁剪:

我在上面的帖子中发现了这个问题(感谢@Jameo指出我因为旋转问题而导致它。)我已经旋转了图像,如下所示。

UIImage *rotateImage = [[UIImage alloc] initWithCGImage: image.CGImage
                               scale: 1.0
                         orientation: UIImageOrientationRight];

并使用以下代码裁剪图像:

// get sub image
+ (UIImage*) getSubImageFrom: (UIImage*) img WithRect: (CGRect) rect
{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // translated rectangle for drawing sub image
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height);
    // clip to the bounds of the image context
    // not strictly necessary as it will get clipped anyway?
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
    // draw image
    [img drawInRect:drawRect];
    // grab image
    UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return subImage;
}

图像被裁剪但不是实际的坐标。

我的观察: 1)使用Affinetransform返回的FaceRect裁剪图像。 这是错误坐标的原因还是因为我的代码中存在错误? 2)在设置仿射变换之前,我无法为图像设置ROI,原因是什么,这是设置ROI的过程吗?

faceRect = CGRectApplyAffineTransform(faceRect, t);

但仍然没有正确完成裁剪差异如下所示:

完整图片:

完整图像

裁剪图像

在此输入图像描述

那这个呢?

- (UIImage *) getSubImageFrom:(UIImage *)imageToCrop WithRect:(CGRect)rect {

    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);    
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;    
}

试试这个来获得面部坐标:

CGRect newBounds = CGRectMake(faceFeature.bounds.origin.x, 
                              _picture.size.height - faceFeature.bounds.origin.y - largestFace.bounds.size.height, 
                              faceFeature.bounds.size.width, 
                              faceFeature.bounds.size.height);

然后使用以下方法裁剪图像:

CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, newBounds);
UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
UIImageView *newView = [[UIImageView alloc] initWithImage:croppedImage];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM