MatToUIImage does not respect alpha channel (Patch #4401)


Added by Rasmus Diederichsen over 9 years ago. Updated over 9 years ago.


Status:Done Start date:2015-06-11
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:ios
Target version:-
Affected version:branch 'master' (3.0-dev) Operating System:Any
Difficulty:Easy HW Platform:Any
Pull request:https://github.com/Itseez/opencv/pull/4115

Description

When using the MatToUIImage function from imgcodecs/ios.h on a 4-channel Mat object, there is no transparency in the resulting UIImage. This can be fixed by adding two lines in ios_conversions.mm. For me, this is unfortunate as I am unable to build the ios framework from source, so I have to implement this one method myself.

   ...
    if (cvMat.elemSize() == 1)
        colorSpace = CGColorSpaceCreateDeviceGray();
    else
        colorSpace = CGColorSpaceCreateDeviceRGB();

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    bool alpha = cvMat.channels() == 4;   // <===== 
    CGBitmapInfo bitMapInfo = (alpha ? kCGImageAlphaLast : kCGImageAlphaNone) | kCGBitmapByteOrderDefault; // <=====
    // Creating CGImage from 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
                                        bitMapInfo,                                 // bitmap info // <=====
   ...

History

Updated by Alexander Alekhin over 9 years ago

  • Status changed from New to Done
  • Assignee deleted (Alexander Shishkov)
  • Pull request set to https://github.com/Itseez/opencv/pull/4115

Also available in: Atom PDF