Cropping an image in iOS using OpenCV face detection

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

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

回答1:

what about this?

- (UIImage *) getSubImageFrom:(UIImage *)imageToCrop WithRect:(CGRect)rect {      CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);         UIImage *cropped = [UIImage imageWithCGImage:imageRef];     CGImageRelease(imageRef);      return cropped;     }


回答2:

try this to get the face coordinates :

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);

and then crop the image using :

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!