[英]Add co-ordinates to each pixel in python
為此,我正在嘗試為圖像的每個像素添加坐標,我正在執行以下操作
import cv2
import numpy as np
img = cv2.imread('images/0001.jpg')
grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
np_grayscale = np.array(grayscale)
# make the array 3d
processed_image = np_grayscale[:, :, np.newaxis]
x = 0
y = 0
for pixel_line in reversed(processed_image):
for pixel in pixel_line:
pixel = np.append(pixel, [x, y])
x += 1
y += 1
print(processed_image)
但這似乎不起作用,因為我仍在獲取表單中的原始數組
[[[255]
[255]
[255]
...
[255]
[255]
[255]]
...
...
...
[255]
[255]
[255]]]
此外,我認為這不是最有效的方法,因為我讀到 append 創建了數組的新副本,有人可以幫忙嗎
您可以使用meshgrid()
和stack()
使用原始圖像創建索引網格:
import numpy as np
x = np.asarray([
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 255, 0, 0],
[0, 0, 0, 0],
])
indices = np.meshgrid(
np.arange(x.shape[0]),
np.arange(x.shape[1]),
sparse=False
)
x = np.stack((x, *indices)).T
# array([[[ 0, 0, 0],
# [ 0, 0, 1],
# [ 0, 0, 2],
# [ 0, 0, 3]],
# [[ 0, 1, 0],
# [ 0, 1, 1],
# [255, 1, 2],
# [ 0, 1, 3]],
# [[ 0, 2, 0],
# [ 0, 2, 1],
# [ 0, 2, 2],
# [ 0, 2, 3]],
# [[ 0, 3, 0],
# [ 0, 3, 1],
# [ 0, 3, 2],
# [ 0, 3, 3]]])
x[0, 0, :] # 0, 0, 0
x[1, 2, :] # 255, 1, 2
x[-1, -1, :] # 0, 3, 3
使用xarray
。
到目前為止,向 NumPy 數組添加坐標的最簡單方法是使用xarray.DataArray
。 例如,以下是如何添加僅作為行和列索引的坐標:
import xarray as xr
data = np.arange(25).reshape(5, 5)
rows, cols = data.shape
xs = np.arange(cols)
ys = np.arange(rows)
da = xr.DataArray(data, name="mydata", coords=[ys, xs], dims=['y', 'x'])
這個da
東西是一個DataArray
,它本質上是一個索引數組( pandas
意義上的“索引”:索引可以是整數、浮點數、日期等)。 它有一些不錯的功能(試試da.plot()
)。 否則,它基本上表現為 NumPy 數組。 例如,您可以像普通數組一樣對它進行切片:
>>> da[:, 3:]
<xarray.DataArray 'mydata' (y: 5, x: 2)>
array([[ 3, 4],
[ 8, 9],
[13, 14],
[18, 19],
[23, 24]])
Coordinates:
* y (y) int64 0 1 2 3 4
* x (x) int64 3 4
如您所見,這個子數組“知道”它自己的坐標。 更重要的是,你可以有任意多的維度,每個軸可以有多個坐標。 這非常有用。
正如其他人指出的那樣,僅通過仔細的索引就可以實現您想要的,但如果您真的想要坐標,這就是我的做法。
由於您有灰度圖像,您可以將其轉換為 3 通道圖像,其中第一個通道將包含像素值,其他 2 個通道將包含坐標。 因此,如果將圖像分成兩個或更多個,在其他兩個通道中仍將具有原始圖像的坐標,並且為了使圖像可視化,您可以僅使用第一個通道。 這是您可以執行此操作的方法。
processed_image = np.zeros((grayscale.shape[0], grayscale.shape[1], 3), dtype=np.uint64)
processed_image[:, :, 0] = np.asarray(grayscale, dtype=np.uint64)
for i in range(processed_image.shape[0]):
for j in range(processed_image.shape[1]):
processed_image[i][j][1] = i
processed_image[i][j][2] = j
print(processed_image)
# Displaying the image
cv2.imshow("img", np.array(processed_image[:, :, [0]], dtype=np.uint8))
cv2.waitKey(0)
注意:注意numpy arrays 的數據類型。您不能將完整圖像的坐標存儲在np.uint8
數組中,因為它只包含 0-255 之間的值。 此外,在顯示時,您必須將第一個通道轉換回np.uint8
數據類型,因為 OpenCV 只能理解這種格式的圖像(對於 integer 像素值)。 這就是為什么我使用np.uint64
數據類型的數組來存儲像素值的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.