imwrite 16 bit png depth image
Asked Answered
A

1

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?

Alyssa answered 17/6, 2017 at 15:25 Comment(3)
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 to help you.Nucleus
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 it actually does depth = depth.astype(np.uint8)Importunate
I implemented my custom frame_convert and was importing that one.Alyssa
A
8

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)
Alyssa answered 19/6, 2017 at 9:35 Comment(1)
Thanks for the fix, was pulling my hair out over this.Lanford

© 2022 - 2024 — McMap. All rights reserved.