title: UIImage cv::Mat conversions with alpha channel
body: <p>I'm using following codes for converting <code>UIImage*</code> and <code>cv::Mat</code> to each other:</p>
  
  <pre><code>- (cv::Mat)cvMatFromUIImage:(UIImage *)image
  {
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    CGFloat cols = image.size.width;
    CGFloat rows = image.size.height;
  
    cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
  
    CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to  data
                                                   cols,                       // Width of bitmap
                                                   rows,                       // Height of bitmap
                                                   8,                          // Bits per component
                                                   cvMat.step[0],              // Bytes per row
                                                   colorSpace,                 // Colorspace
                                                   kCGImageAlphaNoneSkipLast |
                                                   kCGBitmapByteOrderDefault); // Bitmap info flags
  
    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    CGContextRelease(contextRef);
  
    return cvMat;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>-(UIImage *)UIImageFromCVMat:(cv::Mat)cvMat
  {
    NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize()*cvMat.total()];
    CGColorSpaceRef colorSpace;
  
    if (cvMat.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
    }
  
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
  
    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(cvMat.cols,                                 //width
                                       cvMat.rows,                                 //height
                                       8,                                          //bits per component
                                       8 * cvMat.elemSize(),                       //bits per pixel
                                       cvMat.step[0],                            //bytesPerRow
                                       colorSpace,                                 //colorspace
                                       kCGImageAlphaNone|kCGBitmapByteOrderDefault,// bitmap info
                                       provider,                                   //CGDataProviderRef
                                       NULL,                                       //decode
                                       false,                                      //should interpolate
                                       kCGRenderingIntentDefault                   //intent
                                       );
  
  
    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);
  
    return finalImage;
   }
  </code></pre>
  
  <p>I took these from <a href="http://docs.opencv.org/2.4/doc/tutorials/ios/image_manipulation/image_manipulation.html" rel="noreferrer">OpenCV Documentation</a>. I use them as follows:</p>
  
  <pre><code>UIImage *img = [UIImage imageNamed:@"transparent.png"];
  UIImage *img2 = [self UIImageFromCVMat:[self cvMatFromUIImage:img]];
  </code></pre>
  
  <p>However these functions loses the alpha channel information. I know it is because of the flags <code>kCGImageAlphaNone</code> and <code>kCGImageAlphaNoneSkipLast</code>, unfortunately I could't find a way not lose alpha information by changing these flags.</p>
  
  <p>So, how do I convert these two types between each other without losing alpha information?</p>
  
  <p>Here is the image that I use:</p>
  
  <p><a href="https://i.stack.imgur.com/MEl9O.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MEl9O.png" alt="enter image description here"></a></p>