diff --git a/app/SandboxSetup/beamerlocationgui.h b/app/SandboxSetup/beamerlocationgui.h index b7e407abc3c4e12076040f358af16218b5f338ec..3010601d5bbe5bcdf4165d74cacc706272c12c9a 100644 --- a/app/SandboxSetup/beamerlocationgui.h +++ b/app/SandboxSetup/beamerlocationgui.h @@ -3,6 +3,7 @@ #include <QDialog> #include <QTimer> +#include <QMutex> #include <opencv2/opencv.hpp> #include "qtfullscreen.h" #include "monitorgui.h" diff --git a/app/SandboxSetup/croppingmask.cpp b/app/SandboxSetup/croppingmask.cpp index f96a754bbb6df7ed1aa01ca9a0f2694a38d62e90..d68e1810372a5e95f71adc68c01c6b4db0169e56 100644 --- a/app/SandboxSetup/croppingmask.cpp +++ b/app/SandboxSetup/croppingmask.cpp @@ -28,13 +28,17 @@ CroppingMask::~CroppingMask() } void CroppingMask::valideRoutine(){ + timer->stop(); + setup->getCamera()->capture(); cv::Size s = setup->getCamera()->getDepthFrame().size(); cv::Point center(s.width / 2, s.height / 2); std::vector<cv::Point> rectPoints = getRectPoints(); + cv::Mat depthFrame = setup->getCamera()->getDepthFrame(); setup->setupAdjustMatrix(rectPoints, center); setup->setupCroppingMask(rectPoints); + setup->getProjection()->setDistanceTopSandbox(depthFrame.at<float>(center)); endSuccess = true; } diff --git a/inc/beamerProjection.h b/inc/beamerProjection.h index 0da8db80b7ced86604aafd2585ef082caf9b7aa8..6d492c71175719e64c3c5b8dec9c9cddb1aa61e9 100644 --- a/inc/beamerProjection.h +++ b/inc/beamerProjection.h @@ -8,7 +8,7 @@ class BeamerProjection{ private: cv::Mat adjustingMatrix; - float topSandboxDistance = 1.0f; + float distanceTopSandbox = 1.0f; cv::Point2i findMatchingPixel(int i, int j, float z, Camera *camera, cv::Point3f beamer_pos); void copyPixelsInto(cv::Point2i pixel_dst, cv::Mat &dst, cv::Point2i pixel_src, cv::Mat &src, cv::Rect base); @@ -18,7 +18,9 @@ class BeamerProjection{ BeamerProjection(); void setAdjustingMatrix(cv::Mat matrix){ adjustingMatrix = matrix; } - cv::Mat getAdjustingMatrix(){ return adjustingMatrix.clone(); } + cv::Mat getAdjustingMatrix(){ return adjustingMatrix; } + void setDistanceTopSandbox(float dist){ distanceTopSandbox = dist; }; + float getDistanceTopSandbox(){ return distanceTopSandbox; }; cv::Point2i rotatePixel(cv::Point2i pixel); void adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Camera *camera, cv::Point3f beamer_pos); diff --git a/inc/sandbox.h b/inc/sandbox.h index b6b749701229adb4865bd1005b79f1a447dc680f..182a86aed35beb21caa78b9998cf0c5d096a09a3 100644 --- a/inc/sandbox.h +++ b/inc/sandbox.h @@ -21,6 +21,10 @@ class Sandbox{ public: Sandbox(); + Camera* getCamera(){ return &camera; }; + Beamer* getBeamer(){ return &beamer; }; + BeamerProjection* getProjection(){ return &projection; }; + void init(); cv::Mat getColorFrame(); cv::Mat getDepthFrame(); diff --git a/inc/sandboxConfig.h b/inc/sandboxConfig.h index 3898d465c1b93ac29c4bf4d647b513f01daceb4d..0cef219e0b4244efa2ea8fee0340549a63ae9997 100644 --- a/inc/sandboxConfig.h +++ b/inc/sandboxConfig.h @@ -7,11 +7,12 @@ #include "beamerProjection.h" #include "beamer.h" -#define MASK "CroppingMask" -#define POSITION "BeamerPosition" -#define MATRIX "AdjustingMatrix" -#define RESOLUTION "BeamerResolution" -#define PROCESSPROFIL "FrameProcessProfil" +#define SCFG_MASK "CroppingMask" +#define SCFG_POSITION "BeamerPosition" +#define SCFG_MATRIX "AdjustingMatrix" +#define SCFG_DISTANCE "DistanceTopSandbox" +#define SCFG_RESOLUTION "BeamerResolution" +#define SCFG_PROCESS_PROFIL "FrameProcessProfil" class SandboxConfig{ private: @@ -19,12 +20,14 @@ class SandboxConfig{ public: static int saveAdjustingMatrixInto(char *path, cv::Mat matrix); + static int saveDistanceToSandboxTop(char *path, float distance); static int saveCroppingMaskInto(char *path, cv::Rect mask); static int saveBeamerPositionInto(char *path, cv::Point3f position); static int saveBeamerResolutionInto(char *path, cv::Size resolution); static int saveFrameProcessProfil(char *path, FrameProcessProfil profil); static int loadAdjustingMatrixFrom(char *path, BeamerProjection *projection); + static int loadDistanceToSandboxTop(char *path, BeamerProjection *projection); static int loadCroppingMaskFrom(char *path, Camera *camera); static int loadBeamerPositionFrom(char *path, Beamer *beamer); static int loadBeamerResolutionFrom(char *path, Beamer *beamer); diff --git a/src/components/beamer.cpp b/src/components/beamer.cpp index e68745c5486652f237bc273eb62fc0a7ab032fc4..a1dbd372f78f9b79272439acac6b02f13db7b7ab 100644 --- a/src/components/beamer.cpp +++ b/src/components/beamer.cpp @@ -241,8 +241,8 @@ std::vector<cv::Point3i> Beamer::findCircles(cv::Mat &rgb, double contrast, int // min_dist : Minimal distance between the detected centers // param_1 : Upper threshold of the canny edge detector, determines if a pixel is an edge // param_2 : Threshold for the center detection, after the accumulator is complet, threshold to keep only the possible centers - // min_radius : Min radius of the circles drew on the accumulator - // max_radius : Max radius of the circles drew on the accumulator + // min_radius : Min radius of the circles drawn on the accumulator + // max_radius : Max radius of the circles drawn on the accumulator cv::HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, centersMinDist, (double)cannyEdgeThreshold, (double)houghAccThreshold, minRadius, maxRadius); std::vector<cv::Point3i> result; diff --git a/src/components/beamerProjection.cpp b/src/components/beamerProjection.cpp index 180c2caecf1e6c2abf6049b981fc1251b375e89a..da649fed1b47a358c9e0dc0fa64e86ade0c6693b 100644 --- a/src/components/beamerProjection.cpp +++ b/src/components/beamerProjection.cpp @@ -19,13 +19,12 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam cv::Rect mask = camera->getCroppingMask(); cv::Size dst_size = dst.size(); - cv::resize(src, src, dst.size()); // resize the frames to be a multiple of the camera size : // src.size = n * camera.depth.size , where n is uint > 0 - cv::resize(src, src, getMatchingSize(src, mask)); - cv::resize(dst, dst, src.size()); - + cv::resize(dst, dst, getMatchingSize(dst, mask)); + cv::resize(src, src, dst.size()); + 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)); @@ -79,10 +78,9 @@ void BeamerProjection::adjustFrame(cv::Mat depth, cv::Mat src, cv::Mat &dst, Cam } } } + - - - cv::resize(src, src, dst_size); + cv::resize(dst, dst, dst_size); cv::warpAffine(dst, dst, adjustingMatrix, dst.size()); } @@ -131,9 +129,8 @@ cv::Point2i BeamerProjection::findMatchingPixel(int i, int j, float z, Camera *c float pixel[2] = {static_cast<float>(i), static_cast<float>(j)}; cv::Point3f CP = camera->deprojectPixelToPoint(pixel, z); cv::Point3f BP = CB - CP; - //float alpha = (topSandboxDistance - CP.z) / BP.z; float BAz = CB.z + BP.z; - float BEz = topSandboxDistance - CB.z; + float BEz = distanceTopSandbox - CB.z; float alpha = BEz / BAz; cv::Point3f V = (alpha * BP); cv::Point3f CV = V + CB; diff --git a/src/lib/sandbox.cpp b/src/lib/sandbox.cpp index 2bda963e1f3fdc6bf644aff6f1a7c2b9f928f97f..98f22066c95c36a97a414cf9aa9309f4974821a8 100644 --- a/src/lib/sandbox.cpp +++ b/src/lib/sandbox.cpp @@ -36,8 +36,8 @@ cv::Mat Sandbox::adjustProjection(cv::Mat frame){ projection.adjustFrame(depth, frame, imageCalibrate, &camera, beamer.getPosition()); // frame after process - cv::dilate(imageCalibrate, imageCalibrate, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); - cv::erode(imageCalibrate, imageCalibrate, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); + //cv::dilate(imageCalibrate, imageCalibrate, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); + //cv::erode(imageCalibrate, imageCalibrate, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); return imageCalibrate; } @@ -53,6 +53,8 @@ int Sandbox::loadConfigFrom(char *path){ int err = SandboxConfig::loadAdjustingMatrixFrom(path, &projection); if(err){ return err; } + err = SandboxConfig::loadDistanceToSandboxTop(path, &projection); + if(err){ return err; } err = SandboxConfig::loadCroppingMaskFrom(path, &camera); if(err){ return err; } err = SandboxConfig::loadBeamerResolutionFrom(path, &beamer); @@ -64,13 +66,7 @@ int Sandbox::loadConfigFrom(char *path){ int Sandbox::loadConfig(){ - int err = loadConfigFrom(defaultConfigFilePath); - if(err){ - std::cout << "error config" << std::endl; - return err; - } - - return 0; + return loadConfigFrom(defaultConfigFilePath); } void Sandbox::initWindowsFullScreen(){ diff --git a/src/lib/sandboxSetup.cpp b/src/lib/sandboxSetup.cpp index b78e6da63fb57c5b816cf463eab41bd31c8e4729..ef179b7519fb4484cd01f068d8b1b00bb8fb85c0 100644 --- a/src/lib/sandboxSetup.cpp +++ b/src/lib/sandboxSetup.cpp @@ -14,6 +14,9 @@ SandboxSetup::SandboxSetup(){ int SandboxSetup::saveConfigFrom(char *path){ if(SandboxConfig::saveAdjustingMatrixInto(path, projection.getAdjustingMatrix())) return 1; + + if(SandboxConfig::saveDistanceToSandboxTop(path, projection.getDistanceTopSandbox())) + return 1; if(SandboxConfig::saveCroppingMaskInto(path, camera.getCroppingMask())) return 1; diff --git a/src/tools/sandboxConfig.cpp b/src/tools/sandboxConfig.cpp index a88493df546967e996526351ffebc2297239d085..c75c292d14da82a49228bbbe7ac30128ce3affe5 100644 --- a/src/tools/sandboxConfig.cpp +++ b/src/tools/sandboxConfig.cpp @@ -26,7 +26,26 @@ int SandboxConfig::saveAdjustingMatrixInto(char *path, cv::Mat matrix){ //std::cout << "[Error] No Config File found : " << err.what() << std::endl; } - config[MATRIX] = val; + config[SCFG_MATRIX] = val; + + return saveConfigIn(path, config); +} + + +int SandboxConfig::saveDistanceToSandboxTop(char *path, float distance){ + + YAML::Node val; + + val["distance"] = distance; + + YAML::Node config; + try{ + config = YAML::LoadFile(path); + }catch(YAML::BadFile err){ + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; + } + + config[SCFG_DISTANCE] = val; return saveConfigIn(path, config); } @@ -48,7 +67,7 @@ int SandboxConfig::saveCroppingMaskInto(char *path, cv::Rect mask){ //std::cout << "[Error] No Config File found : " << err.what() << std::endl; } - config[MASK] = val; + config[SCFG_MASK] = val; return saveConfigIn(path, config); } @@ -69,7 +88,7 @@ int SandboxConfig::saveBeamerPositionInto(char *path, cv::Point3f pos){ //std::cout << "[Error] No Config File found : " << err.what() << std::endl; } - config[POSITION] = val; + config[SCFG_POSITION] = val; return saveConfigIn(path, config); } @@ -79,8 +98,8 @@ int SandboxConfig::saveBeamerResolutionInto(char *path, cv::Size res){ YAML::Node val; - val["height"] = res.height; val["width"] = res.width; + val["height"] = res.height; YAML::Node config; try{ @@ -89,7 +108,7 @@ int SandboxConfig::saveBeamerResolutionInto(char *path, cv::Size res){ //std::cout << "[Error] No Config File found : " << err.what() << std::endl; } - config[RESOLUTION] = val; + config[SCFG_RESOLUTION] = val; return saveConfigIn(path, config); } @@ -114,7 +133,7 @@ int SandboxConfig::saveFrameProcessProfil(char *path, FrameProcessProfil profil) //std::cout << "[Error] No Config File found : " << err.what() << std::endl; } - config[PROCESSPROFIL] = val; + config[SCFG_PROCESS_PROFIL] = val; return saveConfigIn(path, config); } @@ -132,23 +151,23 @@ int SandboxConfig::loadAdjustingMatrixFrom(char *path, BeamerProjection *project try{ config = YAML::LoadFile(path); }catch(YAML::BadFile err){ - std::cout << "[Error] No Config File found : " << err.what() << std::endl; + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; return 1; } // no node for adjusting matrix - if(!config[MATRIX]){ + if(!config[SCFG_MATRIX]){ return 2; }; // uncomplet data for adjusting matrix - if(!(config[MATRIX]["width"] && config[MATRIX]["height"] && config[MATRIX]["matrix"])){ + if(!(config[SCFG_MATRIX]["width"] && config[SCFG_MATRIX]["height"] && config[SCFG_MATRIX]["matrix"])){ return 2; } - int height = config[MATRIX]["height"].as<int>(); - int width = config[MATRIX]["width"].as<int>(); - std::vector<float> values = config[MATRIX]["matrix"].as<std::vector<float>>(); + int height = config[SCFG_MATRIX]["height"].as<int>(); + int width = config[SCFG_MATRIX]["width"].as<int>(); + std::vector<float> values = config[SCFG_MATRIX]["matrix"].as<std::vector<float>>(); // verify the number of values in the matrix if(height*width != (int)values.size()){ @@ -162,16 +181,38 @@ int SandboxConfig::loadAdjustingMatrixFrom(char *path, BeamerProjection *project for (int j=0; j<width; j++){ matrix.at<float>(i,j) = values.at(v_i); v_i++; - std::cout << matrix.at<float>(i,j) << " "; } - std::cout << std::endl; } - - - std::cout << "load matrix" << std::endl; + projection->setAdjustingMatrix(matrix); - std::cout << "Ok matrix" << std::endl; + return 0; +} + + +int SandboxConfig::loadDistanceToSandboxTop(char *path, BeamerProjection *projection){ + + YAML::Node config; + + try{ + config = YAML::LoadFile(path); + }catch(YAML::BadFile err){ + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; + return 1; + } + + if(!config[SCFG_DISTANCE]){ + return 2; + }; + + if(!(config[SCFG_DISTANCE]["distance"])){ + return 2; + } + + float distance = config[SCFG_DISTANCE]["distance"].as<float>(); + + projection->setDistanceTopSandbox(distance); + return 0; } @@ -183,24 +224,24 @@ int SandboxConfig::loadCroppingMaskFrom(char *path, Camera *camera){ try{ config = YAML::LoadFile(path); }catch(YAML::BadFile err){ - std::cout << "[Error] No Config File found : " << err.what() << std::endl; + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; return 1; } // no node for cropping mask - if(!config[MASK]){ + if(!config[SCFG_MASK]){ return 2; }; // uncomplet data for cropping mask - if(!(config[MASK]["x"] && config[MASK]["y"] && config[MASK]["width"] && config[MASK]["height"])){ + if(!(config[SCFG_MASK]["x"] && config[SCFG_MASK]["y"] && config[SCFG_MASK]["width"] && config[SCFG_MASK]["height"])){ return 2; } - cv::Rect mask( cv::Point( config[MASK]["x"].as<int>(), - config[MASK]["y"].as<int>()), - cv::Size( config[MASK]["width"].as<int>(), - config[MASK]["height"].as<int>())); + cv::Rect mask( cv::Point( config[SCFG_MASK]["x"].as<int>(), + config[SCFG_MASK]["y"].as<int>()), + cv::Size( config[SCFG_MASK]["width"].as<int>(), + config[SCFG_MASK]["height"].as<int>())); camera->setCroppingMask(mask); @@ -215,23 +256,23 @@ int SandboxConfig::loadBeamerPositionFrom(char *path, Beamer *beamer){ try{ config = YAML::LoadFile(path); }catch(YAML::BadFile err){ - std::cout << "[Error] No Config File found : " << err.what() << std::endl; + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; return 1; } // no node for beamer position - if(!config[POSITION]){ + if(!config[SCFG_POSITION]){ return 2; }; // uncomplet data for beamer position - if(!(config[POSITION]["x"] && config[POSITION]["y"] && config[POSITION]["z"])){ + if(!(config[SCFG_POSITION]["x"] && config[SCFG_POSITION]["y"] && config[SCFG_POSITION]["z"])){ return 2; } - cv::Point3f pos( config[POSITION]["x"].as<float>(), - config[POSITION]["y"].as<float>(), - config[POSITION]["z"].as<float>()); + cv::Point3f pos( config[SCFG_POSITION]["x"].as<float>(), + config[SCFG_POSITION]["y"].as<float>(), + config[SCFG_POSITION]["z"].as<float>()); beamer->setPosition(pos); @@ -246,22 +287,22 @@ int SandboxConfig::loadBeamerResolutionFrom(char *path, Beamer *beamer){ try{ config = YAML::LoadFile(path); }catch(YAML::BadFile err){ - std::cout << "[Error] No Config File found : " << err.what() << std::endl; + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; return 1; } // no node for beamer resolution - if(!config[RESOLUTION]){ + if(!config[SCFG_RESOLUTION]){ return 2; }; // uncomplet data for beamer resolution - if(!(config[RESOLUTION]["height"] && config[RESOLUTION]["width"])){ + if(!(config[SCFG_RESOLUTION]["height"] && config[SCFG_RESOLUTION]["width"])){ return 2; } - cv::Size res( config[RESOLUTION]["width"].as<int>(), - config[RESOLUTION]["height"].as<int>()); + cv::Size res( config[SCFG_RESOLUTION]["width"].as<int>(), + config[SCFG_RESOLUTION]["height"].as<int>()); beamer->setWidth(res.width); beamer->setHeight(res.height); @@ -277,34 +318,34 @@ int SandboxConfig::loadFrameProcessProfil(char *path, FrameProcessProfil *profil try{ config = YAML::LoadFile(path); }catch(YAML::BadFile err){ - std::cout << "[Error] No Config File found : " << err.what() << std::endl; + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; return 1; } // no node for frame process profil - if(!config[PROCESSPROFIL]){ + if(!config[SCFG_PROCESS_PROFIL]){ return 2; }; // uncomplet data for frame process profil - if(!( config[PROCESSPROFIL]["contrast"] && - config[PROCESSPROFIL]["brightness"] && - config[PROCESSPROFIL]["minDistance"] && - config[PROCESSPROFIL]["cannyEdgeThreshold"] && - config[PROCESSPROFIL]["houghAccThreshold"] && - config[PROCESSPROFIL]["minRadius"] && - config[PROCESSPROFIL]["maxRadius"] + if(!( config[SCFG_PROCESS_PROFIL]["contrast"] && + config[SCFG_PROCESS_PROFIL]["brightness"] && + config[SCFG_PROCESS_PROFIL]["minDistance"] && + config[SCFG_PROCESS_PROFIL]["cannyEdgeThreshold"] && + config[SCFG_PROCESS_PROFIL]["houghAccThreshold"] && + config[SCFG_PROCESS_PROFIL]["minRadius"] && + config[SCFG_PROCESS_PROFIL]["maxRadius"] )){ return 2; } - profil->setContrast(config[PROCESSPROFIL]["contrast"].as<double>()); - profil->setBrightness(config[PROCESSPROFIL]["brightness"].as<int>()); - profil->setMinDistance(config[PROCESSPROFIL]["minDistance"].as<uint>()); - profil->setCannyEdgeThreshold(config[PROCESSPROFIL]["cannyEdgeThreshold"].as<uint>()); - profil->setHoughAccThreshold(config[PROCESSPROFIL]["houghAccThreshold"].as<uint>()); - profil->setMinRadius(config[PROCESSPROFIL]["minRadius"].as<uint>()); - profil->setMaxRadius(config[PROCESSPROFIL]["maxRadius"].as<uint>()); + profil->setContrast(config[SCFG_PROCESS_PROFIL]["contrast"].as<double>()); + profil->setBrightness(config[SCFG_PROCESS_PROFIL]["brightness"].as<int>()); + profil->setMinDistance(config[SCFG_PROCESS_PROFIL]["minDistance"].as<uint>()); + profil->setCannyEdgeThreshold(config[SCFG_PROCESS_PROFIL]["cannyEdgeThreshold"].as<uint>()); + profil->setHoughAccThreshold(config[SCFG_PROCESS_PROFIL]["houghAccThreshold"].as<uint>()); + profil->setMinRadius(config[SCFG_PROCESS_PROFIL]["minRadius"].as<uint>()); + profil->setMaxRadius(config[SCFG_PROCESS_PROFIL]["maxRadius"].as<uint>()); return 0; }