-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get aligned color and depth with python #4934
Comments
@kjacks21 hello, several remarks:
First decide which direction alignment will be applied -
frameset = pipeline.wait_for_frames(timeout_ms=100)
if frameset.size() <2:
#Inputs are not ready yet
break |
@ev-mp thank you! Issue resolved. In case others want to see how I did this, I saved the color stream as .png and the depth stream as an array of scaled matrices saved as a .npy file. If there is a better way to do this please let me know, and if you'd like me to make a PR for python examples I'd be happy to since I didn't see anything on how to save the color and depth stream in the current examples. Code below: import argparse
import pyrealsense2 as rs
import numpy as np
import cv2
import os
def extract_from_bag(bag_fname, color_fname, depth_fname):
config = rs.config()
pipeline = rs.pipeline()
# make it so the stream does not continue looping
config.enable_stream(rs.stream.color)
config.enable_stream(rs.stream.depth)
rs.config.enable_device_from_file(config, bag_fname, repeat_playback=False)
profile = pipeline.start(config)
# this makes it so no frames are dropped while writing video
playback = profile.get_device().as_playback()
playback.set_real_time(False)
colorizer = rs.colorizer()
align_to = rs.stream.color
align = rs.align(align_to)
depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
depth_matrices = []
i = 0
while True:
# when stream is finished, RuntimeError is raised, hence this
# exception block to capture this
try:
# frames = pipeline.wait_for_frames()
frames = pipeline.wait_for_frames(timeout_ms=100)
if frames.size() <2:
# Inputs are not ready yet
continue
except (RuntimeError):
print('frame count', i-1)
pipeline.stop()
break
# align the deph to color frame
aligned_frames = align.process(frames)
# get aligned frames
aligned_depth_frame = aligned_frames.get_depth_frame()
color_frame = aligned_frames.get_color_frame()
# validate that both frames are valid
if not aligned_depth_frame or not color_frame:
continue
depth_image = np.asanyarray(aligned_depth_frame.get_data())
scaled_depth_image = depth_image * depth_scale
color_image = np.asanyarray(color_frame.get_data())
# convert color image to BGR for OpenCV
r, g, b = cv2.split(color_image)
color_image = cv2.merge((b, g, r))
depth_colormap = np.asanyarray(colorizer.colorize(aligned_depth_frame).get_data())
images = np.hstack((color_image, depth_colormap))
cv2.namedWindow('Aligned Example', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Aligned Example', images)
fname = "frame{:06d}".format(i) + ".png"
cv2.imwrite(color_fname + fname, color_image)
depth_matrices.append(scaled_depth_image)
# color_out.write(color_image)
if (cv2.waitKey(1) & 0xFF) == ord('q'):
break
i += 1
# release everything now that job finished
np.save(depth_fname, np.array(depth_matrices))
print("Size of depth matrices:", len(depth_matrices))
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", type=str, help=".bag file to read")
parser.add_argument("-c", "--rgbfilename", type=str, help=".mp4 file to save RGB stream")
parser.add_argument("-d", "--depthfilename", type=str, help=".npy file to save depth stream")
args = parser.parse_args()
extract_from_bag(bag_fname=args.input, color_fname=args.rgbfilename, depth_fname=args.depthfilename) |
Hi kjacks21, |
@NguyenKhanh27 Try using I'm also not sure I follow why it saves as a 3x3 matrix. |
After running, an error will be reported and how to solve it. |
@L-xn you're running out of memory. You may need write chunks of the array to disk at specific intervals to avoid exceeding your memory. |
|
@666tua See the answer here for loading the data: #4934 (comment) The depth image should have the same x,y dimensions as the color stream. To get the corresponding depth values for each key point for a given frame, use the key point coordinates to index the depth image. |
There are multiple issues on this but I haven't found one that resolves my issue. I originally posted this to #4306 but realized it was a thread about the matlab wrapper.
When I align the color and depth frames and try to access both, I always get the following error as a result of running the code below (assuming the function is properly called):
Error
Code
The example here only has one frame and other issues about extracting different types of frames using conditionals didn't resolve my issue.
The text was updated successfully, but these errors were encountered: