使用numpy从图像数组的RGB值中获取(x,y)坐标值

时间:2015-07-28 21:37:34

标签: python arrays numpy rgb

我是python的新手,所以我真的需要帮助。

我有一个灰度和阈值的图像,因此只有黑色和白色的颜色。

我不确定如何编写一个算法,它会在图像数组上给出一个坐标(x,y)列表,仅对应于白色像素。

感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

Surely you must already have the image data in the form of a list of intensity values? If you're using Anaconda, you can use the PIL Image module and call getdata() to obtain this intensity information. Some people advise to use NumPy methods, or others, instead, which may improve performance. If you want to look into that then go for it, my answer can apply to any of them.

If you have already a function to convert a greyscale image to B&W, then you should have the intensity information on that output image, a list of 0's and 1's , starting from the top left corner to the bottom right. If you have that, you already have your location data, it just isnt in (x,y) form. To do that, use something like this:

data = image.getdata()
height = image.getHeight()
width = image.getWidth()
pixelList = []

for i in range(height):
    for j in range(width):
        stride = (width*i) + j
        pixelList.append((j, i, data[stride]))

Where data is a list of 0's and 1's (B&W), and I assume you have written getWidth() and getHeight() Don't just copy what I've written, understand what the loops are doing. That will result in a list, pixelList, of tuples, each tuple containing intensity and location information, in the form (x, y, intensity). That may be a messy form for what you are doing, but that's the idea. It would be much cleaner and accessible to instead of making a list of tuples, pass the three values (x, y, intensity) to a Pixel object or something. Then you can get any of those values from anywhere. I would encourage you to do that, for better organization and so you can write the code on your own.

In either case, having the intensity and location stored together makes sorting out the white pixels very easy. Here it is using the list of tuples:

whites = []
for pixel in pixelList:
    if pixel[2] == 1:
        whites.append(pixel[0:2])

Then you have a list of white pixel coordinates.

答案 1 :(得分:0)

您可以使用PILnp.where来高效,简洁地获得结果

from PIL import Image
import numpy as np


img = Image.open('/your_pic.png')
pixel_mat = np.array(img.getdata())
width = img.size[0]

pixel_ind = np.where((pixel_mat[:, :3] > 0).any(axis=1))[0]
coordinate = np.concatenate(
    [
        (pixel_ind % width).reshape(-1, 1),
        (pixel_ind // width).reshape(-1, 1),
    ],
    axis=1,
)

选择所需的像素并获取其索引,然后基于该像素计算坐标。如果不使用Loop表达式,该算法可能会更快。

PIL仅用于获取像素矩阵和图像宽度,您可以使用任何您熟悉的库来替换它。