From 38680f029c30b00fb805ec569f714c3e08de9d49 Mon Sep 17 00:00:00 2001 From: uvos Date: Tue, 27 Oct 2020 20:59:19 +0100 Subject: [PATCH] add logging engine add options to control harris algorithum --- argpopt.h | 29 +++++++++++++++- bgremoval.cpp | 3 +- log.h | 76 ++++++++++++++++++++++++++++++++++++++++++ main.cpp | 19 +++++++---- normalize.h | 2 +- unwrap.cpp | 91 +++++++++++++++++++++++++++------------------------ unwrap.h | 6 ++-- 7 files changed, 172 insertions(+), 54 deletions(-) create mode 100644 log.h diff --git a/argpopt.h b/argpopt.h index e4e1080..a11ad1d 100644 --- a/argpopt.h +++ b/argpopt.h @@ -27,10 +27,15 @@ struct Config std::string norm = ""; std::string maps = ""; std::string bg = ""; + int blockSize = 5; + int apature = 5; + float detectorParameter = 0.01; + float minSize = 7; bool verbose = false; + bool quiet = false; }; -const char *argp_program_version = "0.1"; +const char *argp_program_version = "0.2"; const char *argp_program_bug_address = ""; static char doc[] = "Program to determine the lubricant thikness on a curved surface"; static char args_doc[] = ""; @@ -38,9 +43,14 @@ static char args_doc[] = ""; static struct argp_option options[] = { {"verbose", 'v', 0, 0, "Enable verbose logging" }, +{"quiet", 'q', 0, 0, "Disable info messages" }, {"map", 'm', "File Name", 0, "remap maps file" }, {"bg", 'b', "File Name", 0, "background image file" }, {"normalize", 'n', "File Name", 0, "image to use as a normalization source" }, +{"apature", 'a', "Value", 0, "Sobel size" }, +{"block-size", 't', "Value", 0, "Harris neighborhood size " }, +{"detector-arameter", 'd', "Value", 0, "Harris detector parameter" }, +{"min-size", 's', "Value", 0, "Smallest feature accepted as a corner" }, { 0 } }; @@ -53,6 +63,9 @@ error_t parse_opt (int key, char *arg, struct argp_state *state) case 'v': config->verbose = true; break; + case 'q': + config->quiet = true; + break; case 'm': config->maps.assign(arg); break; @@ -62,6 +75,20 @@ error_t parse_opt (int key, char *arg, struct argp_state *state) case 'n': config->norm.assign(arg); break; + case 'a': + config->apature=atol(arg); + if(config->apature % 2 == 0) ++config->apature; + break; + case 't': + config->blockSize=atol(arg); + if(config->blockSize % 2 == 0) ++config->blockSize; + break; + case 'd': + config->detectorParameter=atol(arg); + break; + case 's': + config->minSize=atol(arg); + break; case ARGP_KEY_NO_ARGS: argp_usage(state); case ARGP_KEY_ARG: diff --git a/bgremoval.cpp b/bgremoval.cpp index 02af5d9..30e92e7 100644 --- a/bgremoval.cpp +++ b/bgremoval.cpp @@ -3,12 +3,13 @@ #include #include #include +#include "log.h" bool createMask(const cv::Mat& in, cv::Mat& mask, const cv::Mat& bg) { if(in.size != bg.size || in.type() != bg.type()) { - std::cerr<<"input image and backgournd image size and type needs to be the same\n"; + Log(Log::ERROR)<<"input image and backgournd image size and type needs to be the same"; return false; } diff --git a/log.h b/log.h new file mode 100644 index 0000000..5f3a57d --- /dev/null +++ b/log.h @@ -0,0 +1,76 @@ +#pragma once +#include +#include + +class Log +{ +public: + + enum Level + { + DEBUG, + INFO, + WARN, + ERROR + }; + +private: + bool opened = false; + Level msglevel = DEBUG; + + inline std::string getLabel(Level level) + { + std::string label; + switch(level) + { + case DEBUG: + label = "DEBUG"; + break; + case INFO: + label = "INFO "; + break; + case WARN: + label = "WARN "; + break; + case ERROR: + label = "ERROR"; + break; + } + return label; + } + +public: + + + inline static bool headers = false; + inline static Level level = WARN; + + Log() {} + Log(Level type) + { + msglevel = type; + if(headers) + { + operator << ("["+getLabel(type)+"]"); + } + } + + ~Log() + { + if(opened) + { + std::cout<<'\n'; + } + opened = false; + } + + template Log &operator<<(const T &msg) + { + if(msglevel >= level) + { + std::cout< inImages = loadImages(config.inFileNames); if(inImages.empty()) { - std::cout<<"Input images must be provided!\n"; + Log(Log::ERROR)<<"Input images must be provided!"; return -1; } @@ -80,16 +83,18 @@ int main(int argc, char* argv[]) cv::waitKey(0); } } - else std::cout<<"can not read background image from "< labPlanes(3); cv::split(labImage, labPlanes); - cv::add(labPlanes[0], labPlanesRef[0], labPlanes[0], cv::noArray(), CV_8UC1); + cv::subtract(labPlanes[0], labPlanesRef[0], labPlanes[0], cv::noArray(), CV_8UC1); cv::merge(labPlanes, labImage); cv::cvtColor(labImage, image, cv::COLOR_Lab2BGR); return true; diff --git a/unwrap.cpp b/unwrap.cpp index ec42ba6..f3e2efe 100644 --- a/unwrap.cpp +++ b/unwrap.cpp @@ -10,7 +10,7 @@ #include "matutils.h" #include "drawing.h" - +#include "log.h" struct DisplacmentMap { @@ -26,8 +26,8 @@ static std::vector< std::vector > sortPointsIntoRows(std::vector& row, float fuzz = 1. xRowDists.push_back(abs(row[i+1].x-row[i].x)); float xMinDist = *std::min(xRowDists.begin(), xRowDists.end()); - std::cout<<__func__<<": xMinDist "<& in, std::vector& o { if(abs((in[i]-mean)/sd) > rej) { - std::cout<<__func__<<": "<& elipses) { if(rows.size() < 2 || rows.size() != elipses.size()) { - std::cerr<<__func__<<": rows < 2 or rows != elipses\n"; + Log(Log::ERROR)<<__func__<<": rows < 2 or rows != elipses"; return DisplacmentMap(); } @@ -235,7 +235,7 @@ static DisplacmentMap calcDisplacementMap(const std::vector< std::vector boundingRect = elipse.boundingRect2f(); - std::cout<<__func__<<": Proc row "<(y,0) >= 0) ++front; if(mat.at(y,mat.cols-1) >= 0) ++back; } - std::cout<<__func__<<" front: "<::max(); float xMeanMin = 0; @@ -313,20 +313,15 @@ static void generateRemapMaps(const DisplacmentMap& map, cv::Mat& xMat, cv::Mat& } xMeanMin = xMeanMin / map.destination.size(); - std::cout<<__func__<<": Grid: xMin "< (xMeanDist)/2) - { - std::cout<<__func__<<": Grid: xMin is outlier\n"; - xMin = xMeanMin; - }*/ + Log(Log::DEBUG)<<__func__<<": Grid: xMin "<(std::lround(abs((xMax-xMin)/xMeanDist))+1); xMat = cv::Mat::zeros(cv::Size(xGridSize, map.destination.size()), CV_32FC1); yMat = cv::Mat::zeros(cv::Size(xGridSize, map.destination.size()), CV_32FC1); - std::cout<<"Grid: "< detectPoints(cv::Mat& image, const cv::Mat& mask, bool verbose = false) +static std::vector detectPoints(cv::Mat& image, const cv::Mat& mask, + int blockSize = 5, int apature = 5, float detectorParameter = 0.01, + float minSize = 7, bool verbose = false) { cv::Mat gray; cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY); //detect corners cv::Mat corners; - cv::cornerHarris(gray, corners, 5, 5, 0.01); + cv::cornerHarris(gray, corners, blockSize, apature, detectorParameter); cv::normalize(corners, corners, 0, 255, cv::NORM_MINMAX, CV_32FC1, cv::Mat()); cv::convertScaleAbs( corners, corners ); cv::threshold(corners, corners, 50, 255, cv::THRESH_BINARY); @@ -381,7 +378,7 @@ static std::vector detectPoints(cv::Mat& image, const cv::Mat& mask //get middle of corners cv::SimpleBlobDetector::Params blobParams; blobParams.filterByArea = true; - blobParams.minArea = 7; + blobParams.minArea = minSize; blobParams.maxArea = 500; blobParams.filterByColor = false; blobParams.blobColor = 255; @@ -397,13 +394,15 @@ static std::vector detectPoints(cv::Mat& image, const cv::Mat& mask return points; } -bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat& mask, bool verbose) +bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat& mask, + int blockSize, int apature, float detectorParameter, float minSize, + bool verbose) { - std::vector points = detectPoints(image, mask, verbose); + std::vector points = detectPoints(image, mask, blockSize, apature, detectorParameter, minSize, verbose); if(verbose) std::cout<<"Found "< ellipses = fitEllipses(rows); - if(verbose) std::cout<<"Found "< #include -bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat& mask, bool verbose = false); +bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat& mask, + int blockSize = 5, int apature = 5, float detectorParameter = 0.01, float minSize = 7, + bool verbose = false); -void applyRemap(cv::Mat& image, cv::Mat& out, const cv::Mat& xmap, const cv::Mat& ymap, cv::Size size); +void applyRemap(cv::Mat& image, cv::Mat& out, const cv::Mat& xmap, const cv::Mat& ymap, unsigned int size);