diff --git a/includes/controller.h b/includes/controller.h index 501b656f4b663cd83012207a0e5eecb36d946362..1ca73ed79ddda92a0b23c8de5922bef00863b46d 100644 --- a/includes/controller.h +++ b/includes/controller.h @@ -15,17 +15,13 @@ class Controller cv::Mat getRGBFrame(); cv::Mat getDepthFrame(); void showImage(cv::Mat* image); - //void exportToAsc(cv::Mat frame, char *fileName); - //void exportFrame(cv::Mat src, char *fileName); - //bool importFrame(char *fileName, cv::Mat &output); + void setupConfig(); private: const char *SANDBOX_POSITION_FILE = "./sandbox.dat"; const char *CALIBRATE_DISTANCE_FILE = "./distance.dat"; const char *CALIBRATE_MATRIX_FILE = "./matrixe.dat"; static const char CHAR_DELIM = ' '; - //const float SANDBOX_CONTOUR = 2.0f * (1020.0f + 770.0f); - //const float MARGE = 0.05 * SANDBOX_CONTOUR; // 5% of marge static const int ESCAPE_CHAR = 27; char *defaultWindowsName = (char*) "Image"; cv::Rect rectSandbox; @@ -34,14 +30,10 @@ class Controller Beamer beamer; void createWindowsFullScreen(char *windowName); void showImage(cv::Mat* image, char *windowName); - //bool readSandboxPosition(std::vector<cv::Point> &rectPoints); - //void writeSandboxPosition(std::vector<cv::Point> rectPoints); double toDegrees(double radians); - void sanboxBorder(); - //void sandboxBorderLoad(); - //bool configFilesFound(); + void setupProjection(Camera *camera); + void setupBeamerLocation(Beamer *beamer, Camera *camera); - }; #endif diff --git a/src/core/components/calibrate.cpp b/src/core/components/calibrate.cpp index e68003ed8f43059e9cdec8bc9e6abf78b7fba570..faeb8c8c6cbfa53c8d5fc1720752648bb4a42234 100644 --- a/src/core/components/calibrate.cpp +++ b/src/core/components/calibrate.cpp @@ -6,6 +6,7 @@ using namespace std; Calibrate::Calibrate() { + matRotation = getRotationMatrix2D(Point(0, 0), 0, 1); } Point2i Calibrate::rotatePixel(Point2i pixel) @@ -32,7 +33,7 @@ Point2i Calibrate::transformationPixel(int i, int j, float z, Camera camera, Poi void Calibrate::transformationFrame(cv::Mat &src, cv::Mat &dst, Camera camera, Point3f beamer) { - int64_t t1 = getTickCount(); + //int64_t t1 = getTickCount(); //transformation on all pixel for (int i = 0; i < src.rows; i++) { @@ -44,7 +45,7 @@ void Calibrate::transformationFrame(cv::Mat &src, cv::Mat &dst, Camera camera, P dst.at<uint16_t>(pixel) = src.at<uint16_t>(pixelIJ); } } - cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl; + //cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl; warpAffine(dst, dst, matRotation, dst.size()); // medianBlur(dst, dst, 3); @@ -54,7 +55,7 @@ void Calibrate::transformationFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, { int nbChannel = src.channels(); //transformation on all pixel - int64_t t1 = getTickCount(); + //int64_t t1 = getTickCount(); for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) @@ -70,7 +71,7 @@ void Calibrate::transformationFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, } } } - cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl; + //cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl; warpAffine(dst, dst, matRotation, dst.size()); dilate(dst, dst, Mat(), Point(-1, -1), 2, 1, 1); erode(dst, dst, Mat(), Point(-1, -1), 2, 1, 1); diff --git a/src/core/controller.cpp b/src/core/controller.cpp index bc2f8183aa551fe9ccb3e7330cefd117e5636c1c..232e090f3b6a6283efd910f8044a73472ef9f0f2 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -6,22 +6,20 @@ using namespace std; using namespace cv; + +/* + * MAIN + */ + Controller::Controller(){ - bool configSanbox = true; - //cout << "All files there? " << configFilesFound() << endl; - if(configSanbox){ - // Blue screen and edit colored frame routine - sanboxBorder(); - // Matching cross under beamer routine - beamer.findBeamer(camera); - }else{ - //sandboxBorderLoad(); - //beamer.findBeamerLoad(camera); - } - // Set full screen parameter for show function - createWindowsFullScreen(defaultWindowsName); + } + +/* + * PUBLIC + */ + Mat Controller::getRGBFrame(){ camera.captureFramesAlign(); return camera.getRGBFrameAlign()(rectSandbox); @@ -36,6 +34,34 @@ void Controller::showImage(cv::Mat* image){ showImage(image, defaultWindowsName); } +void Controller::setupConfig(){ + // Blue screen and edit colored frame routine + setupProjection(&camera); + // Matching cross under beamer routine + setupBeamerLocation(&beamer, &camera); + createWindowsFullScreen(defaultWindowsName); + + + // Debug + Mat matRotation = calibrate.getMatrixRotation(); + cout << "Adjusting Matrix" << endl; + for (int y = 0; y < matRotation.rows; y++){ + for (int x = 0; x < matRotation.cols; x++){ + cout << matRotation.at<float>(y, x) << " "; + } + cout << endl; + } + + cout << "Cropping Mask (base + size)" << endl; + cout << "(" << rectSandbox.x << "," << rectSandbox.y << ") + "; + cout << "(" << rectSandbox.width << "," << rectSandbox.height << ")" << endl; +} + + +/* + * PRIVATE + */ + void Controller::showImage(cv::Mat* image, char *windowName){ static Mat frameBeamer(Size(Beamer::width, Beamer::height), CV_8UC3); camera.captureFramesAlign(); @@ -54,209 +80,54 @@ double Controller::toDegrees(double radians){ return radians * (180.0 / M_PI); } -/* -bool Sandbox::readSandboxPosition(vector<Point> &rectPoints) -{ - std::ifstream infile(SANDBOX_POSITION_FILE); - if (infile.good()) - { - int x, y; - while (infile >> x >> y) - { - rectPoints.push_back(Point(x, y)); - cout << x << " " << y << endl; - } - } - infile.close(); - return rectPoints.size() == 4; +void Controller::createWindowsFullScreen(char *windowName){ + namedWindow(windowName, CV_WINDOW_NORMAL); + setWindowProperty(windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); } -void Sandbox::writeSandboxPosition(vector<Point> rectPoints) -{ - //export points to file - std::ofstream myfile; - myfile.open(SANDBOX_POSITION_FILE); - for (unsigned int i = 0; i < rectPoints.size(); i++) - myfile << rectPoints[i].x << " " << rectPoints[i].y << endl; - myfile.close(); -}*/ - -/* -void Sandbox::sandboxBorderLoad(){ - float distancePlan; - Mat matRotation; - - // Distance Plan - std::ifstream infile(CALIBRATE_DISTANCE_FILE); - if (infile.good()){ - while (infile >> distancePlan){ - cout << distancePlan << endl; - } - } - infile.close(); - - // Matrix Rotation - char *filename = ""; - strcpy(filename, CALIBRATE_MATRIX_FILE); - importFrame(filename, matRotation); - calibrate.setDistancePlan(distancePlan); -} -void Sandbox::exportToAsc(Mat src, char *fileName) -{ - std::ofstream myfile; - myfile.open(fileName); - Mat frameSand = src(rectSandbox); - for (int y = 0; y < src.rows; y++) - { - for (int x = 0; x < src.cols; x++) - { - myfile << x << " " << y << " " << frameSand.at<uint16_t>(y, x) << "\n"; - } - } - myfile.close(); -} -void Sandbox::exportFrame(Mat src, char *fileName) -{ - std::ofstream myfile; - myfile.open(fileName); - Mat frameSand = src(rectSandbox); - myfile << frameSand.cols << " " << frameSand.rows << endl; - for (int y = 0; y < frameSand.rows; y++) - { - for (int x = 0; x < frameSand.cols; x++) - { - myfile << frameSand.at<uint16_t>(y, x) << CHAR_DELIM; - } - myfile << endl; - } - myfile.close(); +void Controller::setupBeamerLocation(Beamer *beamer, Camera *camera){ + beamer->findBeamer(*camera); } -bool Sandbox::importFrame(char *fileName, Mat &output) -{ - - std::ifstream file(fileName); - if (file.is_open()) - { - string elem; - int i = 0; - //read header - getline(file, elem, CHAR_DELIM); - int width = atoi(elem.c_str()); - getline(file, elem, CHAR_DELIM); - int height = atoi(elem.c_str()); - //create & read matrix - output = Mat(Size(width, height), CV_16UC1); - cout << width << " " << height << endl; - while (getline(file, elem, CHAR_DELIM)) - { - output.at<uint16_t>(i / width, i % width) = (uint16_t)atoi(elem.c_str()); - i++; - } - file.close(); - return true; - } - return false; -} - -bool Sandbox::configFilesFound(){ - const char* files[] = { SANDBOX_POSITION_FILE, - CALIBRATE_DISTANCE_FILE, - CALIBRATE_MATRIX_FILE }; - int size = (sizeof(files)/sizeof(char*)); - - for (int i = 0; i < size; i++) - { - char *filename = ""; - strcpy(filename, files[i]); - std::ifstream infile(filename); - if(infile.fail()){ - cout << "Configuration file missing..."; - return false; - } - } - - return true; -}*/ +void Controller::setupProjection(Camera *camera){ - -void Controller::sanboxBorder() -{ + // Blue screen char windowName[] = "border"; createWindowsFullScreen(windowName); Mat frameBeamer(Size(Beamer::width, Beamer::height), CV_8UC3, Scalar(255, 0, 0)); - - // Show blue frame imshow(windowName, frameBeamer); waitKey(100); - camera.startAlign(); // 1 seconde of warming up - camera.captureFramesAlign(); - - Mat frameData = camera.getDepthFrameAlign(); - Mat coloredFrame = camera.getRGBFrameAlign(); + // Take picture + camera->startAlign(); // 1 seconde of warming up + camera->captureFramesAlign(); + Mat frameData = camera->getDepthFrameAlign(); + Mat coloredFrame = camera->getRGBFrameAlign(); Size s = frameData.size(); Point center(s.width / 2, s.height / 2); - - // calibrate distance - //Point center(s.width / 2, s.height / 2); - //float distancePlan = static_cast<float>(mean(frameData(Rect(center, Size(10, 10))))[0]); // get sandbox distance from center of camera - //calibrate.setDistancePlan(distancePlan); destroyAllWindows(); - vector<Point> rectPoints; + // Edit projection float y = coloredFrame.size().height; float x = coloredFrame.size().width; - rectPoints.push_back(Point(1.0/4*x, 1.0/4*y)); - rectPoints.push_back(Point(1.0/4*x, 3.0/4*y)); - rectPoints.push_back(Point(3.0/4*x, 3.0/4*y)); - rectPoints.push_back(Point(3.0/4*x, 1.0/4*y)); - + vector<Point> rectPoints{ Point(1.0/4*x, 1.0/4*y), Point(1.0/4*x, 3.0/4*y), Point(3.0/4*x, 3.0/4*y), Point(3.0/4*x, 1.0/4*y) }; cout << "Edit Rectangle" << endl; BorderEdit::edit(coloredFrame, &rectPoints); // edit projected frame - // adjust model matrixe to forme a rectangle in sandbox + // Set adjusting matrix for the projection int widthTop = rectPoints[3].x - rectPoints[0].x; double angle1 = atan((double)(rectPoints[3].y - rectPoints[0].y) / widthTop); - Mat matRotation = getRotationMatrix2D(center, toDegrees(angle1), 1); - - calibrate.setMatrixRotation(matRotation); cout << "Calibrate Rotation Matrixe" << endl; + Mat matRotation = getRotationMatrix2D(center, toDegrees(angle1), 1); // adjustingMatrix + calibrate.setMatrixRotation(matRotation); + // Set cropping mask Size rectSize = Size(widthTop, cvRound(widthTop / 1.33333) + 5); Point p = calibrate.rotatePixel(rectPoints[0]); - // rectSandbox => CroppingMask - rectSandbox = Rect(p, rectSize); - //writeSandboxPosition(rectPoints); // update with adjusted sandbox's angles + rectSandbox = Rect(p, rectSize); // coppingMask destroyAllWindows(); - - /* - - TODO : save matRotation in config_file - - */ - /*cout << "Saving matrixe in file..." << endl; - myfile.open(CALIBRATE_MATRIX_FILE); - cout << "Data : " << matRotation.rows << " : " << matRotation.cols << endl; - myfile << matRotation.cols << " " << matRotation.rows << endl; - for (int y = 0; y < matRotation.rows; y++) - { - for (int x = 0; x < matRotation.cols; x++) - { - myfile << matRotation.at<uint16_t>(y, x) << CHAR_DELIM; - } - myfile << endl; - } - myfile.close(); - cout << "Done..." << endl;*/ -} - -void Controller::createWindowsFullScreen(char *windowName) -{ - namedWindow(windowName, CV_WINDOW_NORMAL); - setWindowProperty(windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN); -} +} \ No newline at end of file diff --git a/src/sandbox.cpp b/src/sandbox.cpp index 25f0a6e80f5522cbb8eaebf7d4c847f0eed392b3..ce810ce3c99b02100dc1b3b8ed617ce46a0f1875 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -1,7 +1,7 @@ #include "../includes/sandbox.h" Sandbox::Sandbox(){ - controller = Controller(); + controller.setupConfig(); } cv::Mat Sandbox::getRGBFrame(){