pyrealsense2.align
클래스는 깊이 이미지와 다른 이미지 간의 정렬을 수행__init__(self: pyrealsense2.align, align_to: pyrealsense2.stream)
→ Nonealign_to
매개변수에 다른 스트림 타입을 설정align_to
매개변수를 RS2_STREAM_DEPTH
로 설정__init__(self, align_to)
align_to
매개변수에 다른 스트림 타입을 설정합니다.as_decimation_filter(self)
as_disparity_transform(self)
as_hdr_merge(self)
as_hole_filling_filter(self)
as_sequence_id_filter(self)
as_spatial_filter(self)
as_temporal_filter(self)
as_threshold_filter(self)
get_info(self, arg0)
get_option(self, option)
get_option_description(self, option)
get_option_range(self, option)
get_option_value(self, arg0)
get_option_value_description(self, option, value)
get_supported_options(self)
invoke(self, f)
is_decimation_filter(self)
is_disparity_transform(self)
is_hdr_merge(self)
is_hole_filling_filter(self)
is_option_read_only(self, option)
is_sequence_id_filter(self)
is_spatial_filter(self)
is_temporal_filter(self)
is_threshold_filter(self)
on_options_changed(self, callback)
process(self, frames)
set_option(self, option, value)
set_option_value(self, arg0, arg1)
start(self, callback)
supports(self, arg0)
rs.pipeline()
을 통해 카메라 스트리밍을 관리할 파이프라인을 생성합니다. rs.config()
를 통해 스트림을 설정할 수 있는 설정 객체를 생성pipeline_wrapper
와 pipeline_profile
을 통해 현재 연결된 카메라 장치 정보를 가져옵니다. pipeline.start(config)
으로 스트리밍을 시작하고, 카메라에서 데이터를 실시간으로 가져옵니다.depth_sensor.get_depth_scale()
로 깊이 데이터를 실제 거리 단위로 변환하는 스케일 값을 가져옵니다. rs.align()
을 통해 깊이 이미지를 컬러 이미지에 맞추기 위한 정렬 객체를 생성align_to = rs.stream.color
로 설정pipeline.wait_for_frames()
를 통해 깊이 및 컬러 프레임 세트를 가져오고, align.process(frames)
로 깊이 프레임을 컬러 프레임에 맞추어 정렬np.where()
함수를 사용해 클리핑 거리보다 먼 거리에 있는 물체들을 제거하고, 배경을 회색으로 설정 cv2.applyColorMap()
을 사용해 깊이 이미지를 컬러맵(Jet)을 적용하여 시각적으로 보기 쉽게 변환하고, np.hstack()
으로 배경 제거된 컬러 이미지와 깊이 이미지를 나란히 출력합니다.pipeline.stop()
으로 스트리밍을 종료합니다.요약하자면, 이 코드는 RealSense 카메라의 깊이 이미지와 컬러 이미지를 정렬하고, 1미터 이상 떨어진 물체를 배경으로 처리한 후 그 결과를 화면에 표시하는 기능을 합니다.
## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#####################################################
## Align Depth to Color ##
#####################################################
# First import the library
import pyrealsense2 as rs
# Import Numpy for easy array manipulation
import numpy as np
# Import OpenCV for easy image rendering
import cv2
# Create a pipeline
pipeline = rs.pipeline()
# Create a config and configure the pipeline to stream
# different resolutions of color and depth streams
config = rs.config()
# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))
found_rgb = False
for s in device.sensors:
if s.get_info(rs.camera_info.name) == 'RGB Camera':
found_rgb = True
break
if not found_rgb:
print("The demo requires Depth camera with Color sensor")
exit(0)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
# Start streaming
profile = pipeline.start(config)
# Getting the depth sensor's depth scale (see rs-align example for explanation)
depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()
print("Depth Scale is: " , depth_scale)
# We will be removing the background of objects more than
# clipping_distance_in_meters meters away
clipping_distance_in_meters = 1 #1 meter
clipping_distance = clipping_distance_in_meters / depth_scale
# Create an align object
# rs.align allows us to perform alignment of depth frames to others frames
# The "align_to" is the stream type to which we plan to align depth frames.
align_to = rs.stream.color
align = rs.align(align_to)
# Streaming loop
try:
while True:
# Get frameset of color and depth
frames = pipeline.wait_for_frames()
# frames.get_depth_frame() is a 640x360 depth image
# Align the depth frame to color frame
aligned_frames = align.process(frames)
# Get aligned frames
aligned_depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
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())
color_image = np.asanyarray(color_frame.get_data())
# Remove background - Set pixels further than clipping_distance to grey
grey_color = 153
depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels
bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)
# Render images:
# depth align to color on left
# depth on right
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
images = np.hstack((bg_removed, depth_colormap))
cv2.namedWindow('Align Example', cv2.WINDOW_NORMAL)
cv2.imshow('Align Example', images)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
pipeline.stop()