fixing the repetitive wave structure in depth map when writing to disk

79 Views Asked by At

My depth map has no problem when I visualize it using rviz2 however when I save it using astype(np.uint16) after multiplying the depth values by 1000 as mentioned by the author of BundleSDF git repo here, it saves depth images in problematic shape.

If I use this script to save depth images:

from pathlib import Path
from rosbags.highlevel import AnyReader
import numpy as np
from PIL import Image
from datetime import datetime
from matplotlib import image
import cv2
import matplotlib.pyplot as plt



with AnyReader([Path('/home/mona/rosbag2_2023_11_06-15_44_24')]) as reader:
    connections = [x for x in reader.connections if x.topic == '/camera/camera/aligned_depth_to_color/image_raw']

    for connection, timestamp, rawdata in reader.messages(connections=connections):
        msg = reader.deserialize(rawdata, connection.msgtype)
        timestamp_dt = datetime.fromtimestamp(msg.header.stamp.sec + msg.header.stamp.nanosec * 1e-9)
        timestamp_str = timestamp_dt.strftime("%Y-%m-%d %H:%M:%S.%f")


        timestamp_ns = msg.header.stamp.sec * 1e9 + msg.header.stamp.nanosec
        numeric_timestamp = int(timestamp_ns / 1e-9)
        image_data = msg.data.reshape(480, 640,-1)*1000
        
        # Take only the first channel (grayscale)
        grayscale_image = image_data[:, :, 0]
        depth_image_name = 'depth/' + str(numeric_timestamp)[:20] + '.png'
        cv2.imwrite(depth_image_name, grayscale_image.astype(np.uint16))

I get this:

enter image description here

and if I use this slightly modified script to save images

from pathlib import Path
from rosbags.highlevel import AnyReader
import numpy as np
from PIL import Image
from datetime import datetime
from matplotlib import image
import cv2
import matplotlib.pyplot as plt

with AnyReader([Path('/home/mona/rosbag2_2023_11_06-15_44_24')]) as reader:
    connections = [x for x in reader.connections if x.topic == '/camera/camera/aligned_depth_to_color/image_raw']

    for connection, timestamp, rawdata in reader.messages(connections=connections):
        msg = reader.deserialize(rawdata, connection.msgtype)
        timestamp_dt = datetime.fromtimestamp(msg.header.stamp.sec + msg.header.stamp.nanosec * 1e-9)
        timestamp_str = timestamp_dt.strftime("%Y-%m-%d %H:%M:%S.%f")
        timestamp_ns = msg.header.stamp.sec * 1e9 + msg.header.stamp.nanosec
        numeric_timestamp = int(timestamp_ns / 1e-9)        
        w, h = msg.width, msg.height
        image_data = msg.data.reshape(h, w,-1)*1000
        
        # Take only the first channel (grayscale)
        grayscale_image = image_data[:, :, 0]
        depth_image_name = 'depth/' + str(numeric_timestamp)[:20] + '.png'
        depth_data = np.array(grayscale_image, dtype=np.uint16)
        image16 = cv2.UMat(depth_data)
        cv2.imwrite(depth_image_name, image16.get())
  


    

I get this:

enter image description here

My ros2 bag is healthy and has no problem. Also, since this was a depth aligned capture, the rgb images are healthy and saved in correct format.

How can I fix this problem and save the depth images correctly?

Related links:

  1. https://github.com/NVlabs/BundleSDF/issues/82#issuecomment-1698248648

  2. https://github.com/NVlabs/BundleSDF/issues/82#issuecomment-1699580570

  3. https://github.com/NVlabs/BundleSDF/issues/74#issuecomment-1681430819

My cup data as shown by identify command is following the same format as milk data captured by author of BundleSDF git repo.

(base) mona@ada:~/BundleSDF/milk/2022-11-18-15-10-24_milk/depth$ identify ../../../cup/depth/16993034935581901703.png
../../../cup/depth/16993034935581901703.png PNG 640x480 640x480+0+0 16-bit Grayscale Gray 115375B 0.000u 0:00.000
(base) mona@ada:~/BundleSDF/milk/2022-11-18-15-10-24_milk/depth$ identify 1668813100171987935.png
1668813100171987935.png PNG 640x480 640x480+0+0 16-bit Grayscale Gray 49234B 0.000u 0:00.000

Some system info:

(base) mona@ada:~$ ros2 wtf
/opt/ros/humble/lib/python3.10/site-packages/ros2doctor/api/package.py: 112: UserWarning: joy has been updated to a new version. local: 3.1.0 < latest: 3.3.0
/opt/ros/humble/lib/python3.10/site-packages/ros2doctor/api/package.py: 112: UserWarning: sdl2_vendor has been updated to a new version. local: 3.1.0 < latest: 3.3.0

All 5 checks passed
(base) mona@ada:~$ /usr/bin/python3.10
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux

(base) mona@ada:~$ uname -a
Linux ada 6.2.0-36-generic #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  9 15:34:04 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
(base) mona@ada:~$ lsb_release -a
LSB Version:    core-11.1.0ubuntu4-noarch:security-11.1.0ubuntu4-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:    22.04
Codename:   jammy

The RGB image saved looks like this:

enter image description here

The process for capture is:

  1. open a terminal and type this command: ros2 launch realsense2_camera rs_launch.py align_depth.enable:=true

  2. open a new terminal and type this command: ros2 bag record -a

  3. press CTL+C to stop recording

  4. run the script

checking the depth aligned capture in rviz2 I see my depth aligned depth raw image and raw color image like the following and it doesn't seem if there is a problem: enter image description here

These are the topics I am showing in rviz2:

enter image description here

enter image description here

0

There are 0 best solutions below