Posted By

dio on 07/25/10


Tagged

IplImage


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


IplImage from UIImage


 / Published in: Objective C
 

URL: http://niw.at/articles/2009/03/14/using-opencv-on-iphone/en

  1. // NOTE you SHOULD cvReleaseImage() for the return value when end of the code.
  2. - (IplImage *)CreateIplImageFromUIImage:(UIImage *)image {
  3. // Getting CGImage from UIImage
  4. CGImageRef imageRef = image.CGImage;
  5.  
  6. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  7. // Creating temporal IplImage for drawing
  8. IplImage *iplimage = cvCreateImage(
  9. cvSize(image.size.width,image.size.height), IPL_DEPTH_8U, 4
  10. );
  11. // Creating CGContext for temporal IplImage
  12. CGContextRef contextRef = CGBitmapContextCreate(
  13. iplimage->imageData, iplimage->width, iplimage->height,
  14. iplimage->depth, iplimage->widthStep,
  15. colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault
  16. );
  17. // Drawing CGImage to CGContext
  18. CGContextDrawImage(
  19. contextRef,
  20. CGRectMake(0, 0, image.size.width, image.size.height),
  21. imageRef
  22. );
  23. CGContextRelease(contextRef);
  24. CGColorSpaceRelease(colorSpace);
  25.  
  26. // Creating result IplImage
  27. IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
  28. cvCvtColor(iplimage, ret, CV_RGBA2BGR);
  29. cvReleaseImage(&iplimage);
  30.  
  31. return ret;
  32. }

Report this snippet  

You need to login to post a comment.