diff --git a/includes/beamer.h b/includes/beamer.h index 93eaab6b285dc47c6a6f81f24f8ac35c9c3ad98b..cce6df0a35cc63f00eefc3831fffeeb7eae90f40 100644 --- a/includes/beamer.h +++ b/includes/beamer.h @@ -21,5 +21,7 @@ public: int getWidth(){ return width; }; int getHeight(){ return height; }; void findBeamerFrom(Camera camera); + + void printPosition(); }; #endif diff --git a/includes/beamerProjection.h b/includes/beamerProjection.h index 28c11e525d0750f7da376cdc62e21d6ee8b605ff..366b2483aa8826d7e27add5a2a6d9a9658283caf 100644 --- a/includes/beamerProjection.h +++ b/includes/beamerProjection.h @@ -16,7 +16,8 @@ public: cv::Point2i rotatePixel(cv::Point2i pixel); void adjustFrame(cv::Mat &src, cv::Mat &dst, Camera camera, cv::Point3f beamer); void adjustFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, Camera camera, cv::Point3f beamer); - void setAdjustingMatrix(cv::Mat matrix){ adjustingMatrix = matrix.clone(); } + void setAdjustingMatrix(cv::Mat matrix){ matrix.clone(); } cv::Mat getAdjustingMatrix(){ return adjustingMatrix.clone(); } + void printAdjustingMatrix(); }; #endif \ No newline at end of file diff --git a/includes/camera.h b/includes/camera.h index e5e0dc4dd229e06d098c284633999f7a17861cec..0cc36ca4b4b669fe24ead233dea9a6e8c9ccbb4f 100644 --- a/includes/camera.h +++ b/includes/camera.h @@ -40,5 +40,7 @@ public: cv::Mat getRGBFrameAlign(){ return matRGB.clone(); }; void setCroppingMask(cv::Rect mask){ croppingMask = mask; }; cv::Rect getCroppingMask(){ return croppingMask; }; + + void printCroppingMask(); }; #endif \ No newline at end of file diff --git a/includes/controller.h b/includes/controller.h index 3b85f26306ab4bcb2209bc7c53e9ed75eb720987..2b6f55589224d73a79f7cb619a4867613b1d2a72 100644 --- a/includes/controller.h +++ b/includes/controller.h @@ -10,24 +10,26 @@ class Controller { - public: - Controller(); - cv::Mat getRGBFrame(); - cv::Mat getDepthFrame(); - void showImage(cv::Mat* image); - void loadConfig(); - void loadConfigFrom(char *path); - void initWindowsFullScreen(); - private: static const int ESCAPE_CHAR = 27; char *defaultConfigFilePath = (char *)"./sandbox_conf.yaml"; - char *defaultWindowsName = (char*) "Image"; + char *defaultWindowsName = (char*) "ShowApp"; BeamerProjection projection; Camera camera; Beamer beamer; void initWindowsFullScreen(char *windowName); void showImage(cv::Mat* image, char *windowName); + + public: + Controller(); + cv::Mat getRGBFrame(); + cv::Mat getDepthFrame(); + cv::Mat* adjustProjection(cv::Mat* frame); + void showImage(cv::Mat* image); + int loadConfig(); + int loadConfigFrom(char *path); + void initWindowsFullScreen(); + }; #endif diff --git a/includes/sandbox.h b/includes/sandbox.h index 90e55e5542d5fb07f6dfa081d746349898cbf98a..594fca61a5d463eb666180bb37964fc2d2b69aa0 100644 --- a/includes/sandbox.h +++ b/includes/sandbox.h @@ -6,14 +6,17 @@ class Sandbox { + private: + Controller controller; + public: Sandbox(); cv::Mat getRGBFrame(); cv::Mat getDepthFrame(); + cv::Mat* adjustProjection(cv::Mat* frame); + void initWindowsFullScreen(); void showImage(cv::Mat* image); - void loadConfig(); - - private: - Controller controller; + int loadConfig(); + }; #endif diff --git a/src/core/components/beamer.cpp b/src/core/components/beamer.cpp index 25f5760b5bb05b3cef691521228110ddd98fb26d..97d2aa72a88611fd38364a070d8107cb225f61d7 100644 --- a/src/core/components/beamer.cpp +++ b/src/core/components/beamer.cpp @@ -113,6 +113,9 @@ void Beamer::findBeamerFrom(Camera camera) vector<Point3d> points2; //1 point for each vector (to calculate constante d) double fact = -20.0; + + camera.startAlign(); + for (int i = 0; i < (int)points.size(); i++) { vector<Point3f> capturedPoints; @@ -158,6 +161,8 @@ void Beamer::findBeamerFrom(Camera camera) } } + camera.stop(); + Point3d pa, pb; double mua; double mub; @@ -186,3 +191,9 @@ void Beamer::findBeamerFrom(Camera camera) destroyAllWindows(); } + + +void Beamer::printPosition(){ + cv::Point3f pos = getPosition(); + std::cout << "(" << pos.x << "," << pos.y << "," << pos.z << ")" << std::endl; +} diff --git a/src/core/components/beamerProjection.cpp b/src/core/components/beamerProjection.cpp index fc66a89e62e3b5283272df68d078c2b88cb5be62..f6047fa2c2baead6ae80e08f43f76901e876fa26 100644 --- a/src/core/components/beamerProjection.cpp +++ b/src/core/components/beamerProjection.cpp @@ -15,8 +15,8 @@ Point2i BeamerProjection::rotatePixel(Point2i pixel) return Point2i(tmp.at<Vec2f>(0, 0)); } -Point2i BeamerProjection::adjustPixel(int i, int j, float z, Camera camera, Point3f beamer) -{ +Point2i BeamerProjection::adjustPixel(int i, int j, float z, Camera camera, Point3f beamer){ + //pixel to point 3d float coord[2] = {static_cast<float>(j), static_cast<float>(i)}; Point3f p = camera.deprojectPixelToPoint(coord, z / 1000.0); @@ -50,8 +50,8 @@ void BeamerProjection::adjustFrame(cv::Mat &src, cv::Mat &dst, Camera camera, Po // medianBlur(dst, dst, 3); } -void BeamerProjection::adjustFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, Camera camera, Point3f beamer) -{ +void BeamerProjection::adjustFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, Camera camera, Point3f beamer){ + int nbChannel = src.channels(); //transformation on all pixel //int64_t t1 = getTickCount(); @@ -75,3 +75,14 @@ void BeamerProjection::adjustFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, C dilate(dst, dst, Mat(), Point(-1, -1), 2, 1, 1); erode(dst, dst, Mat(), Point(-1, -1), 2, 1, 1); } + + +void BeamerProjection::printAdjustingMatrix(){ + cv::Mat matrix = getAdjustingMatrix(); + for (int y = 0; y < matrix.rows; y++){ + for (int x = 0; x < matrix.cols; x++){ + std::cout << (float)matrix.at<double>(y, x) << " "; + } + std::cout << std::endl; + } +} \ No newline at end of file diff --git a/src/core/components/camera.cpp b/src/core/components/camera.cpp index 0e98056e84811acf5597bc287803578f41099c47..0c9f43083e48f128379d34e8a66cd0daf94ee920 100644 --- a/src/core/components/camera.cpp +++ b/src/core/components/camera.cpp @@ -1,6 +1,9 @@ #include "../../../includes/camera.h" using namespace cv; -Camera::Camera() {} + +Camera::Camera() { + +} // Capture 30 frames to give autoexposure, etc. a chance to settle void Camera::warmingUp() @@ -147,4 +150,10 @@ void Camera::startAlign() void Camera::stop() { pipe.stop(); +} + + +void Camera::printCroppingMask(){ + cv::Rect mask = getCroppingMask(); + std::cout << "(" << mask.x << "," << mask.y << ") + " << mask.width << "," << mask.height << std::endl; } \ No newline at end of file diff --git a/src/core/controller.cpp b/src/core/controller.cpp index 88579b284df831a2c7f314c4b8ec236b52f92af3..39f745a9055fc78b68d5f5f0bcf1f940cb480fce 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -11,7 +11,7 @@ using namespace std; */ Controller::Controller(){ - + camera.startAlign(); } @@ -29,42 +29,72 @@ cv::Mat Controller::getDepthFrame(){ return camera.getDepthFrameAlign()(camera.getCroppingMask()); } + +cv::Mat* Controller::adjustProjection(cv::Mat* frame){ + + static cv::Mat frameBeamer(cv::Size(beamer.getWidth(), beamer.getHeight()), CV_8UC3); + + cv::Mat depth = getDepthFrame(); + cv::Mat imageCalibrate(depth.size(), CV_8UC3, cv::Scalar(0, 0, 0)); + + cv::resize(*frame, *frame, depth.size()); + projection.adjustFrame(depth, *frame, imageCalibrate, camera, beamer.getPosition()); + //flip to align frame with beamer + cv::flip(imageCalibrate, imageCalibrate, 1); + cv::flip(imageCalibrate, imageCalibrate, 0); + + cv::resize(imageCalibrate, frameBeamer, frameBeamer.size()); + frameBeamer.copyTo(*frame); + return frame; +} + void Controller::showImage(cv::Mat* image){ - showImage(image, defaultWindowsName); + + initWindowsFullScreen(defaultWindowsName); + adjustProjection(image); + cv::imshow(defaultWindowsName, *image); } -void Controller::loadConfigFrom(char *path){ - SandboxConfig::loadAdjustingMatrixInto(path, &projection); - SandboxConfig::loadCroppingMaskInto(path, &camera); - SandboxConfig::loadBeamerPositionInto(path, &beamer); +int Controller::loadConfigFrom(char *path){ + + int err = SandboxConfig::loadAdjustingMatrixInto(path, &projection); + if(err){ return err; } + err = SandboxConfig::loadCroppingMaskInto(path, &camera); + if(err){ return err; } + err = SandboxConfig::loadBeamerPositionInto(path, &beamer); + if(err){ return err; } + return 0; } -void Controller::loadConfig(){ - loadConfigFrom(defaultConfigFilePath); +int Controller::loadConfig(){ + + std::cout << "Config loading..." << std::endl; + + int err = loadConfigFrom(defaultConfigFilePath); + if(err){ + std::cout << "error config" << std::endl; + return err; + } + + std::cout << "Adjusting Matrix :" << std::endl; + projection.printAdjustingMatrix(); + std::cout << "Cropping mask (base+size) :" << std::endl; + camera.printCroppingMask(); + std::cout << "Beamer position :" << std::endl; + beamer.printPosition(); + + return 0; } void Controller::initWindowsFullScreen(){ initWindowsFullScreen(defaultWindowsName); } + /* * PRIVATE */ -void Controller::showImage(cv::Mat* image, char *windowName){ - static cv::Mat frameBeamer(cv::Size(beamer.getWidth(), beamer.getHeight()), CV_8UC3); - camera.captureFramesAlign(); - cv::Mat depth = camera.getDepthFrameAlign()(camera.getCroppingMask()); - resize(*image, *image, depth.size()); - cv::Mat imageCalibrate(depth.size(), CV_8UC3, cv::Scalar(0, 0, 0)); - projection.adjustFrame(depth, *image, imageCalibrate, camera, beamer.getPosition()); - //flip to align frame with beamer - cv::flip(imageCalibrate, imageCalibrate, 1); - cv::flip(imageCalibrate, imageCalibrate, 0); - cv::resize(imageCalibrate, frameBeamer, frameBeamer.size()); - cv::imshow(windowName, frameBeamer); -} - void Controller::initWindowsFullScreen(char *windowName){ cv::namedWindow(windowName, CV_WINDOW_NORMAL); cv::setWindowProperty(windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); diff --git a/src/core/tools/sandboxConfig.cpp b/src/core/tools/sandboxConfig.cpp index 81a9efa437096c8a52a368e5314dcce6e91fc713..65c30a50c533cc91e7772aa45c2824260f53c2af 100644 --- a/src/core/tools/sandboxConfig.cpp +++ b/src/core/tools/sandboxConfig.cpp @@ -9,10 +9,9 @@ int SandboxConfig::saveAdjustingMatrixInto(char *path, cv::Mat matrix){ YAML::Node vec = YAML::Load("[]"); for (int y = 0; y < matrix.rows; y++){ for (int x = 0; x < matrix.cols; x++){ - vec.push_back(matrix.at<float>(y, x)); + vec.push_back((float)matrix.at<double>(y, x)); } } - YAML::Node val; @@ -29,14 +28,6 @@ int SandboxConfig::saveAdjustingMatrixInto(char *path, cv::Mat matrix){ config[MATRIX] = val; - std::cout << "Adjusting Matrix" << std::endl; - for (int y = 0; y < matrix.rows; y++){ - for (int x = 0; x < matrix.cols; x++){ - std::cout << matrix.at<float>(y, x) << " "; - } - std::cout << std::endl; - } - return saveConfigIn(path, config); } @@ -59,10 +50,6 @@ int SandboxConfig::saveCroppingMaskInto(char *path, cv::Rect mask){ config[MASK] = val; - std::cout << "Cropping Mask (base + size)" << std::endl; - std::cout << "(" << mask.x << "," << mask.y << ") + "; - std::cout << "(" << mask.width << "," << mask.height << ")" << std::endl; - return saveConfigIn(path, config); } @@ -84,8 +71,6 @@ int SandboxConfig::saveBeamerPositionInto(char *path, cv::Point3f pos){ config[POSITION] = val; - std::cout << "Beamer position: " << pos.x << " " << pos.y << " " << pos.z << std::endl; - return saveConfigIn(path, config); } @@ -131,16 +116,6 @@ int SandboxConfig::loadAdjustingMatrixInto(char *path, BeamerProjection *project projection->setAdjustingMatrix(matrix); - // Debug - matrix = projection->getAdjustingMatrix(); - std::cout << "Adjusting Matrix" << std::endl; - for (int y = 0; y < matrix.rows; y++){ - for (int x = 0; x < matrix.cols; x++){ - std::cout << matrix.at<float>(y, x) << " "; - } - std::cout << std::endl; - } - return 0; } @@ -173,10 +148,6 @@ int SandboxConfig::loadCroppingMaskInto(char *path, Camera *camera){ camera->setCroppingMask(mask); - // Debug - mask = camera->getCroppingMask(); - std::cout << "Cropping mask (base+size): (" << mask.x << "," << mask.y << ") + " << mask.width << "," << mask.height << std::endl; - return 0; } @@ -208,10 +179,6 @@ int SandboxConfig::loadBeamerPositionInto(char *path, Beamer *beamer){ beamer->setPosition(pos); - // Debug - pos = beamer->getPosition(); - std::cout << "Beamer position :" << std::endl << "(" << pos.x << "," << pos.y << "," << pos.z << ")" << std::endl; - return 0; } diff --git a/src/sandbox.cpp b/src/sandbox.cpp index a737a50fb6bd158df8e22f6dfd6618ef6656f7a2..292c1d3204665a2a8239b3c5382f308ad854a8ba 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -1,7 +1,7 @@ #include "../includes/sandbox.h" Sandbox::Sandbox(){ - controller.initWindowsFullScreen(); + } cv::Mat Sandbox::getRGBFrame(){ @@ -12,10 +12,18 @@ cv::Mat Sandbox::getDepthFrame(){ return controller.getDepthFrame(); } +cv::Mat* Sandbox::adjustProjection(cv::Mat* frame){ + return controller.adjustProjection(frame); +} + +void Sandbox::initWindowsFullScreen(){ + controller.initWindowsFullScreen(); +} + void Sandbox::showImage(cv::Mat* image){ controller.showImage(image); } -void Sandbox::loadConfig(){ - controller.loadConfig(); +int Sandbox::loadConfig(){ + return controller.loadConfig(); } \ No newline at end of file diff --git a/src/sandboxSetup.cpp b/src/sandboxSetup.cpp index 061d62cf5e1697301667ec5434c0e9ddfa277db4..36e95dd10d0286327e4d76529f99ded19c1529a9 100644 --- a/src/sandboxSetup.cpp +++ b/src/sandboxSetup.cpp @@ -37,6 +37,7 @@ void SandboxSetup::setupProjection(){ cv::Mat coloredFrame = camera.getRGBFrameAlign(); cv::Size s = frameData.size(); cv::Point center(s.width / 2, s.height / 2); + camera.stop(); cv::destroyAllWindows(); // Edit projection