OpenCV:如何用4个通道加载png图像?

Moh*_*eel 32 opencv

我一直在尝试使用透明通道(RGB和Alph)加载.png文件而没有运气.似乎openCV剥离了图像中的第4个通道.是否有任何方法可以使用完整的4个通道加载图像,包括alpha通道,即使我必须修改OpenCV源代码并重建它

Sat*_*ick 56

如果你正在使用OpenCV的2或3的OpenCV你应该使用IMREAD_*标志(截至提到这里).

C++

using namespace cv;
Mat image = imread("image.png", IMREAD_UNCHANGED);
Run Code Online (Sandbox Code Playgroud)

蟒蛇

import cv2
im = cv2.imread("image.png", cv2.IMREAD_UNCHANGED)
Run Code Online (Sandbox Code Playgroud)

  • 我可能通过阅读源代码获得了:) (3认同)

Die*_*yol 13

根据文档,OpenCV支持PNG上的alpha通道.

只需使用CV_LOAD_IMAGE_UNCHANGED作为标志调用imread函数,如下所示:

cvLoadImage("file.png", CV_LOAD_IMAGE_UNCHANGED)
Run Code Online (Sandbox Code Playgroud)


小智 10

读取透明PNG的正确方法是使用第4个通道作为Alpha通道.大多数情况下,人们想要一个白色背景,如果是这种情况,那么下面的代码可以用于alpha合成.

def read_transparent_png(filename):
    image_4channel = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
    alpha_channel = image_4channel[:,:,3]
    rgb_channels = image_4channel[:,:,:3]

    # White Background Image
    white_background_image = np.ones_like(rgb_channels, dtype=np.uint8) * 255

    # Alpha factor
    alpha_factor = alpha_channel[:,:,np.newaxis].astype(np.float32) / 255.0
    alpha_factor = np.concatenate((alpha_factor,alpha_factor,alpha_factor), axis=2)

    # Transparent Image Rendered on White Background
    base = rgb_channels.astype(np.float32) * alpha_factor
    white = white_background_image.astype(np.float32) * (1 - alpha_factor)
    final_image = base + white
    return final_image.astype(np.uint8)
Run Code Online (Sandbox Code Playgroud)

关于此的详细博客在这里.


Der*_*rzu 5

如果你想在另一个图像上绘制这个透明图像,请按照 @satya-mallick 的回答打开你的图像(模式 IMREAD_UNCHANGED),然后使用此方法在框架 Mat 上绘制图像:

/**
 * @brief Draws a transparent image over a frame Mat.
 * 
 * @param frame the frame where the transparent image will be drawn
 * @param transp the Mat image with transparency, read from a PNG image, with the IMREAD_UNCHANGED flag
 * @param xPos x position of the frame image where the image will start.
 * @param yPos y position of the frame image where the image will start.
 */
void drawTransparency(Mat frame, Mat transp, int xPos, int yPos) {
    Mat mask;
    vector<Mat> layers;
    
    split(transp, layers); // seperate channels
    Mat rgb[3] = { layers[0],layers[1],layers[2] };
    mask = layers[3]; // png's alpha channel used as mask
    merge(rgb, 3, transp);  // put together the RGB channels, now transp insn't transparent 
    transp.copyTo(frame.rowRange(yPos, yPos + transp.rows).colRange(xPos, xPos + transp.cols), mask);
}
Run Code Online (Sandbox Code Playgroud)