【问题标题】:Convert a black and white image to array of numbers?将黑白图像转换为数字数组?
【发布时间】:2018-12-14 18:34:28
【问题描述】:

如上图所示,如何将左侧的图像转换为表示0 for whitedecimals for darker colours closer to 1? as shown in the image usingpython 3 之间图像暗度的数组?

更新: 我试图在这方面做更多的工作。下面也有很好的答案。

# Load image 
filename = tf.constant("one.png")
image_file = tf.read_file(filename)

# Show Image
Image("one.png")

#convert method
def convertRgbToWeight(rgbArray):
    arrayWithPixelWeight = []
    for i in range(int(rgbArray.size / rgbArray[0].size)):
        for j in range(int(rgbArray[0].size / 3)):
            lum = 255-((rgbArray[i][j][0]+rgbArray[i][j][1]+rgbArray[i][j][2])/3) # Reversed luminosity
            arrayWithPixelWeight.append(lum/255) # Map values from range 0-255 to 0-1

    return arrayWithPixelWeight



# Convert image to numbers and print them
image_decoded_png = tf.image.decode_png(image_file,channels=3)
image_as_float32 = tf.cast(image_decoded_png, tf.float32)

numpy.set_printoptions(threshold=numpy.nan)
sess = tf.Session()
squeezedArray = sess.run(image_as_float32)

convertedList = convertRgbToWeight(squeezedArray)

print(convertedList) # This will give me an array of numbers. 

【问题讨论】:

  • 什么图片格式?
  • 我猜您的图像已经在左侧进行了插值以获得平滑的边框,对吧?您可以使用 numpy 包来有效地管理 python 数据。然后我猜你必须在考虑邻域的情况下插入值。
  • @samthegoodone 看到我的回答
  • @SamTheGoodOne 只是好奇你为什么要使用 TensorFlow 来完成诸如此类的简单图像处理任务。使用 cv/NumPy 更容易。
  • 您编辑后的问题更加没有重点。在你问如何做某事之前。现在你也在问你的方式是否还可以。为什么?你得到不正确的结果吗?它看起来冗长或低效吗?您选择可接受答案的标准是什么?

标签: python image numpy image-processing cv2


【解决方案1】:

我建议使用 opencv 读取图像。 opencv 最大的优点是它支持多种图像格式,并且它会自动将图像转换为 numpy 数组。例如:

import cv2
import numpy as np

img_path = '/YOUR/PATH/IMAGE.png'
img = cv2.imread(img_path, 0) # read image as grayscale. Set second parameter to 1 if rgb is required 

现在img 是一个numpy 数组,其值介于0 - 255 之间。默认情况下,0 等于黑色,255 等于白色。要更改这一点,您可以使用 opencv 内置函数bitwise_not

img_reverted= cv2.bitwise_not(img)

我们现在可以使用以下方法缩放数组:

new_img = img_reverted / 255.0  // now all values are ranging from 0 to 1, where white equlas 0.0 and black equals 1.0 

【讨论】:

  • 这将导致白色像素的值为 1,而问题需要黑色像素为 1。
  • 你能看看我的回答吗?我已经用一个可能的答案更新了这个问题。如果这也是使用 tensorflow 的一种可能方法,请发表评论
  • @SamTheGoodOne 对我来说,未经测试,您的代码似乎是合理的。但正如您可能注意到的那样,使用 tensorflow 进行图像处理要困难得多。这就是为什么我强烈推荐使用opencv。它让你的生活更轻松;)
  • 我是 tensorflow 和 python 的新手?。您的解决方案非常好。
【解决方案2】:

加载图像,然后反转并除以 255。

这是我用于此示例的图像 ('Untitled.png'):https://ufile.io/h8ncw

import numpy as np
import cv2
import matplotlib.pyplot as plt

my_img = cv2.imread('Untitled.png') 
inverted_img = (255.0 - my_img)  
final = inverted_img / 255.0

# Visualize the result
plt.imshow(final)
plt.show()

print(final.shape)
(661, 667, 3)

结果(最终对象表示为图像):

【讨论】:

  • 这将导致白色像素的值为 1,而问题需要黑色像素为 1。
  • 再看我的回答。
  • 为什么要投反对票?有什么解释吗?我的答案准确吗?
  • 我实际上赞成你的回答。对我来说很有意义。让我无法理解为什么有人会否决它。
  • 我也是。尤其是当他们不发表评论来解释原因时。谢谢老哥
【解决方案3】:

您必须从路径加载图像,然后将其转换为numpy array

图像的值将在 0 到 255 之间。下一步是标准化 numpy 数组。

希望对你有帮助。

【讨论】:

  • 如果您进一步充实您的答案会有所帮助,即通过解释或展示如何规范化数组。它会使它更加完整,因为不知道这意味着什么的人将不得不求助于第三方资源来学习。您还应该指出,0 指的是黑色和白色到 255,因为问题似乎想要白色 = 0 和黑色 = 1。
【解决方案4】:

您可以使用 PIL 包来管理图像。这是如何完成的示例。

from PIL import Image
image = Image.open('sample.png')
width, height = image.size
pixels = image.load()

# Check if has alpha, to avoid "too many values to unpack" error
has_alpha = len(pixels[0,0]) == 4

# Create empty 2D list
fill = 1
array = [[fill for x in range(width)] for y in range(height)]

for y in range(height):
    for x in range(width):
        if has_alpha:
            r, g, b, a = pixels[x,y]
        else:
            r, g, b = pixels[x,y]
        lum = 255-((r+g+b)/3) # Reversed luminosity
        array[y][x] = lum/255 # Map values from range 0-255 to 0-1

我认为它有效,但请注意,我所做的唯一测试是值是否在所需范围内:

# Test max and min values
h, l = 0,1
for row in array:
    h = max([max(row), h])
    l = min([min(row), l])
print(h, l)

【讨论】:

  • 你能看看我的回答吗?我已经用可能的答案更新了这个问题。如果这也是使用 tensorflow 的一种可能方法,请发表评论。
猜你喜欢
  • 1970-01-01
  • 2011-02-15
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2011-09-20
  • 2013-04-21
  • 2021-02-27
相关资源
最近更新 更多