[英]How can I convert an image from pixels to one-hot encodings?
I have a PNG image I am loading within Tensorflow using: 我有一个PNG图像我在Tensorflow中加载使用:
image = tf.io.decode_png(tf.io.read_file(path), channels=3)
The image contains pixels that match a lookup like this: 该图像包含与查找匹配的像素,如下所示:
image_colors = [
(0, 0, 0), # black
(0.5, 0.5, 0.5), # grey
(1, 0.5, 1), # pink
]
How can I convert it so that the output has the pixels mapped into one-hot encodings where the hot component would be the matching color? 我如何转换它,以便输出将像素映射到一个热编码,其中热组件将是匹配的颜色?
Let me assume for convenience that all values in image_colors
are in [0, 255]
: 让我假设
image_colors
中的所有值都在[0, 255]
:
image_colors = [
(0, 0, 0), # black
(127, 127, 127), # grey
(255, 127, 255), # pink
]
My approach maps pixels into one-hot values as follows: 我的方法将像素映射到一个热值,如下所示:
# Create a "color reference" tensor from image_colors
color_reference = tf.cast(tf.constant(image_colors), dtype=tf.uint8)
# Load the image and obtain tensor with one-hot values
image = tf.io.decode_png(tf.io.read_file(path), channels=3)
comp = tf.equal(image[..., None, :], color_reference)
one_hot = tf.cast(tf.reduce_all(comp, axis=-1), dtype=tf.float32)
Note that you can easily add new colors to image_colors
without changing the TF implementation. 请注意,您可以轻松地向
image_colors
添加新颜色,而无需更改TF实现。 Also, this assumes that all pixels in the image
are in image_colors
. 此外,这假设
image
中的所有像素都在image_colors
。 If that is not the case, one could define a range for each color and then use other tf.math
operations (eg tf.greater
and tf.less
) instead of tf.equal
. 如果不是这种情况,可以为每种颜色定义一个范围,然后使用其他
tf.math
操作(例如tf.greater
和tf.less
)而不是tf.equal
。
There might be better approaches than this. 可能有比这更好的方法。
def map_colors(pixel):
if pixel[0] < 10 and pixel[1] < 10 and pixel[2] < 10: ## Black
return 0
elif pixel[0] > 245 and pixel[1] > 245 and pixel[2] > 245: ## White
return 1
else:
return 11
image = tf.io.decode_png(tf.io.read_file(path), channels=3)
img_shape = image.shape
# Arrange the pixels in RGB format from 3D array to 2D array.
image = tf.reshape(image, [-1, 3])
colors = tf.map_fn(lambda x: map_colors(x), image)
one_hot = tf.one_hot(colors, depth=12)
print(one_hot)
# If necessary
image_one_hot = tf.reshape(one_hot, [img_shape[0], img_shape[1], 12])
Ensure that in map_colors
, you put all your 12 colors with the range of RGB color values they can accept. 确保在
map_colors
,将所有12种颜色放在他们可以接受的RGB颜色值范围内。 Make sure that all the combinations are covered or else, add an extra class of None of the above and name it 12. 确保涵盖所有组合,否则,添加一个额外的上述类别,并将其命名为12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.