We are currently facing the situation that we need to process PNG files that contain an alpha channel. Unfortunately OpenCV curently does not support so (but there’s a hack – but that didn’t help in my situation). The documentation for cvLoadImage contains a sad little note:
Note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB.
Diving into the code I figured alpha channels have been disabled on purpose. Around line 220 in grfmt_png.cpp (the respective PNG encoder / decoder) it reads: png_set_strip_alpha( png_ptr ).
// Allow for PNG alpha channel here
if(color_type == PNG_COLOR_TYPE_RGB_ALPHA) { m_type = CV_8UC4;
}
2. In grfmt_png.cpp, around line 220 allow for an alpha channel by commenting the following statements:
//png_set_strip_alpha( png_ptr );
type = CV_MAKETYPE(CV_MAT_DEPTH(type), CV_MAT_CN(type));
After a recompile you should be able to read alpha-channeled pngs as well!