I want to load Mat
from an input image file with CV_8UC4
format using imread function.
It's been said that you should use IMREAD_UNCHANGED
as second argument to get 4 channel image as output.
Unfortunately, it never works if the file does not have alpha channel.
It seems that there is an option which make imread
generate three channel Mat
regardless of the input data, IMREAD_COLOR
. It's quite strange that it does not support the same option for four channel.
Instead, it's full of other options of which I don't quite understand what is the purpose.
My program should work on every image file regardless of the internal color space of the file.
For that, it should be able to open every image as same CV_8UC4
format.
I may be able to go into their file structure and find their color space by myself and manually change their format to CV_8UC4
after loading them using functions like cvtColor.
However, the internal structure of .bmp
, .jpg
, and especially .png
are quite complex. Analyzing them by myself would be quite inefficient.
I want to do that simply using functions provided by OpenCV if possible.
In fact, I'm more of curious now.
If it is impossible, isn't that mean using any image file with unknown internal format with OpenCV is impossible in general?
imread
is the only image loading function OpenCV supports.
However, if you don't know the correct format of the file, this function is just unusable.
You can't use IMREAD_UNCHANGED
, because you don't know with which color format the output will be generated.
Since once the Mat
value is generated, there is no way to find which color format it uses. (detect color space with openCV)
You can't use IMREAD_COLOR
either, since if the file has an alpha channel, it will be missed.
If I'm right, it's logically impossible to load an image file with unknown internal color format correctly using the functions provided by OpenCV.
Apparently the function imread
can see the correct file format by looking into the input file data. It uses the information internally.
That's why IMREAD_UNCHANGED
is even a thing.
The problem is that it never tells the information to you. And if you want to use it, you should check it by yourself. Everything about this feels very off to me.
In short, what should I do if I want to load file as OpenCV Mat
regardless of the internal file format?
If it's impossible, it there any good reason why the library is designed like this?