diff --git a/app/SandboxSetup/camerafocus.cpp b/app/SandboxSetup/camerafocus.cpp index de5b65168d5f0a2cc966bc09ae1b792522c20a08..1542da0600ba2af5554a6928417a773c512f3be5 100644 --- a/app/SandboxSetup/camerafocus.cpp +++ b/app/SandboxSetup/camerafocus.cpp @@ -7,12 +7,17 @@ CameraFocus::CameraFocus(SandboxSetup *sandbox, QWidget *parent) : { ui->setupUi(this); setup = sandbox; + + if(setup->loadFrameProcessProfil()){ + std::cout << "No process profil found" << std::endl; + } + initCameraParams(); frameTimer = new QTimer(this); connect(frameTimer, &QTimer::timeout, this, &CameraFocus::refreshFrame); setup->camera.start(); - frameTimer->start(200); + frameTimer->start(100); } CameraFocus::~CameraFocus() @@ -30,30 +35,36 @@ void CameraFocus::on_btnbxValidate_clicked(QAbstractButton *button) void CameraFocus::on_btnbxValidate_accepted() { state = true; + if(setup->saveFrameProcessProfil()){ + std::cout << "Error : couldn't save profil" << std::endl; + std::exit(1); + } } void CameraFocus::refreshFrame(){ setup->camera.captureFrame(); - profil_t p = setup->beamer.getProfil(); + FrameProcessProfil p = *(setup->beamer.getProfil()); - // convert Mat to QPixmap cv::Mat gray; + cv::Mat rgbFromGray; cv::Mat rgb = setup->camera.getRGBFrame(); + std::vector<int> crc; + if(p.getUpperMinThreshold() >= p.getLowerMinThreshold()){ + crc = setup->beamer.findCercleZ(rgb, p.getContrast(), p.getBrightness(), p.getRadiusRatio(), p.getUpperMinThreshold(), p.getLowerMinThreshold()); + } + + // Preview image for the user cv::cvtColor(rgb, gray, CV_BGR2GRAY); - gray = setup->beamer.editContrast(gray, (double)p.contrast, (double)p.brightness); + gray = setup->beamer.editContrast(gray, (double)p.getContrast(), (double)p.getBrightness()); + cv::cvtColor(gray, rgbFromGray, CV_GRAY2BGR); - std::vector<int> crc = setup->beamer.findCercleZ(rgb, p.contrast, p.brightness, p.ratioRadius, p.upperMinThreshold, p.lowerMinThreshold); if(!crc.empty()){ - ui->lblStatus->setText(QString("Found it!")); - cv::circle(gray, cv::Point(crc[0], crc[1]), crc[2], cv::Scalar(0,255,0)); - }else{ - ui->lblStatus->setText(QString("Nothing...")); + cv::circle(rgbFromGray, cv::Point(crc[0], crc[1]), crc[2], cv::Scalar(0,255,0), 2); } - QImage img = QImage((uchar *)gray.data, (int)gray.cols, (int)gray.rows, static_cast<int>(gray.step.buf[0]), QImage::Format_Grayscale8); - // QImage::Format_RGB888); + QImage img = QImage((uchar *)rgbFromGray.data, (int)rgbFromGray.cols, (int)rgbFromGray.rows, static_cast<int>(rgbFromGray.step.buf[0]), QImage::Format_RGB888); QPixmap image = QPixmap::fromImage(img); ui->lblFrame->setPixmap(image); @@ -62,45 +73,59 @@ void CameraFocus::refreshFrame(){ void CameraFocus::initCameraParams(){ - profil_t profil = setup->beamer.getProfil(); - ui->dbsbxContrast->setValue(profil.contrast); - ui->sbxBrightness->setValue(profil.brightness); - ui->sbxRadiusRatio->setValue(profil.ratioRadius); - ui->sbxUpperMin->setValue(profil.upperMinThreshold); - ui->sbxLowerMin->setValue(profil.lowerMinThreshold); + FrameProcessProfil *profil = setup->beamer.getProfil(); + + defaultProfil.setContrast(profil->getContrast()); + defaultProfil.setBrightness(profil->getBrightness()); + defaultProfil.setRadiusRatio(profil->getRadiusRatio()); + defaultProfil.setUpperMinThreshold(profil->getUpperMinThreshold()); + defaultProfil.setLowerMinThreshold(profil->getLowerMinThreshold()); + + loadProfil(profil, profil); +} + +void CameraFocus::loadProfil(FrameProcessProfil *profilLoaded, FrameProcessProfil *profilSaved){ + + profilSaved->setContrast(profilLoaded->getContrast()); + profilSaved->setBrightness(profilLoaded->getBrightness()); + profilSaved->setRadiusRatio(profilLoaded->getRadiusRatio()); + profilSaved->setUpperMinThreshold(profilLoaded->getUpperMinThreshold()); + profilSaved->setLowerMinThreshold(profilLoaded->getLowerMinThreshold()); + + ui->dbsbxContrast->setValue(profilLoaded->getContrast()); + ui->sbxBrightness->setValue(profilLoaded->getBrightness()); + ui->sbxRadiusRatio->setValue(profilLoaded->getRadiusRatio()); + ui->sbxUpperMin->setValue(profilLoaded->getUpperMinThreshold()); + ui->sbxLowerMin->setValue(profilLoaded->getLowerMinThreshold()); } void CameraFocus::on_dbsbxContrast_valueChanged(double arg1) { - profil_t p = setup->beamer.getProfil(); - p.contrast = arg1; - setup->beamer.setProfil(p); + setup->beamer.getProfil()->setContrast(arg1); } void CameraFocus::on_sbxBrightness_valueChanged(int arg1) { - profil_t p = setup->beamer.getProfil(); - p.brightness = arg1; - setup->beamer.setProfil(p); + setup->beamer.getProfil()->setBrightness(arg1); } void CameraFocus::on_sbxRadiusRatio_valueChanged(int arg1) { - profil_t p = setup->beamer.getProfil(); - p.ratioRadius = arg1; - setup->beamer.setProfil(p); + setup->beamer.getProfil()->setRadiusRatio(arg1); } void CameraFocus::on_sbxUpperMin_valueChanged(int arg1) { - profil_t p = setup->beamer.getProfil(); - p.upperMinThreshold = arg1; - setup->beamer.setProfil(p); + setup->beamer.getProfil()->setUpperMinThreshold(arg1); } void CameraFocus::on_sbxLowerMin_valueChanged(int arg1) { - profil_t p = setup->beamer.getProfil(); - p.lowerMinThreshold = arg1; - setup->beamer.setProfil(p); + setup->beamer.getProfil()->setLowerMinThreshold(arg1); +} + + +void CameraFocus::on_btnReset_clicked() +{ + loadProfil(&defaultProfil, setup->beamer.getProfil()); } diff --git a/app/SandboxSetup/camerafocus.h b/app/SandboxSetup/camerafocus.h index 3922454a6c3f8affcba4c1cc09a629938fc6a517..914d758ecd8671feb506b1e00622dd3809cd548a 100644 --- a/app/SandboxSetup/camerafocus.h +++ b/app/SandboxSetup/camerafocus.h @@ -34,13 +34,17 @@ private slots: void on_sbxLowerMin_valueChanged(int arg1); + void on_btnReset_clicked(); + private: Ui::CameraFocus *ui; SandboxSetup *setup; bool state = false; QTimer *frameTimer; + FrameProcessProfil defaultProfil; void refreshFrame(); + void loadProfil(FrameProcessProfil *profilLoaded, FrameProcessProfil *profilSaved); void initCameraParams(); }; diff --git a/app/SandboxSetup/camerafocus.ui b/app/SandboxSetup/camerafocus.ui index d041a25d5735fbbedc04e3be370144bccac444b8..dcaa483dbb30457ffd2b25bb16b13163d3cdb678 100644 --- a/app/SandboxSetup/camerafocus.ui +++ b/app/SandboxSetup/camerafocus.ui @@ -33,7 +33,7 @@ <property name="geometry"> <rect> <x>80</x> - <y>360</y> + <y>380</y> <width>481</width> <height>171</height> </rect> @@ -147,17 +147,17 @@ <bool>false</bool> </property> </widget> - <widget class="QLabel" name="lblStatus"> + <widget class="QPushButton" name="btnReset"> <property name="geometry"> <rect> - <x>450</x> - <y>20</y> - <width>67</width> - <height>17</height> + <x>530</x> + <y>350</y> + <width>89</width> + <height>25</height> </rect> </property> <property name="text"> - <string>Nothing</string> + <string>Reset</string> </property> </widget> </widget> diff --git a/app/SandboxSetup/main.cpp b/app/SandboxSetup/main.cpp index 58aa53f32a447aca65d83618cd55a2e47b0333bb..079ccbf69a9e593b578edc1946a5d991caf24582 100644 --- a/app/SandboxSetup/main.cpp +++ b/app/SandboxSetup/main.cpp @@ -27,6 +27,14 @@ int main(int argc, char *argv[]) return 1; } + FrameProcessProfil p = *setup.beamer.getProfil(); + std::cout << "params" << std::endl; + std::cout << p.getContrast() << std::endl; + std::cout << p.getBrightness() << std::endl; + std::cout << p.getRadiusRatio() << std::endl; + std::cout << p.getUpperMinThreshold() << std::endl; + std::cout << p.getLowerMinThreshold() << std::endl; + if(setup.setupProjection()){ std::cout << "Cancel crop" << std::endl; diff --git a/inc/beamer.h b/inc/beamer.h index 9b4ea72f71bda1acd6c42f9eda4b065942d21c1f..d0460f535330eeceb33434e7090f36f5fee55377 100644 --- a/inc/beamer.h +++ b/inc/beamer.h @@ -5,28 +5,44 @@ #define ESCAPE_CHAR 27 -typedef struct { - double contrast; - int brightness; - int ratioRadius; - int upperMinThreshold; - int lowerMinThreshold; -} profil_t; + +class FrameProcessProfil{ + private: + double contrast; // [0, 255] + int brightness; // [-255, 255] + // radius of the circle based on the width of the frame, where : + // radius = frame.width / ratioRadius + int ratioRadius; // [1, n] + int upperMinThreshold; // [lowerMin, 255] + int lowerMinThreshold; // [0, upperMin] + + public: + FrameProcessProfil(double c=1.0, int b=0, int r=8, int up=200, int low=100){ + contrast = c; + brightness = b; + ratioRadius = r; + upperMinThreshold = up; + lowerMinThreshold = low; + }; + double getContrast(){ return contrast; }; + void setContrast(double c){ contrast = c; }; + int getBrightness(){ return brightness; }; + void setBrightness(int b){ brightness = b; }; + int getRadiusRatio(){ return ratioRadius; }; + void setRadiusRatio(int r){ ratioRadius = r; }; + int getUpperMinThreshold(){ return upperMinThreshold; }; + void setUpperMinThreshold(int up){ upperMinThreshold = up; }; + int getLowerMinThreshold(){ return lowerMinThreshold; }; + void setLowerMinThreshold(int low){ lowerMinThreshold = low; }; + +}; class Beamer{ private: const char *BEAMER_POSITION_FILE = "./beamer.dat"; cv::Point3f beamerPosition; cv::Size resolution = cv::Size(256,144); -/* - double contrast = 1.0; // (0,255) - int brightness = 0; // (-255,255) - // circle must be at least 1/8 of the frame's width long - int ratioRadius = 8; // (1,n) - int upperMinThreshold = 200; // (lowerMin,255) - int lowerMinThreshold = 100; // (0,upperMin) -*/ - profil_t profil = (profil_t){ 1.0, 0, 8, 200, 100 }; + FrameProcessProfil profil = FrameProcessProfil(); cv::Point3f intersection(cv::Vec3f v1, cv::Point3f p1, cv::Vec3f v2, cv::Point3f p2, cv::Vec3f v3, cv::Point3f p3, bool &isFound); cv::Point3d approximatePosition(std::vector<cv::Point3d> points1, std::vector<cv::Point3d> points2); @@ -40,8 +56,8 @@ class Beamer{ int getWidth(){ return resolution.width; }; void setHeight(int h){ resolution.height = h; }; int getHeight(){ return resolution.height; }; - profil_t getProfil(){ return profil; }; - void setProfil(profil_t p){ profil = p; }; + FrameProcessProfil* getProfil(){ return &profil; }; + void setProfil(FrameProcessProfil p){ profil = p; }; int findBeamerFrom(Camera camera); cv::Mat editContrast(cv::Mat image, double contrast, int brightness); diff --git a/inc/sandboxConfig.h b/inc/sandboxConfig.h index 0de6f2900211e8c580f12d78ba7c0de9ab9dcdf2..3898d465c1b93ac29c4bf4d647b513f01daceb4d 100644 --- a/inc/sandboxConfig.h +++ b/inc/sandboxConfig.h @@ -11,6 +11,7 @@ #define POSITION "BeamerPosition" #define MATRIX "AdjustingMatrix" #define RESOLUTION "BeamerResolution" +#define PROCESSPROFIL "FrameProcessProfil" class SandboxConfig{ private: @@ -21,10 +22,13 @@ class SandboxConfig{ 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 loadCroppingMaskFrom(char *path, Camera *camera); static int loadBeamerPositionFrom(char *path, Beamer *beamer); static int loadBeamerResolutionFrom(char *path, Beamer *beamer); + static int loadFrameProcessProfil(char *path, FrameProcessProfil *profil); }; diff --git a/inc/sandboxSetup.h b/inc/sandboxSetup.h index abfbbf83b40b2a071ae5f695d24be95cff6509ca..e3a93891228a86b229425c5fd4a7f8cc025c871b 100644 --- a/inc/sandboxSetup.h +++ b/inc/sandboxSetup.h @@ -25,16 +25,19 @@ class SandboxSetup{ // save config in file => persistant int saveConfigFrom(char *path); int saveConfig(); + int saveFrameProcessProfil(); // edit variables of config => not persistant int setupProjection(); int setupBeamerResolution(); int setupBeamerLocation(); + int loadFrameProcessProfil(); void setBeamerResolution(cv::Size resolution); void setBeamerPosition(cv::Point3f pos); void setCroppingMask(cv::Rect mask); void setAdjustingMatrix(cv::Mat matrix); + void setFrameProcessProfil(FrameProcessProfil profil); }; diff --git a/src/components/beamer.cpp b/src/components/beamer.cpp index 039ff2566b562e717e439c9992f21168b2abfca5..bbb9198ab484bb4535eac995e3e8bebdee94f93b 100644 --- a/src/components/beamer.cpp +++ b/src/components/beamer.cpp @@ -49,7 +49,7 @@ int Beamer::findBeamerFrom(Camera camera) rgb = camera.getRGBFrame(); // Look for the circle target - std::vector<int> crc = findCercleZ(rgb, profil.contrast, profil.brightness, profil.ratioRadius, profil.upperMinThreshold, profil.lowerMinThreshold); + std::vector<int> crc = findCercleZ(rgb, profil.getContrast(), profil.getBrightness(), profil.getRadiusRatio(), profil.getUpperMinThreshold(), profil.getLowerMinThreshold()); cv::Scalar color = (!crc.empty()) ? cv::Scalar(0, 255, 0) : cv::Scalar(0, 0, 255); // Show black screen with cross diff --git a/src/lib/sandboxSetup.cpp b/src/lib/sandboxSetup.cpp index 6279bec7cc3031d022c4531e3ef3540d62833231..3938ff858572f461747b55c68e85150acd9db8f2 100644 --- a/src/lib/sandboxSetup.cpp +++ b/src/lib/sandboxSetup.cpp @@ -22,6 +22,10 @@ void SandboxSetup::setAdjustingMatrix(cv::Mat matrix){ projection.setAdjustingMatrix(matrix); } +void SandboxSetup::setFrameProcessProfil(FrameProcessProfil profil){ + beamer.setProfil(profil); +} + // return 1 when config can't be saved in file int SandboxSetup::saveConfigFrom(char *path){ @@ -37,6 +41,9 @@ int SandboxSetup::saveConfigFrom(char *path){ if(SandboxConfig::saveBeamerPositionInto(path, beamer.getPosition())) return 1; + if(SandboxConfig::saveFrameProcessProfil(path, *beamer.getProfil())) + return 1; + return 0; } @@ -45,6 +52,14 @@ int SandboxSetup::saveConfig(){ return saveConfigFrom(defaultConfigFilePath); } +int SandboxSetup::loadFrameProcessProfil(){ + return SandboxConfig::loadFrameProcessProfil(defaultConfigFilePath, beamer.getProfil()); +} + +int SandboxSetup::saveFrameProcessProfil(){ + return SandboxConfig::saveFrameProcessProfil(defaultConfigFilePath, *beamer.getProfil()); +} + int SandboxSetup::setupBeamerResolution(){ int width = 0; diff --git a/src/tools/sandboxConfig.cpp b/src/tools/sandboxConfig.cpp index 36c0593a7e42bcaea7a8b4fd809add916f4ee1d1..548e94512a6af4f9d1ae7b8a6593504f47179207 100644 --- a/src/tools/sandboxConfig.cpp +++ b/src/tools/sandboxConfig.cpp @@ -95,6 +95,32 @@ int SandboxConfig::saveBeamerResolutionInto(char *path, cv::Size res){ } +int SandboxConfig::saveFrameProcessProfil(char *path, FrameProcessProfil profil){ + + YAML::Node val; + + val["contrast"] = profil.getContrast(); + val["brightness"] = profil.getBrightness(); + val["radiusRatio"] = profil.getRadiusRatio(); + val["upperMinThreshold"] = profil.getUpperMinThreshold(); + val["lowerMinThreshold"] = profil.getLowerMinThreshold(); + + YAML::Node config; + try{ + config = YAML::LoadFile(path); + }catch(YAML::BadFile err){ + //std::cout << "[Error] No Config File found : " << err.what() << std::endl; + } + + config[PROCESSPROFIL] = val; + + return saveConfigIn(path, config); +} + + + + + int SandboxConfig::loadAdjustingMatrixFrom(char *path, BeamerProjection *projection){ @@ -154,12 +180,12 @@ int SandboxConfig::loadCroppingMaskFrom(char *path, Camera *camera){ return 1; } - // no node for adjusting matrix + // no node for cropping mask if(!config[MASK]){ return 2; }; - // uncomplet data for adjusting matrix + // uncomplet data for cropping mask if(!(config[MASK]["x"] && config[MASK]["y"] && config[MASK]["width"] && config[MASK]["height"])){ return 2; } @@ -186,12 +212,12 @@ int SandboxConfig::loadBeamerPositionFrom(char *path, Beamer *beamer){ return 1; } - // no node for adjusting matrix + // no node for beamer position if(!config[POSITION]){ return 2; }; - // uncomplet data for adjusting matrix + // uncomplet data for beamer position if(!(config[POSITION]["x"] && config[POSITION]["y"] && config[POSITION]["z"])){ return 2; } @@ -217,12 +243,12 @@ int SandboxConfig::loadBeamerResolutionFrom(char *path, Beamer *beamer){ return 1; } - // no node for adjusting matrix + // no node for beamer resolution if(!config[RESOLUTION]){ return 2; }; - // uncomplet data for adjusting matrix + // uncomplet data for beamer resolution if(!(config[RESOLUTION]["height"] && config[RESOLUTION]["width"])){ return 2; } @@ -237,6 +263,42 @@ int SandboxConfig::loadBeamerResolutionFrom(char *path, Beamer *beamer){ } +int SandboxConfig::loadFrameProcessProfil(char *path, FrameProcessProfil *profil){ + + 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; + } + + // no node for frame process profil + if(!config[PROCESSPROFIL]){ + return 2; + }; + + // uncomplet data for frame process profil + if(!( config[PROCESSPROFIL]["contrast"] && + config[PROCESSPROFIL]["brightness"] && + config[PROCESSPROFIL]["radiusRatio"] && + config[PROCESSPROFIL]["upperMinThreshold"] && + config[PROCESSPROFIL]["lowerMinThreshold"] + )){ + return 2; + } + + profil->setContrast(config[PROCESSPROFIL]["contrast"].as<double>()); + profil->setBrightness(config[PROCESSPROFIL]["brightness"].as<int>()); + profil->setRadiusRatio(config[PROCESSPROFIL]["radiusRatio"].as<int>()); + profil->setUpperMinThreshold(config[PROCESSPROFIL]["upperMinThreshold"].as<int>()); + profil->setLowerMinThreshold(config[PROCESSPROFIL]["lowerMinThreshold"].as<int>()); + + return 0; +} + + static int saveConfigIn(char *path, YAML::Node config){ YAML::Emitter out;