[英]what does matplotlib.imread() return? how to iterate to filter specific pixels?
When I use matplotlib.imread(), it returns a 3D array of shape (497, 1248, 3)当我使用 matplotlib.imread() 时,它返回一个形状为 (497、1248、3) 的 3D 数组
What values are stored in this array?该数组中存储了哪些值? is it the RGB values of the pixels in the photo?是照片中像素的 RGB 值吗?
img = mpimg.imread("./pearlite_strain2.jpg")
I want to iterate through this array, and save the distance of all pixels that are a certain RGB value, from the left most side of the photo.我想遍历这个数组,并保存从照片最左侧开始的某个 RGB 值的所有像素的距离。
how would I do that?我该怎么做?
The values in the array are RGB
values (y, x, color)
.数组中的值是RGB
值(y, x, color)
。 The array is a standard numpy array, so you can use argwhere
and argmax
to get the required information ( argmax
finds first occurrence of 1
or True
in the boolean array of 0
and 1
or False
and True
).该数组是标准的 numpy 数组,因此您可以使用argwhere
和argmax
来获取所需的信息( argmax 在0
和1
或False
和True
的argmax
数组中找到第一次出现的1
或True
)。 You desired distances are the x
coordinates:您想要的距离是x
坐标:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
img = np.random.randint(0, 200, (2,4,3), np.uint8)
searched = [245, 250, 255] # almost white bright pixels
img[0,2,:] = img[1,1,:] = img[1,3,:] = searched
plt.imshow(img)
all_distances = np.argwhere((img[...,:]==searched).all(2))[:,1]
# array([2, 1, 3], dtype=int64)
first_distances = np.argmax((img[...,:]==searched).all(2), axis=1)
# array([2, 1], dtype=int64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.