I used the below code to crop a face from an image, in my face detection code. But I am not getting the proper face images and I am getting some part of the image saved and not the face. What's wrong in my code?
_faceCascade.detectMultiScale(mat, faces, 1.1, 2, kHaarOptions, cv::Size(40, 40));
And inside the displayfaces functions am cropping with this code:
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);
Am getting the correct co-ordinates of faces[i]
. But the problem is only with cropping and setting ROI. Can some one help me in solving it?
I tried with the below code also, again am getting the same images. (i.e., am not getting the actual face image)
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);
Note: I am detecting the face in live video stream using opencv facedetect. I can get the green rectangle around the face. But I couldn't crop the face with face parameters. I also tried setting faceroi to detect eyes, even that fails. To narrow down the issue, the problem might be in setting ROI for the image?
Updated on 11/02/13:
I have Done Cropping as below but ROI is not Set properly and the image is not cropped well:
I found the issue for my above post ( thanks @Jameo to pointing me that its because of Rotation issue.) I have Rotated the Image as below.
UIImage *rotateImage = [[UIImage alloc] initWithCGImage: image.CGImage
scale: 1.0
orientation: UIImageOrientationRight];
And Cropped the image using the below Code:
// 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;
}
The Image is cropped but Not with actual Co-ordinates.
My observations:
1) Am cropping the image with FaceRect which is returned by Affinetransform. Will this be a reason for wrong Co-ordinates or its because of bug in my code??
2) I couldnt set ROI for an image before setting Affine Transform, what is the reason, Is this the procedure to set ROI?
faceRect = CGRectApplyAffineTransform(faceRect, t);
But Still the cropping is not done properly The difference is shown below:
Full Image:
Cropped Image
See Question&Answers more detail:
os