标记灰度图像中的特定像素值

时间:2017-07-21 07:44:51

标签: python image-processing

我有灰度图像和阈值。超过阈值的像素值应标记为蓝色或" +"标志。

thresh_img = np.zeros((r,c))
thresh_img[:,:] = img[:,:]
thresh_img[thresh_img > 40] = 0

如何在python中执行此操作?

2 个答案:

答案 0 :(得分:1)

使用boolean indexing标识值,然后使用numpy.nonzeronumpy.where获取其索引。对于图像或矩阵,索引可以直接用作位置。然后使用matplotlib.plot(x, y, 'b+')

答案 1 :(得分:0)

执行此操作的方法是使用PIL库(http://www.pythonware.com/products/pil/)。 首先,创建一个用于存储值的数组。然后打开图像并使用for循环,迭代它拥有的所有像素。根据像素的颜色,您可以存储' +'或其他东西(你没有指明什么,所以我认为它是颜色,你可以存储为单个数字,因为灰色具有相似的R G和B值)。所以,我可能会这样做:

from PIL import Image
cols = []
im = Image.open("dead_parrot.jpg") #Can be many different formats.
pix = im.load()
w = im.size[0]
h = im.size[1]
for i in range(w):
    row = []
    for j in range(h):
        red = pix[i,j][0]
        if red > threshold:
            row.append('+')
        else:
            row.append(str(red))
    cols.append(row)
print(cols)

我相信这应该可以胜任。你能试试吗?

相关问题