[英]Slicing the channels of image and storing the channels into numpy array(same size as image). Plotting the numpy array not giving the original image
我分離了彩色圖像的 3 個通道。 我創建了一個與圖像大小相同的新 NumPy 數組,並將圖像的 3 個通道存儲到 3D NumPy 數組的 3 個切片中。 繪制 NumPy 數組后,繪制的圖像與原始圖像不同。 為什么會這樣?
img
和new_img
數組具有相同的元素,但圖像不同。
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
img=mpimg.imread('/storage/emulated/0/1sumint/kali5.jpg')
new_img=np.empty(img.shape)
new_img[:,:,0]=img[:,:,0]
new_img[:,:,1]=img[:,:,1]
new_img[:,:,2]=img[:,:,2]
plt.imshow(new_img)
plt.show()
期望與原始圖像相同的圖像。
問題是您的新圖像將在這一行使用默認數據類型float64
創建:
new_img=np.empty(img.shape)
除非您指定不同的dtype
。
您可以(最好)像這樣復制原始圖像的dtype
:
new_img = np.empty(im.shape, dtype=img.dtype)
或使用這樣的東西:
new_img = np.zeros_like(im)
或者(最糟糕的)指定一個你碰巧知道匹配你的數據的,像這樣,
new_img = np.empty(im.shape, dtype=np.uint8)
我認為您有一定的理由一次復制一個頻道,但如果沒有,您可以避免上述所有問題,只需執行以下操作:
new_img = np.copy(img)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.