![](/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.