在Python中将3D 32位floatarray保存为48位整数PNG以匹配Kitti Ground Truth表单

2024-05-01 21:55:53 发布

您现在位置:Python中文网/ 问答频道 /正文

基蒂有一个光流基准。他们要求流估计是48位PNG文件,以匹配他们的地面真相文件的格式。你知道吗

地面实况PNG图像可用于download here

Kitti有一个Matlab开发包,用于估计值与地面真实值的比较。你知道吗

我想从我的网络输出流作为48位整数PNG文件,这样我的流估计可以与其他Kitti基准流估计进行比较。你知道吗

来自网络的numpy缩放流文件是downloadable from here

但是,在python中将float32 3D数组流转换为3通道48位文件(每个通道16位)时遇到了问题,因为图像库提供者似乎不支持这一点,或者因为我的代码有问题。有人能帮忙吗?你知道吗

我试过很多不同的图书馆,读过很多帖子。你知道吗

不幸的是,Scipy输出的png只有24位。 使用scipyavailable here生成的输出流估计png

# Numpy Flow to 48bit PNG with 16bits per channel

import scipy as sp
from scipy import misc
import numpy as np
import png
import imageio
import cv2
from PIL import Image
from matplotlib import image

"""From Kitti DevKit:-

Optical flow maps are saved as 3-channel uint16 PNG images: The first 
channel
contains the u-component, the second channel the v-component and the 
third
channel denotes if the pixel is valid or not (1 if true, 0 otherwise). To 
convert
the u-/v-flow into floating point values, convert the value to float, 
subtract 2^15 and divide the result by 64.0:"""

Scaled_Flow = np.load('Scaled_Flow.npy') # This is a 32bit float
# This is the very first Kitti Test Flow Output from image_2 testing folder  
# passed through DVF
# The network that produced this flow is only trained to 51 steps, so it 
# won't provide an accurate correspondence
# But the Estimated Flow PNG should look green

ones = np.float32(np.ones((2,375,1242,1))) # Kitti devkit readme says 
that third channel is 1 if flow is valid for that pixel
# 2 for batch size, 3 for height, 3 for width, 1 for this extra layer of 
ones.
with_ones = np.concatenate((Scaled_Flow, ones), axis=3)

im = sp.misc.toimage(with_ones[-1,:,:,:], cmin=-1.0, cmax=1.0) # saves image object
im.save("Scipy_24bit.png", dtype="uint48") # Outputs 24bit only.

Flow = np.int16(with_ones) # An attempt at converting the format from 
float 32 to 16 bit integers
f512 = Flow * 512 # Kitti instructs that the flows are scaled by 512.

x = np.array(Scaled_Flow)
x.astype(np.uint16) # another attempt at converting it to unsigned 16 bit 
integers

try: # try PyPNG
    with open('PyPNGuint48bit.png', 'wb') as f:
        writer = png.Writer(width=375, height=1242, bitdepth=16)
        # Convert z to the Python list of lists expected by
        # the png writer.
        #z2list = x.reshape(-1, x.shape[1]*x.shape[2]).tolist()
        writer.write(f, x)
except:
    print("png lib approach didn't work, it might be to do with the 
sizing")

try: # try imageio
    imageio.imwrite('imageio_Flow_48bit.png', x, format='PNG-FI')
except:
    print("imageio approach didn't work, it probably couldn't handle the 
datatype")

try: # try OpenCV
    cv2.imwrite('OpenCVFlow_48bit_.png',x )
except:
    print("OpenCV approach didn't work, it probably couldn't handle the 
datatype")

try: #try: # try PIL
    im = Image.fromarray(x)
    im.save("PILLOW_Flow_48bit.png", format="PNG")
except:
    print("PILLOW approach didn't work, it probably couldn't handle the 
datatype")

try: # try Matplotlib
    image.imsave('MatplotLib_Flow_48bit.png', x)
except:
    print("Matplotlib approach didn't work, ValueError: object too deep 
for desired array")'''

我想得到一个48位的png文件,和Kitti Ground truth一样 看起来是绿色的。当前,Scipy输出一个24位的png文件,该文件为蓝色和蓝色 脸色苍白。你知道吗


Tags: 文件thetofromimportpngiswith
1条回答
网友
1楼 · 发布于 2024-05-01 21:55:53

以下是我对你的理解:

  1. Scaled_Flow.npy加载数据。这是一个具有形状(23751242,2)的32位浮点numpy数组。你知道吗
  2. Scaled_Flow[1](形状为(375,1242,2)的数组)转换为16位无符号整数:

    • 乘以64
    • 添加2**15,和
    • 将值强制转换为np.uint16。你知道吗

    这与您引用的描述相反:“要将u-/v流转换为浮点值,请将该值转换为浮点值,减去2^15,然后将结果除以64.0”。

  3. 通过串联所有1的数组,将第三维的长度从2增加到3
  4. 将结果保存到PNG文件。你知道吗

有一种方法你可以做到。要创建PNG文件,我将使用^{},这是我为从numpy数组创建PNG和动画PNG文件而编写的库。如果给numpngw.write_png一个数据类型为np.uint16的numpy数组,它将创建一个每个通道16位的PNG文件(在本例中为48位图像)。你知道吗

import numpy as np
from numpngw import write_png


Scaled_Flow = np.load('Scaled_Flow.npy')
sf16 = (64*Scaled_Flow[-1] + 2**15).astype(np.uint16)
imgdata = np.concatenate((sf16, np.ones(sf16.shape[:2] + (1,), dtype=sf16.dtype)), axis=2)

write_png('sf48.png', imgdata)

下面是该脚本创建的图像。你知道吗

png file

相关问题 更多 >