python OpenCV - add alpha channel to RGB image

前端 未结 4 1415
闹比i
闹比i 2020-11-27 17:22

What is the best way to convert RGB image to RGBA in python using opencv?

Let\'s say I have one array with shape

(185, 198, 3) - it is RGB

4条回答
  •  囚心锁ツ
    2020-11-27 17:32

    With opencv3, this should work:

    Python

    # First create the image with alpha channel
    rgba = cv2.cvtColor(rgb_data, cv2.COLOR_RGB2RGBA)
    
    # Then assign the mask to the last channel of the image
    rgba[:, :, 3] = alpha_data
    

    C++

    # First create the image with alpha channel
    cv::cvtColor(rgb_data, rgba , cv::COLOR_RGB2RGBA);
    
    # Split the image for access to alpha channel
    std::vectorchannels(4);
    cv::split(rgba, channels);
    
    # Assign the mask to the last channel of the image
    channels[3] = alpha_data;
    
    # Finally concat channels for rgba image
    cv::merge(channels, 4, rgba);
    

提交回复
热议问题