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

adjust classes to new architecture + add setup app

parent 13e28b91
No related branches found
No related tags found
No related merge requests found
Showing
with 190 additions and 186 deletions
include src/common.mk
include dep.mk
OBJSALL=$(shell find src -name '*.o')
SETUPAPP=SandboxSetupApp
LIBNAME=libsandbox
LIB_MINOR_VERS=0.0
......@@ -12,6 +14,7 @@ all:
$(MAKE) -C src
$(MAKE) pack -C .
$(MAKE) link -C .
$(MAKE) app -C .
pack:
gcc -shared -Wl,-soname,$(LIB_FULL_NAME) -o $(LIB_FULL_NAME) $(OBJSALL)
......@@ -20,6 +23,14 @@ link:
-ln -s $(LIB_FULL_NAME) $(LIBNAME).so.$(LIB_MAJOR_VERS)
-ln -s $(LIB_FULL_NAME) $(LIBNAME).so
app: $(SETUPAPP).o $(LIBNAME).so
g++ $< -o $(SETUPAPP) -L. -lsandbox $(DEP_SANDBOX)
$(SETUPAPP).o: $(SETUPAPP).cpp
$(CCP) $(CFLAGS) -I./includes -c $< -o $@
clean:
-rm -f *.o *.so*
-rm $(SETUPAPP)
$(MAKE) clean -C src
#include "includes/sandboxSetup.h"
int main(int argc, char *argv[]){
BeamerProjection projection;
std::cout << "Projection init" << std::endl;
Camera camera;
std::cout << "Camera init" << std::endl;
Beamer beamer;
std::cout << "Beamer init" << std::endl;
SandboxSetup setup;
std::cout << "Projection init" << std::endl;
SandboxConfig conf;
setup.setupProjection(&beamer, &camera, &projection);
setup.setupBeamerLocation(&beamer, &camera);
// save into file
conf.saveAdjustingMatrix(projection.getAdjustingMatrix());
conf.saveCroppingMask(camera.getCroppingMask());
conf.saveBeamerPosition(beamer.getPosition());
// Debug
cv::Mat matRotation = projection.getAdjustingMatrix();
std::cout << "Adjusting Matrix" << std::endl;
for (int y = 0; y < matRotation.rows; y++){
for (int x = 0; x < matRotation.cols; x++){
std::cout << matRotation.at<float>(y, x) << " ";
}
std::cout << std::endl;
}
cv::Rect rectSandbox = camera.getCroppingMask();
std::cout << "Cropping Mask (base + size)" << std::endl;
std::cout << "(" << rectSandbox.x << "," << rectSandbox.y << ") + ";
std::cout << "(" << rectSandbox.width << "," << rectSandbox.height << ")" << std::endl;
}
......@@ -5,20 +5,20 @@
class Beamer
{
private:
const char *BEAMER_POSITION_FILE = "./beamer.dat";
float solveD(cv::Vec3f v, cv::Point3f p);
cv::Point3f intersection(cv::Vec3f v1, cv::Point3f p1, cv::Vec3f v2, cv::Point3f p2, cv::Vec3f v3, cv::Point3f p3, bool &isFound);
std::vector<int> findCercleZ(cv::Mat &rgb);
cv::Point3f beamerPosition;
int width = 1400;
int height = 1050;
public:
Beamer();
static const int width = 1400;
static const int height = 1050;
cv::Point3f getPosition()
{
return beamerPosition;
};
void findBeamer(Camera camera);
cv::Point3f getPosition(){ return beamerPosition; };
int getWidth(){ return width; };
int getHeight(){ return height; };
void findBeamerFrom(Camera camera);
};
#endif
#ifndef BEAMERPROJECTION_H
#define BEAMERPROJECTION_H
#include <opencv2/opencv.hpp>
#include "beamer.h"
#include "camera.h"
class BeamerProjection
{
private:
const char *wndname = (char *)"Sandbox";
cv::Mat adjustingMatrix;
cv::Point2i adjustPixel(int i, int j, float z, Camera camera, cv::Point3f beamer);
public:
BeamerProjection();
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(); }
cv::Mat getAdjustingMatrix(){ return adjustingMatrix.clone(); }
};
#endif
\ No newline at end of file
#ifndef CALIBRATE_H
#define CALIBRATE_H
#include <opencv2/opencv.hpp>
#include "beamer.h"
#include "camera.h"
class Calibrate
{
private:
const char *wndname = (char *)"Sandbox";
cv::Mat matRotation;
float distancePlan;
public:
Calibrate();
cv::Point2i transformationPixel(int i, int j, float z, Camera camera, cv::Point3f beamer);
cv::Point2i rotatePixel(cv::Point2i pixel);
void transformationFrame(cv::Mat &src, cv::Mat &dst, Camera camera, cv::Point3f beamer);
void transformationFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst, Camera camera, cv::Point3f beamer);
void setMatrixRotation(cv::Mat matrix)
{
matRotation = matrix.clone();
}
cv::Mat getMatrixRotation(){
return matRotation.clone();
}
void setDistancePlan(float distance)
{
distancePlan = distance;
}
};
#endif
\ No newline at end of file
......@@ -8,12 +8,6 @@
class Camera
{
private:
//constants in mm
//const float scale = rs2_get_depth_scale(sensor, NULL);
//static const int maxHeightSand = 400;
//static const int maxZ = 1120;
//static const int minZ = maxZ - maxHeightSand;
rs2::spatial_filter spatFilter;
rs2::temporal_filter tempFilter;
rs2::decimation_filter decFilter;
......@@ -26,6 +20,7 @@ private:
cv::Mat matDepth;
cv::Mat matRGB;
cv::Rect croppingMask;
public:
Camera();
......@@ -41,13 +36,9 @@ public:
void captureFramesAlign();
void startAlign();
cv::Mat getDepthFrameAlign()
{
return matDepth;
}
cv::Mat getRGBFrameAlign()
{
return matRGB.clone();
}
cv::Mat getDepthFrameAlign(){ return matDepth; };
cv::Mat getRGBFrameAlign(){ return matRGB.clone(); };
void setCroppingMask(cv::Rect mask){ croppingMask = mask; };
cv::Rect getCroppingMask(){ return croppingMask; };
};
#endif
\ No newline at end of file
......@@ -3,7 +3,7 @@
#include <opencv2/opencv.hpp>
#include "camera.h"
#include "calibrate.h"
#include "beamerProjection.h"
#include "beamer.h"
#include "borderedit.h"
#include "borderfinder.h"
......@@ -15,25 +15,16 @@ class Controller
cv::Mat getRGBFrame();
cv::Mat getDepthFrame();
void showImage(cv::Mat* image);
void setupConfig();
void initWindowsFullScreen();
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 = ' ';
static const int ESCAPE_CHAR = 27;
char *defaultWindowsName = (char*) "Image";
cv::Rect rectSandbox;
Calibrate calibrate;
BeamerProjection projection;
Camera camera;
Beamer beamer;
void createWindowsFullScreen(char *windowName);
void initWindowsFullScreen(char *windowName);
void showImage(cv::Mat* image, char *windowName);
double toDegrees(double radians);
void setupProjection(Camera *camera);
void setupBeamerLocation(Beamer *beamer, Camera *camera);
};
#endif
#ifndef SANDBOXCONFIG_H
#define SANDBOXCONFIG_H
#include <opencv2/opencv.hpp>
#include "camera.h"
#include "beamerProjection.h"
#include "beamer.h"
class SandboxConfig
{
private:
char *configFilePathname = (char *)"./sandbox.cfg";
public:
SandboxConfig();
void saveAdjustingMatrix(cv::Mat matrix);
void saveCroppingMask(cv::Rect mask);
void saveBeamerPosition(cv::Point3f position);
void load(Beamer *beamer, Camera *camera, BeamerProjection *projection);
};
#endif
#ifndef SANDBOXSETUP_H
#define SANDBOXSETUP_H
#include <opencv2/opencv.hpp>
#include <cstdlib>
#include "beamer.h"
#include "beamerProjection.h"
#include "camera.h"
#include "borderedit.h"
#include "sandboxConfig.h"
class SandboxSetup{
private:
double toDegrees(double radians);
void initWindowsFullScreen(char *windowName);
public:
SandboxSetup();
void setupProjection(Beamer *beamer, Camera *camera, BeamerProjection *projection);
void setupBeamerLocation(Beamer *beamer, Camera *camera);
};
#endif
#ifndef SANDBOX_SETUP_H
#define SANDBOX_SETUP_H
#endif
0.643826 0.269363 0.329644
984.25
3 2
0 0 0
0 0 0
160 120
160 360
480 360
480 120
......@@ -96,7 +96,7 @@ vector<int> Beamer::findCercleZ(Mat &rgb)
return result;
}
void Beamer::findBeamer(Camera camera)
void Beamer::findBeamerFrom(Camera camera)
{
char wname[] = "FindBeamer";
namedWindow(wname, CV_WINDOW_NORMAL);
......
#include "../../../includes/calibrate.h"
#include "../../../includes/camera.h"
#include "../../../includes/beamerProjection.h"
using namespace cv;
using namespace std;
Calibrate::Calibrate()
BeamerProjection::BeamerProjection()
{
matRotation = getRotationMatrix2D(Point(0, 0), 0, 1);
adjustingMatrix = getRotationMatrix2D(Point(0, 0), 0, 1);
}
Point2i Calibrate::rotatePixel(Point2i pixel)
Point2i BeamerProjection::rotatePixel(Point2i pixel)
{
Mat tmp = (Mat_<Vec2f>(1, 1) << Vec2f(pixel.x, pixel.y));
transform(tmp, tmp, matRotation);
transform(tmp, tmp, adjustingMatrix);
return Point2i(tmp.at<Vec2f>(0, 0));
}
Point2i Calibrate::transformationPixel(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)};
......@@ -31,7 +30,7 @@ Point2i Calibrate::transformationPixel(int i, int j, float z, Camera camera, Poi
return camera.projectPointToPixel(p);
}
void Calibrate::transformationFrame(cv::Mat &src, cv::Mat &dst, Camera camera, Point3f beamer)
void BeamerProjection::adjustFrame(cv::Mat &src, cv::Mat &dst, Camera camera, Point3f beamer)
{
//int64_t t1 = getTickCount();
//transformation on all pixel
......@@ -40,18 +39,18 @@ void Calibrate::transformationFrame(cv::Mat &src, cv::Mat &dst, Camera camera, P
for (int j = 0; j < src.cols; j++)
{
Point pixelIJ(j, i);
Point pixel = transformationPixel(i, j, static_cast<float>(src.at<uint16_t>(pixelIJ)), camera, beamer);
Point pixel = adjustPixel(i, j, static_cast<float>(src.at<uint16_t>(pixelIJ)), camera, beamer);
if (pixel.x < dst.cols && pixel.y < dst.rows && pixel.x >= 0 && pixel.y >= 0)
dst.at<uint16_t>(pixel) = src.at<uint16_t>(pixelIJ);
}
}
//cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl;
warpAffine(dst, dst, matRotation, dst.size());
warpAffine(dst, dst, adjustingMatrix, dst.size());
// medianBlur(dst, dst, 3);
}
void Calibrate::transformationFrame(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
......@@ -61,7 +60,7 @@ void Calibrate::transformationFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst,
for (int j = 0; j < src.cols; j++)
{
Point pixelIJ(j, i);
Point pixel = transformationPixel(i, j, static_cast<float>(depth.at<uint16_t>(pixelIJ)), camera, beamer);
Point pixel = adjustPixel(i, j, static_cast<float>(depth.at<uint16_t>(pixelIJ)), camera, beamer);
if (pixel.x < dst.cols && pixel.y < dst.rows && pixel.x >= 0 && pixel.y >= 0)
{
if (nbChannel == 1)
......@@ -72,7 +71,7 @@ void Calibrate::transformationFrame(cv::Mat &depth, cv::Mat &src, cv::Mat &dst,
}
}
//cout << "temps de calcul: " << (getTickCount() - t1) / getTickFrequency() << endl;
warpAffine(dst, dst, matRotation, dst.size());
warpAffine(dst, dst, adjustingMatrix, dst.size());
dilate(dst, dst, Mat(), Point(-1, -1), 2, 1, 1);
erode(dst, dst, Mat(), Point(-1, -1), 2, 1, 1);
}
......@@ -4,7 +4,6 @@
#include <string>
using namespace std;
using namespace cv;
/*
......@@ -20,114 +19,44 @@ Controller::Controller(){
* PUBLIC
*/
Mat Controller::getRGBFrame(){
cv::Mat Controller::getRGBFrame(){
camera.captureFramesAlign();
return camera.getRGBFrameAlign()(rectSandbox);
return camera.getRGBFrameAlign()(camera.getCroppingMask());
}
Mat Controller::getDepthFrame(){
cv::Mat Controller::getDepthFrame(){
camera.captureFramesAlign();
return camera.getDepthFrameAlign()(rectSandbox);
return camera.getDepthFrameAlign()(camera.getCroppingMask());
}
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);
static cv::Mat frameBeamer(cv::Size(beamer.getWidth(), beamer.getHeight()), CV_8UC3);
camera.captureFramesAlign();
Mat depth = camera.getDepthFrameAlign()(rectSandbox);
cv::Mat depth = camera.getDepthFrameAlign()(camera.getCroppingMask());
resize(*image, *image, depth.size());
Mat imageCalibrate(depth.size(), CV_8UC3, Scalar(0, 0, 0));
calibrate.transformationFrame(depth, *image, imageCalibrate, camera, beamer.getPosition());
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
flip(imageCalibrate, imageCalibrate, 1);
flip(imageCalibrate, imageCalibrate, 0);
resize(imageCalibrate, frameBeamer, frameBeamer.size());
imshow(windowName, frameBeamer);
}
double Controller::toDegrees(double radians){
return radians * (180.0 / M_PI);
}
void Controller::createWindowsFullScreen(char *windowName){
namedWindow(windowName, CV_WINDOW_NORMAL);
setWindowProperty(windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
cv::flip(imageCalibrate, imageCalibrate, 1);
cv::flip(imageCalibrate, imageCalibrate, 0);
cv::resize(imageCalibrate, frameBeamer, frameBeamer.size());
cv::imshow(windowName, frameBeamer);
}
void Controller::setupBeamerLocation(Beamer *beamer, Camera *camera){
beamer->findBeamer(*camera);
void Controller::initWindowsFullScreen(char *windowName){
cv::namedWindow(windowName, CV_WINDOW_NORMAL);
cv::setWindowProperty(windowName, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
}
void Controller::setupProjection(Camera *camera){
// Blue screen
char windowName[] = "border";
createWindowsFullScreen(windowName);
Mat frameBeamer(Size(Beamer::width, Beamer::height), CV_8UC3, Scalar(255, 0, 0));
imshow(windowName, frameBeamer);
waitKey(100);
// 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);
destroyAllWindows();
// Edit projection
float y = coloredFrame.size().height;
float x = coloredFrame.size().width;
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
// 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);
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 = Rect(p, rectSize); // coppingMask
destroyAllWindows();
void Controller::initWindowsFullScreen(){
initWindowsFullScreen(defaultWindowsName);
}
\ No newline at end of file
......@@ -44,9 +44,9 @@ void BorderEdit::mouseHandler(int event, int x, int y, int, void *param)
posSandbox[selectedPoint].x = x;
posSandbox[selectedPoint].y = y;
BorderEdit::drawSquare(&posSandbox[0], (int)posSandbox.size());
Rect r = boundingRect(posSandbox);
double f = (double)r.width / (double)r.height;
cout << 4.0 / 3.0 << " == " << f << endl;
//Rect r = boundingRect(posSandbox);
//double f = (double)r.width / (double)r.height;
//cout << 4.0 / 3.0 << " == " << f << endl;
}
break;
}
......
#include "../../../includes/sandboxConfig.h"
SandboxConfig::SandboxConfig(){
}
void SandboxConfig::saveAdjustingMatrix(cv::Mat matrix){
}
void SandboxConfig::saveCroppingMask(cv::Rect mask){
}
void SandboxConfig::saveBeamerPosition(cv::Point3f position){
}
void SandboxConfig::load(Beamer *beamer, Camera *camera, BeamerProjection *projection){
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment