add logging engine

add options to control harris algorithum
This commit is contained in:
2020-10-27 20:59:19 +01:00
parent 6defcad11b
commit 38680f029c
7 changed files with 172 additions and 54 deletions

View File

@ -10,7 +10,7 @@
#include "matutils.h"
#include "drawing.h"
#include "log.h"
struct DisplacmentMap
{
@ -26,8 +26,8 @@ static std::vector< std::vector<cv::Point2f > > sortPointsIntoRows(std::vector<c
cv::Point2f topLeft(*getTopLeft(points));
cv::Point2f bottomRight(*getBottomRight(points));
std::cout<<"topLeft "<<topLeft.x<<' '<<topLeft.y<<'\n';
std::cout<<"bottomRight "<<bottomRight.x<<' '<<bottomRight.y<<'\n';
Log(Log::DEBUG)<<"topLeft "<<topLeft.x<<' '<<topLeft.y;
Log(Log::DEBUG)<<"bottomRight "<<bottomRight.x<<' '<<bottomRight.y;
float fuzz = (bottomRight.x-topLeft.x)*0.01f;
@ -78,7 +78,7 @@ static float detimineXPitch(const std::vector<cv::Point2f>& 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 "<<xMinDist<<'\n';
Log(Log::DEBUG)<<__func__<<": xMinDist "<<xMinDist;
float meanXDistAccum = 0;
size_t validCount = 0;
@ -149,7 +149,7 @@ static void thompsonTauTest(const std::vector<float>& in, std::vector<size_t>& o
{
if(abs((in[i]-mean)/sd) > rej)
{
std::cout<<__func__<<": "<<i<<" is outlier mean: "<<mean<<" sd: "<<sd<<" n: "<<n<<'\n';
Log(Log::DEBUG)<<__func__<<": "<<i<<" is outlier mean: "<<mean<<" sd: "<<sd<<" n: "<<n<<'\n';
outliers.push_back(i);
removed = true;
}
@ -212,7 +212,7 @@ static DisplacmentMap calcDisplacementMap(const std::vector< std::vector<cv::Poi
const std::vector<cv::RotatedRect>& 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<cv::Poi
}
meanYdist = meanYdist/elipses.size();
std::cout<<__func__<<": meanYdist "<<meanYdist<<'\n';
Log(Log::DEBUG)<<__func__<<": meanYdist "<<meanYdist;
for(size_t rowCnt = 0; rowCnt < rows.size(); ++rowCnt)
{
@ -244,7 +244,7 @@ static DisplacmentMap calcDisplacementMap(const std::vector< std::vector<cv::Poi
cv::Rect_<float> boundingRect = elipse.boundingRect2f();
std::cout<<__func__<<": Proc row "<<rowCnt<<'\n';
Log(Log::DEBUG)<<__func__<<": Proc row "<<rowCnt;
for(size_t i = 0; i < row.size(); ++i)
{
@ -253,7 +253,7 @@ static DisplacmentMap calcDisplacementMap(const std::vector< std::vector<cv::Poi
double normDist = ((row[i].x - boundingRect.x)/boundingRect.width-0.5)*2;
double tau = asin(normDist);
float xDest = (((2*tau)/M_PI)*500)+500;
std::cout<<__func__<<": normDist "<<normDist<<" tau "<<tau<<" xDest "<<xDest<<'\n';
Log(Log::DEBUG)<<__func__<<": normDist "<<normDist<<" tau "<<tau<<" xDest "<<xDest;
displacmentmap.destination[rowCnt].push_back(cv::Point2f(xDest,yDest));
}
}
@ -269,7 +269,7 @@ static void removeSparseCollums(cv::Mat& mat)
if(mat.at<float>(y,0) >= 0) ++front;
if(mat.at<float>(y,mat.cols-1) >= 0) ++back;
}
std::cout<<__func__<<" front: "<<front<<" back "<<back<<'\n';
Log(Log::DEBUG)<<__func__<<" front: "<<front<<" back "<<back;
cv::Rect roi;
bool frontRej = (front < mat.rows/2);
bool backRej = (back < mat.rows/2);
@ -286,7 +286,7 @@ static void generateRemapMaps(const DisplacmentMap& map, cv::Mat& xMat, cv::Mat&
{
if(map.destination.size() < 2)
{
std::cerr<<__func__<<": at least 2 rows are needed"<<std::endl;
Log(Log::ERROR)<<__func__<<": at least 2 rows are needed";
return;
}
@ -297,7 +297,7 @@ static void generateRemapMaps(const DisplacmentMap& map, cv::Mat& xMat, cv::Mat&
xMeanDist+=detimineXPitch(map.destination[i]);
}
xMeanDist/=map.destination.size();
std::cout<<__func__<<": xMeanDist "<<xMeanDist<<'\n';
Log(Log::DEBUG)<<__func__<<": xMeanDist "<<xMeanDist;
float xMin = std::numeric_limits<float>::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 "<<xMin<<'\n';
std::cout<<__func__<<": Grid: grid xMax "<<xMax<<'\n';
std::cout<<__func__<<": Grid: grid xMeanMin "<<xMeanMin<<'\n';
/*if(abs(xMeanMin-xMin) > (xMeanDist)/2)
{
std::cout<<__func__<<": Grid: xMin is outlier\n";
xMin = xMeanMin;
}*/
Log(Log::DEBUG)<<__func__<<": Grid: xMin "<<xMin;
Log(Log::DEBUG)<<__func__<<": Grid: grid xMax "<<xMax;
Log(Log::DEBUG)<<__func__<<": Grid: grid xMeanMin "<<xMeanMin;
size_t xGridSize = static_cast<size_t>(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: "<<xGridSize<<'x'<<map.destination.size()<<'\n';
Log(Log::DEBUG)<<"Grid: "<<xGridSize<<'x'<<map.destination.size();
for(int y = 0; y < xMat.rows; y++)
{
@ -339,32 +334,34 @@ static void generateRemapMaps(const DisplacmentMap& map, cv::Mat& xMat, cv::Mat&
bool found = findClosest(xIndex, yIndex,
cv::Point2f((x)*xMeanDist+xMin, (y)*yMeanDist),
map.destination, (2*xMeanDist)/5, (2*yMeanDist)/5);
std::cout<<__func__<<": found: "<<found<<' '<<xIndex<<'x'<<yIndex<<" at: "<<(x)*xMeanDist+xMin<<'x'<<(y)*yMeanDist<<'\n';
Log(Log::DEBUG)<<__func__<<": found: "<<found<<' '<<xIndex<<'x'<<yIndex<<" at: "<<(x)*xMeanDist+xMin<<'x'<<(y)*yMeanDist;
colx[x] = found ? map.source[yIndex][xIndex].x : -1;
coly[x] = found ? map.source[yIndex][xIndex].y : -1;
}
}
std::cout<<__func__<<": xMat raw\n"<<xMat<<'\n';
Log(Log::DEBUG)<<__func__<<": xMat raw\n"<<xMat;
removeSparseCollums(xMat);
removeSparseCollums(yMat);
std::cout<<__func__<<": xMat rejcted\n"<<xMat<<'\n';
Log(Log::DEBUG)<<__func__<<": xMat rejcted\n"<<xMat;
fillMissing(xMat);
std::cout<<__func__<<": xMat filled\n"<<xMat<<'\n';
Log(Log::DEBUG)<<__func__<<": xMat filled\n"<<xMat;
interpolateMissing(xMat);
std::cout<<__func__<<": yMat raw \n"<<yMat<<'\n';
Log(Log::DEBUG)<<__func__<<": yMat raw \n"<<yMat;
interpolateMissing(yMat);
std::cout<<__func__<<": xMat \n"<<xMat<<'\n';
std::cout<<__func__<<": yMat \n"<<yMat<<'\n';
Log(Log::INFO)<<__func__<<": xMat \n"<<xMat;
Log(Log::INFO)<<__func__<<": yMat \n"<<yMat;
}
static std::vector<cv::Point2f> detectPoints(cv::Mat& image, const cv::Mat& mask, bool verbose = false)
static std::vector<cv::Point2f> 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<cv::Point2f> 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<cv::Point2f> 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<cv::Point2f > points = detectPoints(image, mask, verbose);
std::vector<cv::Point2f > points = detectPoints(image, mask, blockSize, apature, detectorParameter, minSize, verbose);
if(verbose) std::cout<<"Found "<<points.size()<<" points\n";
if(points.size() < 8)
{
std::cout<<"Error creating map, insufficant points detected\n";
Log(Log::ERROR)<<"Error creating map, insufficant points detected";
return false;
}
@ -411,15 +410,15 @@ bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat&
if(verbose) std::cout<<"Found "<<rows.size()<<" rows\n";
if(rows.size() < 2)
{
std::cout<<"Error creating map, insufficant rows detected\n";
Log(Log::ERROR)<<"Error creating map, insufficant rows detected";
return false;
}
std::vector< cv::RotatedRect > ellipses = fitEllipses(rows);
if(verbose) std::cout<<"Found "<<ellipses.size()<<" ellipses. rows reduced to "<<rows.size()<<'\n';
if(verbose) Log(Log::INFO)<<"Found "<<ellipses.size()<<" ellipses. rows reduced to "<<rows.size();
if(ellipses.size() < 3)
{
std::cout<<"Error creating map, insufficant ellipses detected\n";
Log(Log::ERROR)<<"Error creating map, insufficant ellipses detected";
return false;
}
@ -446,7 +445,7 @@ bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat&
DisplacmentMap dispMap = calcDisplacementMap(rows, ellipses);
if(dispMap.destination.size() < 2)
{
std::cout<<"Error creating map, unable to calculate destination points";
Log(Log::ERROR)<<"Error creating map, unable to calculate destination points";
return false;
}
@ -470,14 +469,22 @@ bool createRemapMap(cv::Mat& image, const std::string& fileName, const cv::Mat&
sanityCheckMap(xMat, 0, image.cols-1, 0, image.cols-1);
sanityCheckMap(yMat, 0, image.rows-1, 0, image.rows-1);
if(xMat.cols < 3 || xMat.rows < 3)
{
Log(Log::ERROR)<<"Error creating map, to few points with high confidence";
return false;
}
cv::FileStorage matf(fileName+".mat", cv::FileStorage::WRITE );
matf<<"xmat"<<xMat<<"ymat"<<yMat;
matf.release();
Log(Log::INFO)<<"Unwrap maps saved to "<<fileName<<".mat";
if(verbose)
{
cv::Mat remaped;
applyRemap(image, remaped, xMat, yMat, cv::Size(900,1000));
applyRemap(image, remaped, xMat, yMat, 800);
cv::imshow( "Viewer", remaped );
cv::waitKey(0);
}
@ -509,12 +516,12 @@ bool findDeadSpace(const cv::Mat& mat, cv::Rect& roi)
return true;
}
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 outputSize)
{
cv::Mat xMapResized;
cv::Mat yMapResized;
cv::resize(xmap, xMapResized, cv::Size(800,900), cv::INTER_CUBIC);
cv::resize(ymap, yMapResized, cv::Size(800,900), cv::INTER_CUBIC);
cv::resize(xmap, xMapResized, cv::Size(outputSize,outputSize*(xmap.rows/(double)xmap.cols)), cv::INTER_CUBIC);
cv::resize(ymap, yMapResized, cv::Size(outputSize,outputSize*(xmap.rows/(double)xmap.cols)), cv::INTER_CUBIC);
cv::Rect roi;
cv::Mat xMapRed;
cv::Mat yMapRed;