Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:17:56+00:00 2026-05-23T10:17:56+00:00

I am working on openCV for detecting the face .I want face to get

  • 0

I am working on openCV for detecting the face .I want face to get cropped once its detected.Till now I got the face and have marked the rect/ellipse around it on iPhone.

Please help me out in cropping the face in circular/elliptical pattern

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


}

Thanks
Vikas

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:17:57+00:00Added an answer on May 23, 2026 at 10:17 am

    There are a pile of blend modes to choose from, a few of which are useful for “masking”. I believe this should do approximately what you want:

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

    “approximately” because it’ll mask the entire context contents every time, thus doing the wrong thing for more than one face. To handle this case, use CGContextAddEllipseInRect() in the loop and CGContextFillPath() at the end.

    You might also want to look at CGContextBeginTransparencyLayerWithRect().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a project using OpenCV and have made a face
Possible Duplicate: cmake is not working in opencv c++ project I have a huge
I have been working on an opencv project for iOS. I was given a
I've gotten OpenCV working with Python and I can even detect a face through
I'm working on a project using OpenCV on template matching, and I want to
I have opencv installed and working on my iphone (big thanks to this community).
I have implemented an openCV applicationWhere I use SURF descriptor. It is working fine
I moved to Ubuntu recently and I can't get openCV working with QtCreator. As
I have been successfully working with the Haar algorithm in OpenCV-2.1.0 (cvHaarDetectObjects) to detect
I'm trying to get OpenCV working under windows 7. I am able to generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.