如何檢查在OpenCV中像素是否透明?我有一個帶有透明部分的PNG圖像,我想將rgb圖像轉換爲hsv,然後更改像素的色調。我需要透明像素在轉換後保持透明。在OpenCV中檢查像素的透明度
請任何幫助。
如何檢查在OpenCV中像素是否透明?我有一個帶有透明部分的PNG圖像,我想將rgb圖像轉換爲hsv,然後更改像素的色調。我需要透明像素在轉換後保持透明。在OpenCV中檢查像素的透明度
請任何幫助。
OpenCV不支持圖像中的透明性。 (V2.4之前,我不知道最新版本)
您可以在http://blog.developer.stylight.de/2010/05/how-to-load-alpha-channel-pngs-with.html嘗試解決和重建OpenCV的,或者您也可以使用類似的ImageMagick的阿爾法層(forum link)作爲一個單獨的圖像中提取並加載它。
您可以嘗試GDAL。它與CV2兼容
這些鏈接可能會有用。
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。 – Karmar
感謝。實際上,OpenCV imread函數在傳遞第二個參數-1時返回圖像的第四個通道,如Mat image = imread(「5.png」,-1); – Karmar