使用numpy从图像阵列的RGB值获取

使用numpy从图像阵列的RGB值获取

本文介绍了使用numpy从图像阵列的RGB值获取(x,y)坐标值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to python so I really need help with this one.

我有一个灰度图像并设置了阈值,因此只有黑色和白色。

I have an image greyscaled and thresholded so that the only colors present are black and white.

我不确定如何编写一种算法,该算法将为我提供图像阵列上与白色像素相对应的坐标(x,y)的列表

I'm not sure how to go about writing an algorithm that will give me a list of coordinates (x,y) on the image array that correspond to the white pixels only.

感谢您的帮助!

推荐答案

当然,您必须已经具有强度值列表形式的图像数据了吗?如果使用的是Anaconda,则可以使用 PIL图像模块并调用 getdata()来获取强度信息。 。有些人建议使用NumPy方法,或者其他方法,这可能会提高性能。如果您想研究该问题,那么我的答案可以应用于其中的任何一个。

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.

如果您已经具有将灰度图像转换为B&的功能, W,那么您应该在该输出图像上获得强度信息,即从左上角到右下角的0和1列表。如果有的话,那么您已经有了位置数据,它不是(x,y)形式的。为此,请使用以下命令:

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]))

其中 data 是0和1(B& W)的列表,我假设您已经写了 getWidth () getHeight()不仅要复制我写的内容,还要了解循环的作用。这将产生一个元组列表 pixelList ,每个元组包含强度和位置信息,形式为(x,y,intensity)。那可能是您所做工作的混乱形式,但这就是想法。将三个值(x,y,intensity)传递给Pixel对象或其他东西,而不是创建元组列表,这样会更加整洁和易于访问。然后,您可以从任何地方获取任何这些值。我鼓励您这样做,以便更好的组织,以便您可以自己编写代码。

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.

这篇关于使用numpy从图像阵列的RGB值获取(x,y)坐标值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:38