From 6f02bf4f9740265cf75b2883ae51973c4611162b Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Tue, 30 Jan 2018 18:16:26 +0100 Subject: [PATCH] added Histogramm Layer multiple errors --- Source/CMakeLists.txt | 1 + .../DestructionPower/DestructionPower.h | 2 +- .../AbstractionLayer_Histogram.cpp | 176 ++++++++++++++++++ .../AbstractionLayer_Histogram.h | 43 +++++ .../AbstractionLayer_Histogram_Properties.h | 29 +++ Source/functions/solve/puzzleExtension.cpp | 4 + Source/functions/solve/structure.cpp | 33 ++-- Source/header/input.h | 3 +- Source/header/solve.h | 3 + 9 files changed, 277 insertions(+), 17 deletions(-) create mode 100644 Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp create mode 100644 Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h create mode 100644 Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index f544bf4..6cacb59 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -14,6 +14,7 @@ set(SOURCE_FILES functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.cpp functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.cpp functions/AbstractionLayers/DestructionPower/DestructionPower.cpp + functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp header/solve.h header/input.h functions/solve/puzzleExtension.cpp) diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h index f51fc35..8d8eb9f 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h @@ -1,6 +1,6 @@ #pragma once //TODO!! increase Destructioncount -#define DESTRUCTION_COUNT 2 +#define DESTRUCTION_COUNT 1 #include "DestructionPower_Properties.h" #include "../AbstraktionLayer_Base.h" diff --git a/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp new file mode 100644 index 0000000..6b9f864 --- /dev/null +++ b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp @@ -0,0 +1,176 @@ +// +// Created by Niko on 1/11/2018. +// +#include "../../../header.h" +#include "AbstractionLayer_Histogram.h" + +using namespace cv; + +Mat HistogramComparer::readImages(int count) +{ + char name[100]; + Mat corr; + Mat ref_gray; + + sprintf(name, PATH, count); + Mat src = imread(name, 1); + // namedWindow("UP1", WINDOW_AUTOSIZE); + //imshow("UP1",src); + if (!src.data) + { + cerr << "Problem loading image!!!" << endl; + return src; + } + + if(DISPLAY)imshow("src",src); + + Mat im_color; + cvtColor(src, im_color, COLOR_BGR2HSV); + return im_color; + +} + +bool AbstractionLayer_Histogram::PreProcessing(coor mySize, const vector* partArray){ + // HistogramComparer localImage; + cout << "Abstraction 2 Preprocessing... " << flush; + const vector& ref_partArray = *partArray; + HistogramComparer analyse(mySize.row*mySize.col); + Part buf; + int iterator=0; + if(!analyse.getImages()) + { + cerr << "Error occured in getImages!" << endl; + return false; + } + else // hier werden alle vier verschiedenen Rotationsarten 'gleichzeitig' abgespeichert + //TODO rows and cols + + for(int i = 0; i < mySize.row*mySize.col; i++) + { + Mat src_img1 = analyse.readImages(i); + Mat hsv_img1; + /// Convert to HSV + cvtColor(src_img1, hsv_img1, COLOR_BGR2HSV); + + /// Using 50 bins for hue and 60 for saturation + int h_bins = 50; + int s_bins = 60; + int histSize[] = {h_bins, s_bins}; + + // hue varies from 0 to 179, saturation from 0 to 255 + float h_ranges[] = {0, 180}; + float s_ranges[] = {0, 256}; + + const float *ranges[] = {h_ranges, s_ranges}; + + // Use the o-th and 1-st channels + int channels[] = {0, 1}; + + /// Histograms + MatND hist_img1; + + /// Calculate the histograms for the HSV images + calcHist(&hsv_img1, 1, channels, Mat(), hist_img1, 2, histSize, ranges, true, false); + // normalize(hist_img1, hist_img1, 0, 1, NORM_MINMAX, -1, Mat()); + + ref_partArray[iterator]->m_his.image=hsv_img1; + iterator++; + + } + + + InitialiseConstraintMatrixSize(mySize.col, mySize.row); //col row switched in this function + + cout << "Done!" << endl; + return true; + +} + +bool AbstractionLayer_Histogram::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector){ + + //evaluateQuality = evaluateProbabilaty + for(int i = 0;i < qVector.size();i++) + { + if(PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_his.image)) + { + qVector[i].first=1; + continue; + } + if(constraintCoordinate.row<1 || constraintCoordinate.col < 1 ) + qVector[i].first=0.8; + else + qVector[i].first=0; + } + + +} +bool AbstractionLayer_Histogram::PlaceOfPartGood(coor myCoor, Mat& myPart) +{ + //sets coordinates to correct position for layer + + + if( myCoor.row == 1 && myCoor.col == 1){return true;} + else if(myCoor.col == 1 && myCoor.row >1){ + if(CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart)){ + return true; + } + else return false; + } + + else if( myCoor.row == 1 && myCoor.col >1){ + if(CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ + return true; + } + else return false; + } + else if (myCoor.col > 1 && myCoor.row >1){ + if( CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart) && + CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ + + return true; + } + else return false; + }else return false; + + +} + +bool AbstractionLayer_Histogram::CompareHistogram(Mat hist_img1,Mat hist_img2) +{ + // Correlation + double Correlation = compareHist(hist_img1, hist_img2, CV_COMP_CORREL); + + if(Correlation > 0.95 ){ + + return true; + } + else + return false; +} + +bool AbstractionLayer_Histogram::SetConstraintOnPosition(const coor constraintCoordinate, const AbstractionLayer_Histogram_Properties constraint) +{ + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].image=constraint.image; + //m_constraintMatrix[constraintCoordinate.col+1][constraintCoordinate.row+1].m_connections=constraint.m_connections; +} + +bool AbstractionLayer_Histogram::RemoveConstraintOnPosition(const coor constraintCoordinate) +{ + Mat dummy(1,1,0); + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].image = dummy; +} +bool HistogramComparer::getImages() { + + Mat src; + + for (int i = 0; i < nr_parts; i++) { + if (DISPLAY) cout << "Bild " << i << endl; + Mat img = readImages(i); + + if (!img.data) { + cerr << "Error: No pic found!!" << endl; + return false; + } + else return true; + } +} \ No newline at end of file diff --git a/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h new file mode 100644 index 0000000..9c69ff8 --- /dev/null +++ b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h @@ -0,0 +1,43 @@ +// +// Created by Niko on 1/11/2018. +// + +#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_H +#define MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_H +#define DISPLAY false +//#define PATH "..\\..\\..\\pieces\\%04d.jpg" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include "AbstractionLayer_Histogram_Properties.h" +#include "../AbstraktionLayer_Base.h" + +using namespace std; +using namespace cv; + +class AbstractionLayer_Histogram : public AbstractionLayer_Base +{ +public: + bool PreProcessing(coor mySize, const vector* partArray) override ; + bool EvaluateQuality ( coor constraintCoordinate, qualityVector& qVector)override; + bool RemoveConstraintOnPosition( coor constraintCoordinate)override; + bool PlaceOfPartGood(coor myCoor, Mat& myPart); + bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_Histogram_Properties constraint)override; + qualityVector returnInBox(vector& PuzzleBox); + void printConstraintMatrix(); + bool CompareHistogram(Mat Part, Mat RefPart); + +private: +}; + +class HistogramComparer{ +public: + explicit HistogramComparer(int s = 1008): nr_parts(s){} + Mat readImages(int); + bool getImages(); +private: + int nr_parts; + +}; + +#endif //MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_H + diff --git a/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h new file mode 100644 index 0000000..bb1270a --- /dev/null +++ b/Source/functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h @@ -0,0 +1,29 @@ +// +// Created by Niko on 1/11/2018. +// + +#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_PROPERTIES_H +#define MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_PROPERTIES_H +#include +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" + +using namespace cv; +class AbstractionLayer_Histogram_Properties +{ +public: + AbstractionLayer_Histogram_Properties() : Correlation(-1){} + double getCorrelation(){return Correlation;} + Mat getmHistogram(){return m_Histogram;} + +private: + + double Correlation; + Mat m_Histogram; + Mat image; + friend class AbstractionLayer_Histogram; + + +}; +#endif //MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_PROPERTIES_H + diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 60fabd2..59b6cea 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -70,6 +70,7 @@ void Puzzle::removeConstrains(coor removeCoordinates) this->a3.RemoveConstraintOnPosition(removeCoordinates); this->a4.RemoveConstraintOnPosition(removeCoordinates); this->acm.RemoveConstraintOnPosition(removeCoordinates); + this->his.RemoveConstraintOnPosition(removeCoordinates); //TODO!! Add other layer remove here } void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) @@ -90,6 +91,9 @@ void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) //a2 this->acm.SetConstraintOnPosition(setConstraints,constraintPiece->m_acm); + + //his + this->his.SetConstraintOnPosition(setConstraints,constraintPiece->m_his); //TODO!! Add other layer remove here } diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index fb0a773..883f2bd 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -58,27 +58,27 @@ coor calculateNextCoor(vector& log, Puzzle& puzzleMat) { if (log.size() == 1) - return {0,0}; - +// return {0,0}; +return {1,1}; unsigned int col= log.rbegin()[1].myCoor.col; unsigned int row= log.rbegin()[1].myCoor.row; //level 2 edges first - if(col == 0 && row < 27) - return {col,++row}; - if(row== 27 && col < 35) - return {++col,row}; - if(col == 35 && row >0) - return {col, --row}; - if(col >1&& row ==0) - return{--col,row}; - if(col==1 && row==0) - return {1,1}; +// if(col == 0 && row < 27) +// return {col,++row}; +// if(row== 27 && col < 35) +// return {++col,row}; +// if(col == 35 && row >0) +// return {col, --row}; +// if(col >1&& row ==0) +// return{--col,row}; +// if(col==1 && row==0) +// return {1,1}; - log.pop_back();//vis only!!! - puzzleMat.resultImage(log);//vis only!!! +// log.pop_back();//vis only!!! +// puzzleMat.resultImage(log);//vis only!!! if(row& log,Puzzle& puzzleMat) //TODO!! Add more layers here switch(log.back().abstractionLevel) { - case 0://pömpel + case 10://pömpel puzzleMat.a1.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; case 2://SURFFeature @@ -117,6 +117,9 @@ void solve(vector& log,Puzzle& puzzleMat) case 1://color puzzleMat.acm.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; + case 0://histogram + puzzleMat.his.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); + break; case -1://random setsolution(log,puzzleMat); return; diff --git a/Source/header/input.h b/Source/header/input.h index 848e75a..794e16b 100755 --- a/Source/header/input.h +++ b/Source/header/input.h @@ -10,7 +10,7 @@ #include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h" #include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching_Properties.h" #include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures_Properties.h" - +#include "../functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h" class LayerContainer; class Part @@ -47,6 +47,7 @@ public: AbstractionLayer_PoempelPosition_Properties m_a3; AbstractionLayer_SURFFeatures_Properties m_a4; AbstractionLayer_ColorMatching_Properties m_acm; + AbstractionLayer_Histogram_Properties m_his; private: int32_t m_partID; uint8_t m_numOfRotations; diff --git a/Source/header/solve.h b/Source/header/solve.h index ac9af6b..c1447ad 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -9,6 +9,7 @@ #include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h" #include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.h" #include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.h" +#include "../functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h" using namespace std; @@ -56,6 +57,7 @@ public: if(!a3.PreProcessing({cols,rows}, &p_myBox)) return false; if(!a4.PreProcessing({cols,rows}, &p_myBox)) return false; if(!acm.PreProcessing({cols,rows}, &p_myBox)) return false; + if(!his.PreProcessing({cols,rows}, &p_myBox)) return false; return true; } @@ -67,6 +69,7 @@ public: AbstractionLayer_PoempelPosition a3; AbstractionLayer_SURFFeatures a4; AbstractionLayer_ColorMatching acm; + AbstractionLayer_Histogram his; void removeConstrains(coor removeCoordinates); void setConstraints(coor setConstraints, Part *constraintPiece);