简体   繁体   English

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

[英]Marking a particular pixel value in a grayscale image

I have a grayscale image and a threshold. 我有一个灰度图像和一个阈值。 The pixel values exceeding the threshold should be marked as either blue color or "+" sign. 超出阈值的像素值应标记为蓝色或“ +”号。

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

How to do this in python? 如何在python中做到这一点?

Use boolean indexing to identify the values, then use numpy.nonzero or numpy.where to get their indices. 使用布尔索引标识值,然后使用numpy.nonzeronumpy.where得到他们的指数。 For images or matrices the indices can be directly used as positions. 对于图像或矩阵,索引可以直接用作位置。 Then use matplotlib.plot(x, y, 'b+') 然后使用matplotlib.plot(x, y, 'b+')

A way to do this is using the PIL library ( http://www.pythonware.com/products/pil/ ). 一种方法是使用PIL库( http://www.pythonware.com/products/pil/ )。 First, you create an array to store values. 首先,创建一个数组来存储值。 Then you open the image and using a for loop, you iterate over all the pixels it has. 然后打开图像并使用for循环,遍历图像中所有的像素。 Depending on the color of the pixel, you store a '+' or something else (you don't specify what so I'll suppose it's the color, which you can store as a single number since grays have similar RG and B values). 根据像素的颜色,您可以存储“ +”或其他内容(您无需指定内容,所以我认为它是颜色,可以将其存储为一个数字,因为灰色的RG和B值相似) 。 So, I might do something this way: 所以,我可以这样做:

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)

I believe this should do the job. 我相信这应该做的。 Could you try it? 你可以试试看吗?

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

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