【发布时间】:2018-12-14 18:34:28
【问题描述】:
如上图所示,如何将左侧的图像转换为表示0 for white 和decimals 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