這個問題在這里已經有了答案: 使用 NumPy 將固定調色板應用于影像? (3 個回答) 10 小時前關閉。
我有一個尺寸為 (224, 224, 3) 的 3D NumPy 陣列。第三個維度代表顏色。我在整個圖片中只有 21 種顏色,所以每個像素只有 21 個不同的值。
這些是顏色代碼:
colors = np.array([
[ 0, 0, 0], [128, 0, 0], [ 0, 128, 0], [128, 128, 0], [ 0, 0, 128],
[128, 0, 128], [ 0, 128, 128], [128, 128, 128], [ 64, 0, 0], [192, 0, 0],
[ 64, 128, 0], [192, 128, 0], [ 64, 0, 128], [192, 0, 128], [ 64, 128, 128],
[192, 128, 128], [ 0, 64, 0], [128, 64, 0], [ 0, 192, 0], [128, 192, 0],
[ 0, 64, 128]], dtype=np.uint8)
如何迭代每個像素并創建一個形狀為 (224, 224) 的二維陣列,該陣列由基于第三維值的整數 0 到 20 組成?最有效的方法是什么?
uj5u.com熱心網友回復:
讓我們將顏色視為基于 4 的位置系統,然后我們可以創建兩種散列函式來從基于 4 的系統轉換為基于 10 的系統并向后轉換:
import numpy as np
from typing import Sequence
# I have changed the order I guess
colors = np.array(
[
[0, 0, 0],
[64, 0, 0],
[128, 0, 0],
[192, 0, 0],
[0, 64, 0],
[64, 64, 0],
[128, 64, 0],
[192, 64, 0],
[0, 128, 0],
[64, 128, 0],
[128, 128, 0],
[192, 128, 0],
[0, 192, 0],
[64, 192, 0],
[128, 192, 0],
[192, 192, 0],
[0, 0, 64],
[64, 0, 64],
[128, 0, 64],
[192, 0, 64],
[0, 64, 64],
],
dtype=np.uint8,
)
def color_to_value(cell: Sequence[int]):
mult = 1
value = 0
for sub in cell:
value = (sub // 64) * mult
mult *= 4
return value
_MAP = (
0,
64,
128,
192,
)
def value_to_color(value: int):
seq = [0] * 3
for i in range(3):
reminder = value % 4
value //= 4
seq[i] = _MAP[reminder]
return seq
for i in range(21):
print(value_to_color(i))
img = np.random.random((16, 16, 3)) * 255
converted = np.array(
[[color_to_value(cell) for cell in row] for row in img], dtype=np.int8
)
print(converted)
uj5u.com熱心網友回復:
使用np.unique.
以下是一些用于測驗的樣板代碼。
IMAGE_SHAPE = (224, 224) # Given image shape
# Given colors.
colors = np.array([
[ 0, 0, 0], [128, 0, 0], [ 0, 128, 0], [128, 128, 0], [ 0, 0, 128],
[128, 0, 128], [ 0, 128, 128], [128, 128, 128], [ 64, 0, 0], [192, 0, 0],
[ 64, 128, 0], [192, 128, 0], [ 64, 0, 128], [192, 0, 128], [ 64, 128, 128],
[192, 128, 128], [ 0, 64, 0], [128, 64, 0], [ 0, 192, 0], [128, 192, 0],
[ 0, 64, 128]], dtype=np.uint8)
# Generate a random image with pixel colors taken from 'colors'.
image = colors[np.random.randint(0, colors.shape[0], IMAGE_SHAPE)]
運行print(image)將產生一個(224, 224, 3)看起來像的陣列
array([[[128, 128, 0],
[ 0, 128, 0],
[192, 0, 0],
...,
[192, 0, 0],
[128, 0, 0],
[ 64, 0, 128]],
...,
[[ 64, 128, 0],
[128, 128, 128],
[ 0, 64, 128],
...,
[ 64, 0, 0],
[192, 128, 128],
[192, 0, 0]]], dtype=uint8)
要將影像轉換為像素顏色索引陣列,請呼叫np.unique為
_colors, index_image = np.unique(image.reshape(-1, 3), return_inverse=True)
index_image = index_image.reshape(IMAGE_SHAPE)
該陣列_colors將包含相同的像素值colors(盡管可能以不同的順序),并且index_image將是一個(224, 224)陣列,其中包含給出每個像素索引的條目_colors:
print(_colors)
print(index_image.shape)
print(index_image)
結果是
array([[ 0, 0, 0],
[ 0, 0, 128],
[ 0, 64, 0],
[ 0, 64, 128],
[ 0, 128, 0],
[ 0, 128, 128],
[ 0, 192, 0],
[ 64, 0, 0],
[ 64, 0, 128],
[ 64, 128, 0],
[ 64, 128, 128],
[128, 0, 0],
[128, 0, 128],
[128, 64, 0],
[128, 128, 0],
[128, 128, 128],
[128, 192, 0],
[192, 0, 0],
[192, 0, 128],
[192, 128, 0],
[192, 128, 128]], dtype=uint8)
(224, 224)
array([[14, 4, 17, ..., 17, 11, 8],
[ 9, 7, 11, ..., 5, 17, 7],
...,
[ 8, 10, 0, ..., 1, 6, 16],
[ 9, 15, 3, ..., 7, 20, 17]])
可以通過索引簡單地反轉映射_colors:
print(_colors[index_image])
print(np.all(_color[index_image] == image))
生產
array([[[128, 128, 0],
[ 0, 128, 0],
[192, 0, 0],
...,
[192, 0, 0],
[128, 0, 0],
[ 64, 0, 128]],
...,
[[ 64, 128, 0],
[128, 128, 128],
[ 0, 64, 128],
...,
[ 64, 0, 0],
[192, 128, 128],
[192, 0, 0]]], dtype=uint8)
True
uj5u.com熱心網友回復:
由于您的顏色代碼是唯一的,您可以考慮對每個顏色條進行編碼,然后用于np.searchsorted搜索排序后的代碼:
>>> m = 256 ** np.arange(3)
>>> coded = colors @ m
>>> perm = coded.argsort()
>>> sort = coded[perm]
>>> img = colors[np.random.choice(len(colors), 4)].reshape(2, 2, 3)
>>> result = perm[np.searchsorted(sort, img.reshape(-1, 3) @ m)].reshape(img.shape[:-1])
>>> img
array([[[128, 0, 128],
[128, 64, 0]],
[[ 64, 0, 128],
[128, 128, 128]]], dtype=uint8)
>>> result
array([[ 5, 17],
[12, 7]], dtype=int64)
>>> np.all(colors[result] == img)
True
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/483810.html
標籤:Python 数组 图片 麻木的 numpy-ndarray
上一篇:組合陣列元素并發送回應
下一篇:如何從嵌套結構中生成JSON
