OpenCV: loading an image with an alpha channel, but with a depth of 8 bits

I am a little confused with the flags that it accepts cv::imread.

My goal is to upload images that have an alpha channel with that alpha channel (i.e. how CV_8UC4). At the same time, I want to download them always with a depth of 8 bits.

First of all, I tried to use the following:

cv::imread(/*path*/, cv::IMREAD_COLOR);

This is an alpha channel strip and returns an 8-bit image. For transparent TIF, everything looks right, only the transparent parts are black. For transparent PNG, hoverver, this looks completely wrong.

The next thing I tried was:

cv::imread(/*path*/, cv::IMREAD_ANYCOLOR);

The result was exactly the same as with cv::IMREAD_COLOR. Next attempt:

cv::imread(/*path*/, cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);

Alpha channels still do not exist, but now the original image depth is preserved.

Then I tried:

cv::imread(/*path*/, cv::IMREAD_UNCHANGED);

PNG. 8 . , . , .

OpenCV. :

IMREAD_UNCHANGED    
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE    
If set, always convert image to the single channel grayscale image.
IMREAD_COLOR    
If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH     
If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR     
If set, the image is read in any possible color format.

, IMREAD_UNCHANGED - . IMREAD_ANYCOLOR , ?

-, 8 ?

, : IMREAD_UNCHANGED rgb -, RGBA. rgb -, BGR. ? , OpenCV BGR.

, PNG - coorecty :

enter image description here enter image description here

+2

Source: https://habr.com/ru/post/1654652/


All Articles