我在我的人脸检测代码中使用以下代码从图像中裁剪出脸部。但是我没有得到正确的脸部图像,只保存了图像的一部分而不是脸部。我的代码有什么问题?
我已经得到了
我尝试了下面的代码,但是我得到的图像仍然是相同的(即,我没有得到实际的人脸图像)。
注意:我正在使用OpenCV facedetect在实时视频流中检测人脸。我可以得到绿色矩形框围绕着脸部,但是我无法根据脸部参数裁剪出脸部。我还尝试了设置faceroi来检测眼睛,但也失败了。为了缩小问题范围,可能问题出在设置图像ROI上?
更新于11/02/13:
我已经进行了如下裁剪,但ROI没有正确设置,图像裁剪效果不佳:
我发现我的问题是由于旋转问题造成的(感谢@Jameo指出)。我已经对图像做了如下旋转处理。
_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]
的正确坐标。但问题在于裁剪和设置ROI。有人能帮我解决吗?我尝试了下面的代码,但是我得到的图像仍然是相同的(即,我没有得到实际的人脸图像)。
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上?
更新于11/02/13:
我已经进行了如下裁剪,但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);
但是仍然没有正确地裁剪图片,差异如下:
完整图片:
裁剪后的图片: