3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【RealSense】RGB画像でクリックした位置の取得

Last updated at Posted at 2021-02-11

RealSenseのカメラ画像上でクリックした点に対応した3次情報を取得するプログラムです

GetClickPoint.py
# coding: utf-8
import pyrealsense2 as rs
import numpy as np
import cv2

def get_click_point(event,x,y,flags,param):
    global depth_frame

    if event == cv2.EVENT_LBUTTONDOWN:

        if x>=640:
            return

        depth = depth_frame.get_distance(x, y)
        point = np.array([x,y,depth])
        if depth==0 :
            print("ERROR: cannot get depth data ! ")
            return 

        x=point[0]
        y=point[1]
        z=point[2]

        x,y,z=rs.rs2_deproject_pixel_to_point(color_intrinsics, [x, y], z)

        print("point:",[x,y,z])

pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

print("Start streaming")
pipeline.start(config)

cv2.namedWindow('RealsenseImage', cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback('RealsenseImage',get_click_point)

while cv2.waitKey(1)<0:

    frames = pipeline.wait_for_frames()
    depth_frame = frames.get_depth_frame()
    color_frame = frames.get_color_frame()

    color_intrinsics = color_frame.profile.as_video_stream_profile().intrinsics

    depth_image = np.asanyarray(depth_frame.get_data())
    color_image = np.asanyarray(color_frame.get_data())

    depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
    images = np.hstack((color_image, depth_colormap))

    cv2.imshow("RealsenseImage",images)

RealSenseのRGB画像を左クリックするとその3次元位置情報が出力されます

Screenshot from 2021-02-11 23-47-14.png

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?