图像像素及其颜色

发布于 2025-02-07 22:31:35 字数 324 浏览 0 评论 0原文

我有一个灰度图像,我想将其分成像素,并确定图像的每个像素中的灰度。 需要以以下形式进行数组:( x,x的像素,y,灰色阴影0-255)。

1,1,25;

1,2,36;

1,3,50; 。 。 。

50,60,96; 。 。 。 如果图像为500 x 600个点,则最终应该得到 - (500,600,灰度)。

您能告诉我,如何从图像中获取如此多的数据?我需要做什么?我应该使用哪些库?如果有人解决了这样的问题,请举一个例子。非常感谢!

I have a grayscale image, I would like to split it into pixels and determine the grayscale in each pixel of the image.
The array is needed in the following form: (pixel by X, pixel by Y, gray shade 0-255).

1,1,25;

1,2,36;

1,3,50;
.
.
.

50,60,96;
.
.
.
if the image is 500 by 600 dots, then at the end it should get - (500,600, grayscale).

Could you tell me please, how can I get such an array of data from an image? What do I need to do? Which libraries should I use? If there is someone who has solved such a problem, please give an example. Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2025-02-14 22:31:35

如果您已经有一个图像文件,则可以这样读取:

from PIL import Image

img = Image.open('/path/to/image.png')

要获得一个数组:

import numpy as np

ima = np.asarray(img)

如果它确实是8位灰度图像,则可以使用image.open('image.png' ,mode ='l'),但是在任何情况下,您总是可以使用ima [:,:,0]获得红色通道。如果是灰度,则所有频道将相等。

现在,您可以用坐标堆叠这些灰色级别:

h, w, _ = ima.shape
x, y = np.meshgrid(np.arange(w), np.arange(h))
np.dstack([x, y, ima[..., 0]])

If you already have an image file, you can read it like this:

from PIL import Image

img = Image.open('/path/to/image.png')

To get this an an array:

import numpy as np

ima = np.asarray(img)

If it's really an 8-bit greyscale image, you might be able to use Image.open('image.png', mode='L'), but in any case you can always just get the red channel with ima[:, :, 0]. If it's greyscale, all the channels will be equal.

Now you can stack these grey levels with the coordinates:

h, w, _ = ima.shape
x, y = np.meshgrid(np.arange(w), np.arange(h))
np.dstack([x, y, ima[..., 0]])
扬花落满肩 2025-02-14 22:31:35

我会这样做:

# random data
np.random.seed(10)
img = np.random.randint(0,256, (500,600))

# coordinates
# np.meshgrid is also a good (better) choice
x, y = np.where(np.ones_like(img))

# put them together
out = np.stack([x,y, img.ravel()], axis=1)

输出:

array([[  0,   0,   9],
       [  0,   1, 125],
       [  0,   2, 228],
       ...,
       [499, 597, 111],
       [499, 598, 128],
       [499, 599,   8]])

I would do this:

# random data
np.random.seed(10)
img = np.random.randint(0,256, (500,600))

# coordinates
# np.meshgrid is also a good (better) choice
x, y = np.where(np.ones_like(img))

# put them together
out = np.stack([x,y, img.ravel()], axis=1)

Output:

array([[  0,   0,   9],
       [  0,   1, 125],
       [  0,   2, 228],
       ...,
       [499, 597, 111],
       [499, 598, 128],
       [499, 599,   8]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文