Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

iphone - Cropping an image in iOS using OpenCV face detection

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:

Full Image

Cropped Image

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...