How to load png images with 4 channels?

Opencv

Opencv Problem Overview


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 Solutions


Solution 1 - Opencv

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)

Solution 2 - Opencv

According to the [documentation](http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#imread "imread documentation"), OpenCV supports alpha channel on PNGs.

Just call the imread function using CV_LOAD_IMAGE_UNCHANGED as flags like this:

cvLoadImage("file.png", CV_LOAD_IMAGE_UNCHANGED)

Solution 3 - Opencv

The right way to read a transparent PNG is to use the 4th channel as alpha channel. Most of the times one wants a white background, if that is the case then below code can be used for alpha compositing.

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)

A detailed blog on this is here here.

Solution 4 - Opencv

If you wanna draw this transparent image over another image, open your image as answered by @satya-mallick (mode IMREAD_UNCHANGED), then use this method to draw the image over a frame 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);
}

Solution 5 - Opencv

The best possible way to load a png image with all 4 channels is ;

img= cv2.imread('imagepath.jpg',negative value)

As per openCV documentation,
If Flag value is,

  1. =0 Return a grayscale image.
  2. <0 Return the loaded image as is (with alpha channel).

Solution 6 - Opencv

you can use:

import matplotlib.image as mpimg


img=mpimg.imread('image.png')
plt.imshow(img)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohammad ElwakeelView Question on Stackoverflow
Solution 1 - OpencvSatya MallickView Answer on Stackoverflow
Solution 2 - OpencvDiego Cerdan PuyolView Answer on Stackoverflow
Solution 3 - OpencvNikhilView Answer on Stackoverflow
Solution 4 - OpencvDerzuView Answer on Stackoverflow
Solution 5 - Opencv0xPrateekView Answer on Stackoverflow
Solution 6 - OpencvsmilingfishView Answer on Stackoverflow