【问题标题】:check pixel's transparency in OpenCV在 OpenCV 中检查像素的透明度
【发布时间】:2023-03-08 15:13:01
【问题描述】:

如何检查 OpenCV 中的像素是否透明?我有一个带有透明部分的 png 图像,我想将 rgb 图像转换为 hsv,然后改变像素的色调。我需要透明像素在转换后保持透明。

请帮忙。

【问题讨论】:

    标签: image-processing opencv transparency pixel alpha


    【解决方案1】:

    你可以试试GDAL。与CV2兼容

    这些链接可能有用。

    Reading Raster Data with GDAL

    GDAL API Tutorial

    import gdal
    from gdalconst import *
    import numpy as np
    
    ds = gdal.Open('lena.jpg', GA_ReadOnly)
    B = ds.GetRasterBand(1)
    G = ds.GetRasterBand(2)
    R = ds.GetRasterBand(3)
    A = ds.GetRasterBand(4) // Alpha
    
    height, width = B.shape
    img = np.zeros(height, width, 3)
    img[:, :, 0] = B
    img[:, :, 1] = G
    img[:, :, 2] = R
    
    // Do something you want
    
    ds.GetRasterBand(1).WriteArray(new_B)
    ds.GetRasterBand(2).WriteArray(new_G)
    ds.GetRasterBand(3).WriteArray(new_R)
    // The forth band dose not need to be changed
    
    // Close image, the changes is writen in the source file
    ds = None
    
    // Note that I did not test this code
    

    【讨论】:

    • 感谢您的回答。我想先用 OpenCV 解决这个问题。如果它不起作用,我会尝试 GDAl API。
    【解决方案2】:

    OpenCV 不支持图像的透明性。 (v2.4之前,我不确定最新版本)

    您可以尝试http://blog.developer.stylight.de/2010/05/how-to-load-alpha-channel-pngs-with.html 的解决方案并重建 OpenCV,或者您可以使用 ImageMagick 之类的工具将 alpha 层 (forum link) 提取为单独的图像并加载它。

    【讨论】:

    • 谢谢。实际上OpenCV imread 函数返回图像的第 4 个通道,当传递第二个参数 -1 时,如 Mat image = imread("5.png", -1);
    猜你喜欢
    • 2017-05-08
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    相关资源
    最近更新 更多