[英]How to get the coordinates of all blue pixels with matplotlib imread?
我想使用matplolib的imshow將jpeg圖像的所有藍色像素附加到列表中。 當我啟動我的代碼時,我沒有得到RGB代碼結果:'array([89,67,28],dtype = uint8),array([51,53,16],dtype = uint8),'等等。這里出了什么問題?
將matplotlib.pyplot導入為plt導入matplotlib.image為mpimg
control = mpimg.imread('jpeg.jpg')
ys = control.shape[0] #length of image
xs = control.shape[1] # image width
pixelcoords= []
for x in range(xs):
for y in range(ys):
# if pixel is blue
pixelcoords.append(control[x][y])
print(pixelcoords)
在讀取圖像時,您將得到一個numpy維度的數組(寬x高x [R,G,B,alpha])。
t = mpimg.imread("path/Test1.PNG")
現在你可以通過沿着寬度和高度尺寸(由“:”表示)和RGB,alpha堆棧中的第三個維度獲取所有內容來訪問藍色圖層。 它為您提供了一個2D數組,其中每個藍色像素都具有非零值。 要查找非零項的所有坐標,可以使用np.nonzero函數,它將它們的坐標作為X和Y數組
X,Y = np.nonzero(t[:,:,2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.