将黑白图像转换为数字数组?

时间:2018-07-06 07:52:06

标签: python image numpy image-processing cv2

The image is 28 pixels by 28 pixels. They can interpret this as a big array of numbers: 就像上面的图片所示,如何将左边的图片转换成表示0 for whitedecimals for darker colours closer to 1? as shown in the image using python 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. 

4 个答案:

答案 0 :(得分:4)

我建议使用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等于白色。要更改此设置,您可以使用内置函数bitwise_not的opencv:

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)

加载图像,然后将其反转并除以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)

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

final image

答案 2 :(得分:0)

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

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

希望有帮助。

答案 3 :(得分:0)

您可以使用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)