How to grab, save and filter images
This topic will explain how to grab, save and filter images.
Grab and save a depth images
The code below can be used to grab a depth image and save it.
Context about the code can be found in the comments.
import cv2
import matplotlib.pyplot as plt
from matplotlib import pylab
import numpy as np
from pyexiv2 import Image
import pyrealsense2 as rs
import time
# Create a context object. This object owns the handles to all connected realsense devices
pipeline = rs.pipeline()
pipeline.start()
depth = None
i = 0
# Create a pipeline object. This object configures the streaming camera and owns it's handle
while not depth:
i += 1
frames = pipeline.wait_for_frames()
depth = frames.get_depth_frame()
print(f"Grabbing frame took this many tries: {i}")
depth_data = depth.as_frame().get_data()
np_image = np.asanyarray(depth_data) # Do not use this image for distances!
height, width = np_image.shape # Get resolution
cv2.imwrite("testimg.png", np_image)
# Create an empty array with float values and calculate the distance for each pixel
distarray = np.empty([height, width ], dtype=np.float)
for y in range(height):
for x in range(width):
distarray[y,x] = depth.get_distance(x, y)
# Don't call stop before all operations on the depth image are completed! This will break the get_distance function.
pipeline.stop()
max_dist = np.max(distarray)
print(f"max_dist = {max_dist}")
# Create normalize function and apply this function to the array with distances
normalize = lambda t: t / max_dist * 65535 # 16 bit int!
vfunc = np.vectorize(normalize)
normalized_distarray = vfunc(distarray)
# Convert the normalized image with floats to uint16 to get rid of the decimals
img = normalized_distarray.astype(np.uint16)
plt.imshow(img, cmap='gray')
# Write imag and add metadata. You need the max_dist to convert the values back to distances.
filename = "normalized.png"
cv2.imwrite(filename, img)
with Image(filename) as img:
img.modify_xmp({'Xmp.dc.max_dist': f"{max_dist}"})
# Read out the metadata for confirmation
with Image(filename) as img:
data = img.read_xmp()
print(data)
Filter images
Intel has a great example on their Github:
depth_filters.ipynb
You can download it and use it in jupyter notebook. For those that want to apply images directly to depth frames from a Realsense camera I attached a slighly modified version that will use the camera instead of a device from file.
depth_filters.ipynb (10.0 KB)
Good luck!