Skip to content
Snippets Groups Projects
Commit 2dfcac8e authored by simon.fanetti's avatar simon.fanetti
Browse files

fixed pixels deprojection

parent e069476e
Branches
No related tags found
1 merge request!1Camera
...@@ -133,7 +133,7 @@ void BeamerLocationGui::userValidePoint(){ ...@@ -133,7 +133,7 @@ void BeamerLocationGui::userValidePoint(){
if(!circle.empty()){ if(!circle.empty()){
capturedPoints.push_back( beamer->deprojectPixel(circle.at(0), &depth, camera) ); capturedPoints.push_back( beamer->deprojectPixel(cv::Point2i(circle.at(0).x, circle.at(0).y), &depth, camera) );
updateLabelSteps(); updateLabelSteps();
// enough points to perform linear regression and move into next step // enough points to perform linear regression and move into next step
......
...@@ -84,7 +84,7 @@ class Beamer{ ...@@ -84,7 +84,7 @@ class Beamer{
int findBeamerFrom(Camera *camera); int findBeamerFrom(Camera *camera);
cv::Mat editContrast(cv::Mat image, double contrast, int brightness); cv::Mat editContrast(cv::Mat image, double contrast, int brightness);
cv::Point3f deprojectPixel(cv::Point3i circle, cv::Mat *depth, Camera *camera); cv::Point3f deprojectPixel(cv::Point2i circle, cv::Mat *depth, Camera *camera);
std::vector<cv::Point2i> getCrossList(); std::vector<cv::Point2i> getCrossList();
cv::Mat getCrossFrame(cv::Point2i projectedCross, int step, int max, bool circlesFound); cv::Mat getCrossFrame(cv::Point2i projectedCross, int step, int max, bool circlesFound);
cv::Point3d approximatePosition(std::vector<cv::Point3d> *bases, std::vector<cv::Point3d> *directions); cv::Point3d approximatePosition(std::vector<cv::Point3d> *bases, std::vector<cv::Point3d> *directions);
......
...@@ -25,6 +25,7 @@ class Camera{ ...@@ -25,6 +25,7 @@ class Camera{
Camera(); Camera();
~Camera(); ~Camera();
// return a float matrix of depth in meters
cv::Mat getDepthFrame(){ cv::Mat getDepthFrame(){
cv::Mat meters; cv::Mat meters;
cv::Mat values = cv::Mat(cv::Size(depth_frame->get_width(), depth_frame->get_height()), CV_16UC1, (void *)depth_frame->get_data(), cv::Mat::AUTO_STEP); cv::Mat values = cv::Mat(cv::Size(depth_frame->get_width(), depth_frame->get_height()), CV_16UC1, (void *)depth_frame->get_data(), cv::Mat::AUTO_STEP);
......
...@@ -21,6 +21,7 @@ class Sandbox{ ...@@ -21,6 +21,7 @@ class Sandbox{
public: public:
Sandbox(); Sandbox();
void init();
cv::Mat getColorFrame(); cv::Mat getColorFrame();
cv::Mat getDepthFrame(); cv::Mat getDepthFrame();
cv::Mat adjustProjection(cv::Mat frame); cv::Mat adjustProjection(cv::Mat frame);
......
...@@ -64,7 +64,7 @@ int Beamer::findBeamerFrom(Camera *camera){ ...@@ -64,7 +64,7 @@ int Beamer::findBeamerFrom(Camera *camera){
} }
else if (keyCode == ' '){ else if (keyCode == ' '){
if(!circles.empty()){ if(!circles.empty()){
capturedPoints.push_back( deprojectPixel(circles.at(0), &depth, camera) ); capturedPoints.push_back( deprojectPixel( cv::Point2i(circles.at(0).x, circles.at(0).y ), &depth, camera) );
} }
} }
} }
...@@ -81,12 +81,10 @@ int Beamer::findBeamerFrom(Camera *camera){ ...@@ -81,12 +81,10 @@ int Beamer::findBeamerFrom(Camera *camera){
return 0; return 0;
} }
cv::Point3f Beamer::deprojectPixel(cv::Point3i circle, cv::Mat *depth, Camera *camera){ cv::Point3f Beamer::deprojectPixel(cv::Point2i circle, cv::Mat *depth, Camera *camera){
float coord[2] = {(float)circle.x, (float)circle.y}; float coord[2] = {(float)circle.x, (float)circle.y};
float z = static_cast<float>(depth->at<uint16_t>(circle.y, circle.x)); float z = depth->at<float>(circle.y, circle.x);
// rs2.get_depth_frame().get_units() return camera->deprojectPixelToPoint(coord, z);
float depth_unit = 1 / 1000.0;
return camera->deprojectPixelToPoint(coord, z * depth_unit);
} }
void Beamer::findLinearLineFrom(std::vector<cv::Point3f> *capturedPoints, std::vector<cv::Point3d> *bases, std::vector<cv::Point3d> *directions){ void Beamer::findLinearLineFrom(std::vector<cv::Point3f> *capturedPoints, std::vector<cv::Point3d> *bases, std::vector<cv::Point3d> *directions){
......
...@@ -25,12 +25,14 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam ...@@ -25,12 +25,14 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam
// src.size = n * camera.depth.size , where n is uint > 0 // src.size = n * camera.depth.size , where n is uint > 0
cv::resize(src, src, getMatchingSize(src, mask)); cv::resize(src, src, getMatchingSize(src, mask));
cv::resize(dst, dst, src.size()); cv::resize(dst, dst, src.size());
//src.copyTo(dst);
cv::Mat pixelsDeprojectMap = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
cv::Mat pixelsDeprojectHighestMap = cv::Mat_<cv::Point2i>(mask.height, mask.width, cv::Point2i(-1,-1));
// Browse the depth frame matching the cropping mask // Browse the depth frame matching the cropping mask
// while adapting pixels's position to the beamer's position // while adapting pixels's position to the beamer's position
for (int j = 0; j < mask.height; j++){ for (int j = 0; j < depth.rows; j++){
for (int i = 0; i < mask.width; i++){ for (int i = 0; i < depth.cols; i++){
// coordinates of the pixel relative to the orginial image taken from the camera // coordinates of the pixel relative to the orginial image taken from the camera
int base_x = mask.x+i; int base_x = mask.x+i;
...@@ -40,17 +42,46 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam ...@@ -40,17 +42,46 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam
// pixels based on the original depth frame taken from the camera // pixels based on the original depth frame taken from the camera
cv::Point2i pixel = findMatchingPixel( base_x, base_y, z, camera, beamer_pos ); cv::Point2i pixel = findMatchingPixel( base_x, base_y, z, camera, beamer_pos );
// pixel relative to the cropping mask // pixel relative to the cropping mask (the area where the frame is projected)
pixel.x -= mask.x; pixel.x -= mask.x;
pixel.y -= mask.y; pixel.y -= mask.y;
if( (0<=pixel.x && pixel.x<mask.width) && (0<=pixel.y && pixel.y<mask.height) ){ pixelsDeprojectMap.at<cv::Point2i>(j,i) = pixel;
}
}
for (int j = 0; j < pixelsDeprojectMap.rows; j++){
for (int i = 0; i < pixelsDeprojectMap.cols; i++){
cv::Point2i pixel = pixelsDeprojectMap.at<cv::Point2i>(j,i);
if(pixel.x != -1 && pixel.y != -1){
// check and keep the highest point at the location pointed by pixel
cv::Point2i defaultPoint = pixelsDeprojectHighestMap.at<cv::Point2i>(j,i);
if(defaultPoint.x != -1 && defaultPoint.y != -1){
if(depth.at<float>(defaultPoint) <= depth.at<float>(pixel))
pixel = defaultPoint;
}
pixelsDeprojectHighestMap.at<cv::Point2i>(j,i) = pixel;
}
}
}
for (int j = 0; j < pixelsDeprojectHighestMap.rows; j++){
for (int i = 0; i < pixelsDeprojectHighestMap.cols; i++){
cv::Point2i pixel = pixelsDeprojectHighestMap.at<cv::Point2i>(j,i);
if( (0<=pixel.x && pixel.x<depth.cols) && (0<=pixel.y && pixel.y<depth.rows) ){
// src and dst must be of same size // src and dst must be of same size
copyPixelsInto(pixel, dst, cv::Point2i(i,j), src, mask); copyPixelsInto(pixel, dst, cv::Point2i(i,j), src, mask);
} }
} }
} }
cv::resize(src, src, dst_size); cv::resize(src, src, dst_size);
cv::warpAffine(dst, dst, adjustingMatrix, dst.size()); cv::warpAffine(dst, dst, adjustingMatrix, dst.size());
} }
......
...@@ -54,6 +54,8 @@ int Camera::start(){ ...@@ -54,6 +54,8 @@ int Camera::start(){
// Doc presets : https://dev.intelrealsense.com/docs/d400-series-visual-presets // Doc presets : https://dev.intelrealsense.com/docs/d400-series-visual-presets
sensor.set_option(RS2_OPTION_VISUAL_PRESET, RS2_RS400_VISUAL_PRESET_HIGH_DENSITY); sensor.set_option(RS2_OPTION_VISUAL_PRESET, RS2_RS400_VISUAL_PRESET_HIGH_DENSITY);
//sensor.set_option(RS2_OPTION_VISUAL_PRESET, RS2_RS400_VISUAL_PRESET_DEFAULT);
// 5 = range mapped to unlimited // 5 = range mapped to unlimited
spatFilter->set_option(RS2_OPTION_HOLES_FILL, 5); spatFilter->set_option(RS2_OPTION_HOLES_FILL, 5);
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
*/ */
Sandbox::Sandbox(){ Sandbox::Sandbox(){
camera.start();
} }
...@@ -14,6 +14,10 @@ Sandbox::Sandbox(){ ...@@ -14,6 +14,10 @@ Sandbox::Sandbox(){
* PUBLIC * PUBLIC
*/ */
void Sandbox::init(){
camera.start();
}
cv::Mat Sandbox::getColorFrame(){ cv::Mat Sandbox::getColorFrame(){
camera.capture(); camera.capture();
return camera.getColorFrame()(camera.getCroppingMask()); return camera.getColorFrame()(camera.getCroppingMask());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment