2017-06-17 1354 views
1

我有以下代碼。我試圖保存一個16位深度的圖像,我從Kinect v1中檢索一個png文件。我寫了下面的代碼示例:imwrite 16位png深度圖像

def display_depth(dev, data, timestamp): 
    global keep_runningp  
    #cv2.imshow('Depth', frame_convert2.pretty_depth_cv(data)) 
    cv2.imshow('Depth', frame_convert2.pretty_depth_cv(data)) 
    print type(data) 
    print data.shape 
    depthf.write(repr(timestamp)+" Depth/"+repr(timestamp)+".png\n") 
    namef="Sample_dataset/Depth/"+repr(timestamp)+".png"  
    cv2.imwrite(namef,frame_convert2.pretty_depth(data))  
    if cv2.waitKey(10) == 27:   
     keep_running = False 

它的工作原理,當我添加以下代碼,它從一個16位無符號的數據轉換爲8位無符號NumPy的數組:

depth = depth.astype(np.uint8) 

沒有這一行,我只是得到整個空白/白色PNG圖像。但我需要有一個16位的PNG文件。

我如何將它保存爲16位PNG文件?

+1

如果我創建,比方說,' a = np.random.randint(0,65536,size =(48,48,3))。astype(np.uint16)''然後運行'cv2.imwrite('foo.png',a)',我得到一個16位的RGB文件。如果我使用'a = np.random.randint(0,65535,size =(48,48))。astype(np.uint16)',我會得到一個16位的灰度圖像。換句話說,'cv2.imwrite()'適用於我。我認爲我們需要一個[最小,完整且可驗證的示例](https://stackoverflow.com/help/mcve)來幫助您。 –

+1

您正在保存'frame_convert2.pretty_depth(data)'這意味着您不保存16位圖像,而是保存8位圖像。如果你看一下[函數實現](https://github.com/OpenKinect/libfreenect/blob/master/wrappers/python/frame_convert2.py),它實際上是'depth = depth.astype(np.uint8) ' – api55

+0

我實現了我的自定義frame_convert並且正在導入那個。 – MIRMIX

回答

0

雖然類型我的數據是這樣的

<type 'numpy.uint16'>

解決方案,我的問題是加入這行來我的代碼

depth.astype(np.uint16)