我從OpenCV中的URL加載圖像。有些圖像是PNG,有四個通道。我正在尋找一種方法來刪除第4頻道,如果它存在。如何從PNG圖像中刪除第4個通道
這是我如何加載圖像:
def read_image_from_url(self, imgurl):
req = urllib.urlopen(imgurl)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
return cv2.imdecode(arr,-1) # 'load it as it is'
我不想改變cv2.imdecode(arr,-1)
而是我要檢查加載圖像是否有第四個通道,如果是這樣,將其刪除。
事情是這樣的,但我不知道如何實際刪除第4通道
def read_image_from_url(self, imgurl):
req = urllib.urlopen(imgurl)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
image = cv2.imdecode(arr,-1) # 'load it as it is'
s = image.shape
#check if third tuple of s is 4
#if it is 4 then remove the 4th channel and return the image.
是啊,有我想AS -IS某些圖像。這就是爲什麼我不願意改變國旗,而是做一個檢查,如果有第四層,然後才刪除它。 – Anthony
那麼你應該問如何檢查是否有第四個頻道,而不是如何刪除它...... – Piglet
這個標誌也會影響比特深度,所以這不是一個很好的解決方法來移除alpha通道 –