11

I have the following code. I am trying to save a 16 bit depth image which I retrieve from Kinect v1 as a png file. I wrote the following code sample:

def display_depth(dev, data, timestamp):
    global keep_runningp    
    cv2.imshow('Depth', frame_convert2.pretty_depth_cv(data))
    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

It works when I add the following code, which converts data from a 16 bit unsigned to an 8 bit unsigned NumPy array:

depth = depth.astype(np.uint8)

Without this line, I am just getting the whole blank/white png image. But I need to have a 16 bit png file.

How I can save it as a 16 bit png file?

MIRMIX
  • 1,052
  • 2
  • 14
  • 40
  • 4
    If I create, say, `a = np.random.randint(0, 65536, size=(48, 48, 3)).astype(np.uint16)` and then run `cv2.imwrite('foo.png', a)`, I get a 16 bit RGB file. If instead I use `a = np.random.randint(0, 65535, size=(48, 48)).astype(np.uint16)`, I get a 16 bit grayscale image. In other words, `cv2.imwrite()` works for me. I think we need a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) to help you. – Warren Weckesser Jun 17 '17 at 16:18
  • 1
    you are saving `frame_convert2.pretty_depth(data)` this means you are NOT saving a 16 bit image but a 8 bit image. If you take a look to the [function implementation](https://github.com/OpenKinect/libfreenect/blob/master/wrappers/python/frame_convert2.py) it actually does `depth = depth.astype(np.uint8)` – api55 Jun 17 '17 at 18:25
  • I implemented my custom frame_convert and was importing that one. – MIRMIX Jun 19 '17 at 09:38

1 Answers1

7

Though type of my data was like this

<type 'numpy.uint16'>

Solution , to my problem was adding this line to my code

depth.astype(np.uint16)
MIRMIX
  • 1,052
  • 2
  • 14
  • 40