2011-06-21 3 views
1

Je travaille sur openCV pour détecter le visage. Je veux que le visage soit recadré une fois qu'il est détecté. Jusqu'à présent, j'ai eu le visage et marqué la rect/ellipse autour de lui sur l'iPhone.Crop image circulaire ou elliptique de l'original UIImage

S'il vous plaît, aidez-moi en culture face à motif circulaire/elliptique

(UIImage *) opencvFaceDetect:(UIImage *)originalImage 
{ 

cvSetErrMode(CV_ErrModeParent); 

IplImage *image = [self CreateIplImageFromUIImage:originalImage]; 

// Scaling down 

/* 
Creates IPL image (header and data) ----------------cvCreateImage 
CVAPI(IplImage*) cvCreateImage(CvSize size, int depth, int channels); 
*/ 

IplImage *small_image = cvCreateImage(cvSize(image->width/2,image->height/2), 
    IPL_DEPTH_8U, 3); 

/*SMOOTHES DOWN THYE GUASSIAN SURFACE--------:cvPyrDown*/ 
cvPyrDown(image, small_image, CV_GAUSSIAN_5x5); 
int scale = 2; 

// Load XML 
NSString *path = [[NSBundle mainBundle] pathForResource:@"haarcascade_frontalface_default" ofType:@"xml"]; 
CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad([path cStringUsingEncoding:NSASCIIStringEncoding], NULL, NULL, NULL); 

// Check whether the cascade has loaded successfully. Else report and error and quit 

if(!cascade) 
{ 
    NSLog(@"ERROR: Could not load classifier cascade\n"); 
    //return; 
} 

//Allocate the Memory storage 
CvMemStorage* storage = cvCreateMemStorage(0); 

// Clear the memory storage which was used before 
cvClearMemStorage(storage); 

CGColorSpaceRef colorSpace; 
CGContextRef contextRef; 


CGRect face_rect; 
// Find whether the cascade is loaded, to find the faces. If yes, then: 
if(cascade) 
{ 
CvSeq* faces = cvHaarDetectObjects(small_image, cascade, storage, 1.1f, 3, 0, cvSize(20, 20)); 
cvReleaseImage(&small_image); 

// Create canvas to show the results 
CGImageRef imageRef = originalImage.CGImage; 
colorSpace = CGColorSpaceCreateDeviceRGB(); 
contextRef = CGBitmapContextCreate(NULL, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width * 4, 
               colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); 
//VIKAS 
CGContextDrawImage(contextRef, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), imageRef); 



CGContextSetLineWidth(contextRef, 4); 
CGContextSetRGBStrokeColor(contextRef, 1.0, 1.0, 1.0, 0.5); 




// Draw results on the iamge:Draw all components of face in the form of small rectangles 

// Loop the number of faces found. 

for(int i = 0; i < faces->total; i++) 
    { 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

    // Calc the rect of faces 
    // Create a new rectangle for drawing the face 

    CvRect cvrect = *(CvRect*)cvGetSeqElem(faces, i); 
    // CGRect face_rect = CGContextConvertRectToDeviceSpace(contextRef, 
    //       CGRectMake(cvrect.x * scale, cvrect.y * scale, cvrect.width * scale, cvrect.height * scale)); 


    face_rect = CGContextConvertRectToDeviceSpace(contextRef, 
                 CGRectMake(cvrect.x*scale, cvrect.y , cvrect.width*scale , cvrect.height*scale*1.25 
                    )); 

    facedetectapp=(FaceDetectAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    facedetectapp.grabcropcoordrect=face_rect; 

    NSLog(@" FACE off %f %f %f %f",facedetectapp.grabcropcoordrect.origin.x,facedetectapp.grabcropcoordrect.origin.y,facedetectapp.grabcropcoordrect.size.width,facedetectapp.grabcropcoordrect.size.height); 
    CGContextStrokeRect(contextRef, face_rect); 
     //CGContextFillEllipseInRect(contextRef,face_rect); 
    CGContextStrokeEllipseInRect(contextRef,face_rect); 


    [pool release]; 
} 

} 
CGImageRef imageRef = CGImageCreateWithImageInRect([originalImage CGImage],face_rect); 
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 


CGContextRelease(contextRef); 
CGColorSpaceRelease(colorSpace); 

cvReleaseMemStorage(&storage); 
cvReleaseHaarClassifierCascade(&cascade); 

    return returnImage; 
} 


} 

Merci Vikas

+1

Vous avez laissé une majuscule pour le titre et vous avez oublié de formater votre code. Veuillez corriger ceci si vous voulez que les gens lisent votre question. – PengOne

+0

Je n'ai jamais fait cela mais j'ai lu à ce sujet donc je vous donne juste un commentaire. Vous pouvez dessiner l'éclipse sur une image de masque, puis utiliser CGImageCreateWithMask. Jetez un oeil à cet exemple: http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html – n3on

+0

@ n3on :: J'ai essayé d'utiliser le iphonedevelopertips.com/cocoa/how-to-mask-an -image.html mais jst met le masque sur l'image mais ne le recadre pas –

Répondre

0

Il y a un tas de modes de mélange à choisir, dont quelques-unes des sont utiles pour " masquage ". Je crois que cela devrait faire à peu près ce que vous voulez:

CGContextSaveGState(contextRef); 
CGContextSetBlendMode(contextRef,kCGBlendModeDestinationIn); 
CGContextFillEllipseInRect(contextRef,face_rect); 
CGContextRestoreGState(contextRef); 

« approximativement », car il va masquer le contenu de contexte à chaque fois, en faisant ainsi la mauvaise chose pour plus d'un visage. Pour gérer ce cas, utilisez CGContextAddEllipseInRect() dans la boucle et CGContextFillPath() à la fin.

Vous pouvez également regarder CGContextBeginTransparencyLayerWithRect().

+0

salut j'ai essayé de l'utiliser mais je pourrais l'élaborer –

0

Voici la réponse que j'ai donnée dans How to crop UIImage on oval shape or circle shape? pour créer le cercle de l'image. Il fonctionne pour moi ..

Téléchargez le fichier d'archive de support depuis l'URL http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

#import "UIImage+RoundedCorner.h" 
#import "UIImage+Resize.h" 

Après des lignes utilisées pour redimensionner l'image et convertir pour rond avec un rayon

UIImage *mask = [UIImage imageNamed:@"mask.jpg"]; 

mask = [mask resizedImage:CGSizeMake(47, 47) interpolationQuality:kCGInterpolationHigh ]; 
mask = [mask roundedCornerImage:23.5 borderSize:1]; 

espère que cela aide un peu un ..