简体   繁体   English

如何用matplotlib imread获取所有蓝色像素的坐标?

[英]How to get the coordinates of all blue pixels with matplotlib imread?

I would like to append to a list all of the blue pixels of a jpeg image using matplolib's imshow. 我想使用matplolib的imshow将jpeg图像的所有蓝色像素附加到列表中。 When I launch my code, I do not get an RGB code result:'array([89, 67, 28], dtype=uint8), array([51, 53, 16], dtype=uint8),' etc... What is going wrong here? 当我启动我的代码时,我没有得到RGB代码结果:'array([89,67,28],dtype = uint8),array([51,53,16],dtype = uint8),'等等。这里出了什么问题?

import matplotlib.pyplot as plt import matplotlib.image as mpimg 将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)

When reading an image you will get an numpy array of the dimensions (width x height x [R,G,B, alpha]). 在读取图像时,您将得到一个numpy维度的数组(宽x高x [R,G,B,alpha])。

t = mpimg.imread("path/Test1.PNG")

now you can just access the blue layer by taking everything along the width and height dimension (indicated by the ":") and only the 3rd dimension from the RGB,alpha stack. 现在你可以通过沿着宽度和高度尺寸(由“:”表示)和RGB,alpha堆栈中的第三个维度获取所有内容来访问蓝色图层。 that gives you a 2D array, where every blue pixel has a nonzero value. 它为您提供了一个2D数组,其中每个蓝色像素都具有非零值。 to find all the coordinates of nonzero entries you can use the np.nonzero function, which gives you their coordinates as an X and Y array 要查找非零项的所有坐标,可以使用np.nonzero函数,它将它们的坐标作为X和Y数组

X,Y = np.nonzero(t[:,:,2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM