Tuesday, 15 February 2011

iphone - Cropping an image in iOS using OpenCV face detection -



iphone - Cropping an image in iOS using OpenCV face detection -

i used below code crop face image, in face detection code. not getting proper face images , getting part of image saved , not face. what's wrong in code?

_facecascade.detectmultiscale(mat, faces, 1.1, 2, khaaroptions, cv::size(40, 40));

and within displayfaces functions cropping 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 right co-ordinates of faces[i]. problem cropping , setting roi. can 1 help me in solving it?

i tried below code also, 1 time again getting same images. (i.e., not getting 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: detecting face in live video stream using opencv facedetect. can greenish rectangle around face. couldn't crop face face parameters. tried setting faceroi observe eyes, fails. narrow downwards issue, problem might in setting roi image?

updated on 11/02/13:

i have done cropping below roi not set , image not cropped well:

i found issue above post ( @jameo pointing me because of rotation issue.) have rotated image below.

uiimage *rotateimage = [[uiimage alloc] initwithcgimage: image.cgimage scale: 1.0 orientation: uiimageorientationright];

and cropped image using below code:

// sub image + (uiimage*) getsubimagefrom: (uiimage*) img withrect: (cgrect) rect { uigraphicsbeginimagecontext(rect.size); cgcontextref context = uigraphicsgetcurrentcontext(); // translated rectangle drawing sub image cgrect drawrect = cgrectmake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height); // clip bounds of image context // not strictly necessary clipped anyway? cgcontextcliptorect(context, cgrectmake(0, 0, rect.size.width, rect.size.height)); // draw image [img drawinrect:drawrect]; // grab image uiimage* subimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); homecoming subimage; }

the image cropped not actual co-ordinates.

my observations: 1) cropping image facerect returned affinetransform. reason wrong co-ordinates or because of bug in code?? 2) couldnt set roi image before setting affine transform, reason, procedure set roi?

facerect = cgrectapplyaffinetransform(facerect, t);

but still cropping not done difference shown below:

full image:

cropped image

what this?

- (uiimage *) getsubimagefrom:(uiimage *)imagetocrop withrect:(cgrect)rect { cgimageref imageref = cgimagecreatewithimageinrect([imagetocrop cgimage], rect); uiimage *cropped = [uiimage imagewithcgimage:imageref]; cgimagerelease(imageref); homecoming cropped; }

iphone ios objective-c image-processing opencv

No comments:

Post a Comment