/** * Lubricant Detecter * Copyright (C) 2021 Carl Klemm * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 3 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include "argpopt.h" #include "uvosunwrap/unwrap.h" #include "uvosunwrap/bgremoval.h" #include "uvosunwrap/normalize.h" #include "uvosunwrap/log.h" #include "uvosunwrap/charuco.h" #include "uvosunwrap/harris.h" #include "uvosunwrap/curve.h" #define IMREAD_SIZE pow(2, 20) enum { CREATE_CHARUCO, CREATE_MAP, APPLY_MAP, APPLY_CURVE, CREATE_CURVE, SHOW_IMAGE, EXIT }; int selectOperation(char** opt) { if(!opt || !opt[0]) return -1; else if(strcmp(opt[0], "create" ) == 0) return CREATE_MAP; else if(strcmp(opt[0], "apply" ) == 0) return APPLY_MAP; else if(strcmp(opt[0], "curve" ) == 0) return APPLY_CURVE; else if(strcmp(opt[0], "mkcurve" ) == 0) return CREATE_CURVE; else if(strcmp(opt[0], "mkboard" ) == 0) return CREATE_CHARUCO; else if(strcmp(opt[0], "show" ) == 0) return SHOW_IMAGE; else if(strcmp(opt[0], "exit" ) == 0) return EXIT; return -1; } cv::Mat openImageImg(char* fileName) { int fd = open(fileName, O_RDONLY); if(fd < 0) { Log(Log::WARN)<<"could not open "< buffer(IMREAD_SIZE); size_t count; while((count = read(fd, buffer.data()+pos, IMREAD_SIZE)) > 0) { pos+=count; buffer.resize(pos+IMREAD_SIZE); Log(Log::WARN)<>image; if(matf.isOpened() && !image.data) { Log(Log::WARN)< loadImages(char** fileNames) { std::vector images; for(size_t i = 0; fileNames[i]; ++i) { cv::Mat tmpImage; const std::string str(fileNames[i]); if(str.find(".mat") != std::string::npos) { Log(Log::DEBUG)<<__func__<<": "< inImages; if(operation == CREATE_MAP || operation == APPLY_MAP || operation == APPLY_CURVE || operation == SHOW_IMAGE) { inImages = loadImages(fileNames); if(inImages.empty()) { Log(Log::ERROR)<<"Input images must be provided"; return -1; } } if(operation == CREATE_CHARUCO) { std::string fileName = config.output.empty() ? "out.png" : config.output; createCharucoBoard(config.size*X_BOARD_SIZE, fileName); Log(Log::INFO)<<"Exported charuco map of size "< points; if(config.harris) points = harrisDetectPoints(inImages[0], mask); else points = detectCharucoPoints(inImages[0], config.verbose); Log(Log::INFO)<<"Found "< remapedImages; for(size_t i = 0; i>curve; if(!curve.data || curve.type() != CV_32FC1 || curve.rows != 2 || curve.cols < 3) { Log(Log::INFO)<<"invalid curve"; return -1; } if(inImages[0].channels() > 1) cvtColor(inImages[0], inImages[0], cv::COLOR_BGR2GRAY); if(inImages[0].type() != CV_32FC1) inImages[0].convertTo(inImages[0], CV_32F); Log(Log::DEBUG)<<"applyCurve"; applyCurve(inImages[0], curve); cv::FileStorage fsO("out.mat", cv::FileStorage::WRITE); fsO<<"image"< "; int num = 0; std::cin>>num; if(std::cin.fail()) { std::cin.clear(); std::cout<<"invalid number"; return -1; } cv::Mat curve = cv::Mat::zeros(2, num, CV_32FC1); float* keys = curve.ptr(0); float* values = curve.ptr(1); std::cout<<"Please type "< "; double key; double value; std::cin>>key; if(std::cin.fail()) { std::cin.clear(); --i; continue; } std::cin>>value; if(std::cin.fail()) { std::cin.clear(); --i; continue; } keys[i] = key; values[i] = value; } for(int i = 0; i < curve.cols; ++i) std::cout< "; char cmdline[1024]; char** tokens = new char*[256]; char *token; char *r = cmdline; for(size_t i = 0; i < 256/sizeof(char*); ++i) tokens[i] = nullptr; std::cin.getline(cmdline, sizeof(cmdline), '\n'); for(size_t i = 0; (token = strtok_r(r, " ", &r)) && i < 256; ++i) { tokens[i] = new char[strlen(token)+1]; strcpy(tokens[i], token); } int operation = selectOperation(tokens); if(operation < 0) { Log(Log::ERROR)<<"A operation must be selected"; continue; } if(perfromOperation(operation, tokens+1, config) < 0) break; for(size_t i = 0; i < 256/sizeof(char*) && tokens[i]; ++i) delete[] tokens[i]; delete[] tokens; } } if(config.verbose) { cv::destroyWindow("Viewer"); cv::waitKey(0); } return ret; }