matplotlib.imread()返回什么?如何迭代过滤特定像素?

2024-10-08 18:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用matplotlib.imread()时,它返回一个形状的3D数组(4971248,3)

此数组中存储了哪些值?这是照片中像素的RGB值吗

img = mpimg.imread("./pearlite_strain2.jpg")

我想遍历这个数组,并保存从照片最左侧到某个RGB值的所有像素的距离

我该怎么做


Tags: 距离imgmatplotlibrgb像素数组照片jpg
1条回答
网友
1楼 · 发布于 2024-10-08 18:28:20

数组中的值是RGB(y, x, color)。该数组是一个标准的numpy数组,因此您可以使用^{}^{}来获取所需的信息(argmax01或^}和True的布尔数组中查找1True的第一个匹配项)。所需距离为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) 

enter image description here

相关问题 更多 >

    热门问题