如何加载具有 4 个通道的 png 图像?
问题描述:
我一直在尝试使用透明通道(RGB 和 Alph)加载 .png 文件,但没有成功.看来 openCV 从图像中去除了第 4 个通道.即使我必须修改 OpenCV 源代码并重建它,是否有任何方法可以加载包含 alpha 通道在内的完整 4 个通道的图像?
I have been trying to load .png files with transparency channel (RGB and Alph) with no luck. It appears that openCV strips the 4th channel out of the image. Is there any method to load the image with the complete 4 channels including the alpha channel even if I had to modify the OpenCV source code and rebuild it?
答
如果您使用的是 OpenCV 2 或 OpenCV 3,您应该使用 IMREAD_* 标志(如 此处).
If you are using OpenCV 2 or OpenCV 3 you should use IMREAD_* flags (as mentioned at here).
C++
using namespace cv;
Mat image = imread("image.png", IMREAD_UNCHANGED);
Python
import cv2
im = cv2.imread("image.png", cv2.IMREAD_UNCHANGED)