![](/img/trans.png)
[英]How to change the color of 4 pixels in an image, multiple times using opencv?
[英]array of image pixels and their color
我有一个灰度图像,我想将其拆分为像素并确定图像每个像素的灰度。 需要以下形式的数组:(X 像素,Y 像素,灰度 0-255)。
1,1,25;
1,2,36;
1,3,50;
. . .
50,60,96;
. . . 如果图像是 500 x 600 点,那么最后它应该得到 - (500,600,灰度)。
你能告诉我,我怎样才能从图像中获取这样一组数据? 我需要做什么? 我应该使用哪些库? 如果有人解决了这样的问题,请举个例子。 非常感谢!
我会这样做:
# 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]])
如果您已经有一个图像文件,您可以像这样读取它:
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]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.