Skip to content
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

How to crop rs2::frame #11140

Closed
AlexKanakis5 opened this issue Nov 23, 2022 · 4 comments
Closed

How to crop rs2::frame #11140

AlexKanakis5 opened this issue Nov 23, 2022 · 4 comments

Comments

@AlexKanakis5
Copy link

Required Info
Camera Model D415
Firmware Version 05.13.00.50
Operating System & Version Linux (Ubuntu 20)
Kernel Version (Linux Only) 5.15.0-53-generic
Platform PC
SDK Version 2
Language C++
Segment others

Issue Description

<Describe your issue / question / feature request / etc..>

Hello,

I want to crop a frame to specific dimensions. From what I understand, I have to convert it to Mat, crop it and then convert it back to a frame. I have seen the issue #2634 on how to convert Mat to frame, and the code works when applying Gaussian blur as the example. However when I crop the Mat and then convert it, the resulting frame is not cropped. I am new and I am really struggling with this, any help would be much appreciated.
realsense_crop.txt

@MartyG-RealSense
Copy link
Collaborator

Hi @AlexKanakis5 There is a further discussion of this cropping method at #8512 if you have not seen it already

A couple of different approaches to cropping are at #2016 (comment) and #3894 (comment)

@AlexKanakis5
Copy link
Author

I didn't get an answer to my original question but these suggestions really helped. What I wanted was to create a point cloud from the copped frame. What I ended up doing was to follow what the #2016 comment described with some help from the link in the #3894 comment. For anyone in a similar situation as me this is the methodology I followed:

  1. Get the pixels of the box that would be used to crop the image
  2. Convert them to points by using the rs2_deproject_pixel_to_point function
  3. Iterate over the points and only keep the points inside the cropped part

Here is a snippet from my code that I used to convert the pixels to points. This does not work on its own but it gives an idea on how to get there.

auto profile = pipe.start(cfg);
rs2::frame depth = frames.get_depth_frame();

// get calibration data for pixel to vertix convertion 
auto stream = profile.get_stream(RS2_STREAM_DEPTH).as<rs2::video_stream_profile>();
auto intrinsics = stream.get_intrinsics(); // Calibration data
rs2_intrinsics intr = depth.get_profile().as<rs2::video_stream_profile>().get_intrinsics(); // Calibration data

// this gets pixel values of bounding box and converts them to 3D points 
float startpixel[2]; // From pixel
float startpoint[3]; // From point (in 3D)
pixel start = {171,43}; // pixel value
startpixel[0] = start.first;
startpixel[1] = start.second;
auto startdist = depth.get_distance(startpixel[0], startpixel[1]);
rs2_deproject_pixel_to_point(startpoint, &intr, startpixel, startdist);

@MartyG-RealSense
Copy link
Collaborator

It's great to hear that you achieved a solution, @AlexKanakis5 - thanks so much for sharing it with the RealSense community!

@MartyG-RealSense
Copy link
Collaborator

Case closed due to solution achieved and no further comments received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants