From dc71f6fa639cdcab82ba07a705e6077a324efbb1 Mon Sep 17 00:00:00 2001 From: niko-fhtw <33751312+niko-fhtw@users.noreply.github.com> Date: Fri, 19 Jan 2018 16:53:53 +0100 Subject: [PATCH 1/7] added firstdraft code --- .gitignore | 1 + Source/CMakeLists.txt | 2 +- .../Layer1/AbstractionLayer_1.cpp | 5 +- .../AbstractionLayer_Histogram.cpp | 160 ++++++++++++++++++ .../AbstractionLayer_Histogram.h | 40 +++++ .../AbstractionLayer_Histogram_Properties.h | 29 ++++ .../AbstractionLayer_MeanDifference.h | 35 ++++ ...stractionLayer_MeanDifference_Properties.h | 20 +++ Source/header/input.h | 2 + 9 files changed, 289 insertions(+), 5 deletions(-) create mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp create mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h create mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h create mode 100644 Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h create mode 100644 Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h diff --git a/.gitignore b/.gitignore index 112fbf0..3719e13 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ cmake-build-debug cmake-build-debug .idea .DS_Store +CMakeLists.txt diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 9ef8c1e..658b4fd 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -8,9 +8,9 @@ set(SOURCE_FILES main.cpp header.h functions/solve/structure.cpp - functions/AbstractionLayers/AbstraktionLayer_Base.h functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp + functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp functions/AbstractionLayers/DestructionPower/DestructionPower.cpp header/solve.h header/input.h diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index b594cff..33cde89 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -4,7 +4,6 @@ #include "AbstractionLayer_1.h" #include "../../../header.h" - #include #include @@ -47,6 +46,7 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr //it through qualityVector and removes all that do not trigger PlaceOfPartGood bool AbstractionLayer_1::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector) { + //evaluateQuality = evaluateProbabilaty for(int i = 0;im_a1.m_connections)) @@ -120,8 +120,6 @@ void AbstractionLayer_1::CreateRandomPuzzle() } - - //puts all pieces of the current constraint matrix into a puzzlebox qualityVector AbstractionLayer_1::returnInBox(vector& PuzzleBox) { @@ -407,7 +405,6 @@ Point analyseParts::findCenter(Mat img){ return center; } - vector analyseParts::findCorners(vector contour, Point center){ int minContourPoint = 5; vector> quad_contour; diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp new file mode 100644 index 0000000..a780cc6 --- /dev/null +++ b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp @@ -0,0 +1,160 @@ +// +// 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); + 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; + analyseParts 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 = localImage.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_aHistogram.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_aHistogram.image)) + { + qVector[i].first=1; + + continue; + } + qVector[i].first=0; + } + + +} +bool AbstractionLayer_Histogram::PlaceOfPartGood(coor myCoor, Mat& myPart) +{ + + HistogramComparer localComparer; + //sets coordinates to correct position for layer + myCoor.row++; + myCoor.col++; + + if( myCoor.row == 1 && myCoor.col == 1){return true;} + else if(myCoor.col == 1 && myCoor.row >1){ + if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart)){ + return true; + } + else return false; + } + else if( myCoor.row == 1 && myCoor.col >1){ + if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ + return true; + } + else return false; + } + else if (myCoor.col > 1 && myCoor.row >1){ + if( localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart) && + localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ + return true; + } + else return false; + }else return false; + + +} + +bool HistogramComparer::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+1][constraintCoordinate.row+1].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+1][constraintCoordinate.row+1].image = dummy; +} diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h new file mode 100644 index 0000000..5fb2a82 --- /dev/null +++ b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h @@ -0,0 +1,40 @@ +// +// 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(); + +private: +}; + +class HistogramComparer{ +public: + Mat readImages(int); + bool CompareHistogram(Mat Part, Mat RefPart); +private: + +}; + +#endif //MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_H + diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h new file mode 100644 index 0000000..bb1270a --- /dev/null +++ b/Source/functions/AbstractionLayers/LayerHistogram/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/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h new file mode 100644 index 0000000..b3556c2 --- /dev/null +++ b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h @@ -0,0 +1,35 @@ +// +// Created by Niko on 1/15/2018. +// + +#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H +#define MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H +#define DISPLAY false +#define PATH "..\\..\\..\\pieces\\%04d.jpg" + +using namespace std; +using namespace cv; + +class AbstractionLayer_MeanDifference : public AbstractionLayer_Base +{ +public: + bool PreProcessing(coor mySize, const vector* partArray) override ; + bool EvaluateQuality ( coor constraintCoordinate, qualityVector& qVector)override; + bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_1_Properties constraint)override; + bool RemoveConstraintOnPosition( coor constraintCoordinate)override; + bool PlaceOfPartGood(coor myCoor, Mat& myPart); + + qualityVector returnInBox(vector& PuzzleBox); + void printConstraintMatrix(); + +private: +}; + +class cMeanDifference{ +public: + Mat readImages(int); + bool calculateMeanDifference(Mat Part, Mat RefPart); +private: + +}; +#endif //MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H diff --git a/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h new file mode 100644 index 0000000..2a1cdd9 --- /dev/null +++ b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h @@ -0,0 +1,20 @@ +// +// Created by Niko on 1/15/2018. +// + +#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H +#define MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H + +class AbstractionLayer_MeanDifference_Properties +{ +public: + AbstractionLayer_MeanDifference_Properties() : MeanDifference(-1){} + double getMeanDifference(){return MeanDifference;}; + +private: + + double MeanDifference; + friend class AbstractionLayer_MeanDifference; + Mat image; +}; +#endif //MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H diff --git a/Source/header/input.h b/Source/header/input.h index 6cee3da..09c6e78 100755 --- a/Source/header/input.h +++ b/Source/header/input.h @@ -8,6 +8,7 @@ #include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h" +#include "../functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h" class LayerContainer; @@ -42,6 +43,7 @@ public: bool set; AbstractionLayer_1_Properties m_a1; + AbstractionLayer_Histogram_Properties m_aHistogram; private: int32_t m_partID; uint8_t m_numOfRotations; From ddd5d80750b8e19bc33ea6604d7c44fde88a9f94 Mon Sep 17 00:00:00 2001 From: niko-fhtw <33751312+niko-fhtw@users.noreply.github.com> Date: Fri, 19 Jan 2018 17:01:52 +0100 Subject: [PATCH 2/7] added to dispatcher_part2 --- Source/header/solve.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/header/solve.h b/Source/header/solve.h index 7bbc49a..a27bef6 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -9,6 +9,7 @@ #include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h" +#include "../functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h" using namespace std; @@ -46,6 +47,7 @@ public: createBox(); createp_box(); dp.PreProcessing({cols,rows}, nullptr); a1.PreProcessing({cols,rows}, &p_myBox); + a3.PreProcessing({cols,rows},&p_myBox); return true; } @@ -53,6 +55,7 @@ public: DestructionPower dp; AbstractionLayer_1 a1; + AbstractionLayer_Histogram a3; void removeConstrains(coor removeCoordinates); void setConstraints(coor setConstraints, Part *constraintPiece); From d91dded858ed38038fb42dd55993633551743b32 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Fri, 19 Jan 2018 21:36:31 +0100 Subject: [PATCH 3/7] RM: added into dispatcher strucure --- .../DestructionPower/DestructionPower.cpp | 9 ++++--- .../DestructionPower/DestructionPower.h | 4 +-- .../AbstractionLayer_Histogram.cpp | 8 +++--- Source/functions/solve/puzzleExtension.cpp | 9 ++++++- Source/functions/solve/structure.cpp | 27 ++++++++++++++++--- Source/header/input.h | 2 +- 6 files changed, 43 insertions(+), 16 deletions(-) diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index 9fb4998..f74d459 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -9,10 +9,11 @@ //sets relations of speed for the different layers map DestructionPower_Properties::SpeedTable = { - {0,0.99} + {0,0.99}, + {1,0.7}, + {2,0.7} }; - bool DestructionPower::PreProcessing(coor mySize,const vector* partArray) { cout << "DestructionPower Preprocessing... "; @@ -55,7 +56,7 @@ void DestructionPower::DestructionOfSurrounding(const coor constraintCoordinate) m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i] /=divisor; else //create default destructionPower //TODO find some better solution for default - m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i] =1-m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SpeedTable[i]; + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i] = m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SpeedTable[i]; } } @@ -102,6 +103,6 @@ DestructionPower_Properties::DestructionPower_Properties() { { DestructionArray.emplace_back((DestructionPower_Properties::SpeedTable[i]*DESTRUCTION_INIT)); - DestructionArray.back()<0.99 ? DestructionArray.back()*=aging:DestructionArray.back(); + DestructionArray.back()<0.8 ? DestructionArray.back()=aging*DestructionArray.back()+(float)0.01:DestructionArray.back(); } } diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h index 56d3ecf..4f8b506 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h @@ -3,8 +3,8 @@ // #pragma once - -#define DESTRUCTION_COUNT 1 +//TODO!! increase Destructioncount +#define DESTRUCTION_COUNT 2 #include "DestructionPower_Properties.h" #include "../AbstraktionLayer_Base.h" diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp index a780cc6..380ef48 100644 --- a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp +++ b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp @@ -71,7 +71,7 @@ HistogramComparer localImage; 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_aHistogram.image=hsv_img1; + ref_partArray[iterator]->m_Histogram.image=hsv_img1; iterator++; } @@ -89,7 +89,7 @@ bool AbstractionLayer_Histogram::EvaluateQuality (const coor constraintCoordinat //evaluateQuality = evaluateProbabilaty for(int i = 0;i < qVector.size();i++) { - if(PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_aHistogram.image)) + if(PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_Histogram.image)) { qVector[i].first=1; @@ -147,7 +147,7 @@ bool HistogramComparer::CompareHistogram(Mat hist_img1,Mat hist_img2) bool AbstractionLayer_Histogram::SetConstraintOnPosition(const coor constraintCoordinate, const AbstractionLayer_Histogram_Properties constraint) { - m_constraintMatrix[constraintCoordinate.col+1][constraintCoordinate.row+1].image=constraint.image; + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].image=constraint.image; //m_constraintMatrix[constraintCoordinate.col+1][constraintCoordinate.row+1].m_connections=constraint.m_connections; } @@ -156,5 +156,5 @@ bool AbstractionLayer_Histogram::SetConstraintOnPosition(const coor constraintCo bool AbstractionLayer_Histogram::RemoveConstraintOnPosition(const coor constraintCoordinate) { Mat dummy(1,1,0); - m_constraintMatrix[constraintCoordinate.col+1][constraintCoordinate.row+1].image = dummy; + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].image = dummy; } diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 84de8d3..40bb3a5 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -46,7 +46,8 @@ void Puzzle::putIntoBox() for(int rotations=0;rotations<4;rotations++) { tmpPart.m_a1.shift(1); - //TODO! add all other layers with their rotaionvariance here + //TODO! add all other layer with their rotaionvariance into "tmpPart" + //if it piece is roation invariant no need to do anything myBox.emplace_back(tmpPart); } @@ -65,6 +66,8 @@ void Puzzle::shuffle() void Puzzle::removeConstrains(coor removeCoordinates) { this->a1.RemoveConstraintOnPosition(removeCoordinates); + this->a3.RemoveConstraintOnPosition(removeCoordinates); + //TODO!! Add other layer remove here } void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) { @@ -75,6 +78,10 @@ void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) //a1 this->a1.SetConstraintOnPosition(setConstraints,constraintPiece->m_a1); + //a3 + this->a3.SetConstraintOnPosition(setConstraints,constraintPiece->m_Histogram); + + //TODO!! Add other layer remove here } void Puzzle::createRandomPuzzle() diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 05411a1..3beec78 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -35,7 +35,10 @@ bool next(vector& log,Puzzle& puzzleMat) setsolution(log,puzzleMat); else setsolution(log,puzzleMat); + if(log.back().myCoor.row==26) + cout << log.back().myCoor.row << ", " << log.back().myCoor.col << endl; return true; + } void createNextLogElement(vector& log, Puzzle& puzzleMat) @@ -55,7 +58,6 @@ coor calculateNextCoor(vector& log, Puzzle& puzzleMat) { //level 1: //go left to right, then increase current row - if (log.size() == 1) return {0,0}; @@ -73,11 +75,15 @@ void solve(vector& log,Puzzle& puzzleMat) { log.back().abstractionLevel = puzzleMat.dp.getNextAbstractionLayer(log.back().myCoor,log.back().abstractionLevel); //sets in abstractionLevel //status(log,p_Box,puzzleMat); + //TODO!! Add more layers here switch(log.back().abstractionLevel) { case 0://pömpel puzzleMat.a1.EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; + case 1:// + puzzleMat.a3.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); + break; case -1://random setsolution(log,puzzleMat); return; @@ -105,6 +111,8 @@ void setsolution(vector& log, Puzzle& puzzleMat) puzzleMat.combinedQualityVector.clear(); //clear data from temp variable //tell log entry that it is set log.back().Set(); + if(log.back().myCoor.row==27) + cout << "hello" << endl; puzzleMat.setConstraints(log.back().myCoor,log.back().PieceCollector.begin()->second); //cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; } @@ -187,6 +195,9 @@ float capLogElements(vector& log) break; } int newid=0; + //check if all over + if(id==log.back().PieceCollector.size()) + return 0; if(id>0) newid = --id; //set to the one just over limit @@ -241,6 +252,7 @@ bool SetBestOrMoreLayersArithmetical(vector& log, qualityVector& cqVec case 4: threshold = 0.60; break; default: threshold = 0.5; break; } + //TODO!! add more layers here! // check Quality of current Puzzle Piece in combinedQualityVector with Threshold value for (qualityVector::iterator it = cqVector.begin(); it != cqVector.end(); it++) @@ -269,6 +281,7 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, { bool summarizedVectors = false; int countSummarizedVectors = 0; + bool removePart=true; // check if both qualityVectors are not empty if(qVector.empty()) @@ -287,19 +300,25 @@ void CalculateNewCombinedQuality(vector& log, qualityVector& qVector, for (unsigned int i = 0; i < cqVector.size(); i++) { for (unsigned int j = 0; j < qVector.size(); j++) { // search same PuzzlePart of qualityVector and combinedQualityVector - if (&cqVector.at(i).second == &qVector.at(j).second) { + if (cqVector.at(i).second->GetPartID() == qVector.at(j).second->GetPartID() && cqVector.at(i).second->GetNumOfRotations() == qVector.at(j).second->GetNumOfRotations()) { // sum Quality of PieceCollector (qualityVector) to combinedQualityVector cqVector.at(j).first += qVector.at(i).first; countSummarizedVectors++; + removePart=false; break; // skip remaining for loop => save time! } // remove element at poisition X in combinedQualityVector, because it was not summarized // inefficient way to delete element X //cqVector->erase(cqVector->begin()+i); // efficient way, but no sorted cqVector => wayne //echt? lol - swap(cqVector.at(i), cqVector.back()); - cqVector.pop_back(); + } + if(removePart) + { + swap(cqVector.at(i), cqVector.back()); + cqVector.pop_back(); + } + } // cqVector should have the same size now as newest qVector diff --git a/Source/header/input.h b/Source/header/input.h index 09c6e78..8fd6181 100755 --- a/Source/header/input.h +++ b/Source/header/input.h @@ -43,7 +43,7 @@ public: bool set; AbstractionLayer_1_Properties m_a1; - AbstractionLayer_Histogram_Properties m_aHistogram; + AbstractionLayer_Histogram_Properties m_Histogram; private: int32_t m_partID; uint8_t m_numOfRotations; From 1c85bee091323a4ab3615b6baa4323d321f3b8f8 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Fri, 19 Jan 2018 22:09:12 +0100 Subject: [PATCH 4/7] RM: aging, minor edits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aging erhöht, damit histo aufgerufen wird. histo layer matrix position verändert, damit die stimmt histo.image daten sind momentan immer NULL -> error bei Aufruf --- .../DestructionPower/DestructionPower.cpp | 4 ++++ .../LayerHistogram/AbstractionLayer_Histogram.cpp | 10 ++++------ Source/functions/solve/structure.cpp | 6 +----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index f74d459..eeea3a8 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -40,6 +40,7 @@ bool DestructionPower::RemoveConstraintOnPosition(const coor constraintCoordinat void DestructionPower::DestructionOfSurrounding(const coor constraintCoordinate) { for (int i = 0; i < DESTRUCTION_COUNT; ++i) { + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray.push_back(0); int divisor=0; if(constraintCoordinate.row > 0) @@ -57,6 +58,9 @@ void DestructionPower::DestructionOfSurrounding(const coor constraintCoordinate) else //create default destructionPower //TODO find some better solution for default m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i] = m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SpeedTable[i]; + //aging + if(m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i]<0.9) + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i]=m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray[i]*(float)1.001+(float)0.01; } } diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp index 380ef48..d1177c9 100644 --- a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp +++ b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp @@ -105,23 +105,21 @@ bool AbstractionLayer_Histogram::PlaceOfPartGood(coor myCoor, Mat& myPart) HistogramComparer localComparer; //sets coordinates to correct position for layer - myCoor.row++; - myCoor.col++; - if( myCoor.row == 1 && myCoor.col == 1){return true;} - else if(myCoor.col == 1 && myCoor.row >1){ + if( myCoor.row == 0 && myCoor.col == 0){return true;} + else if(myCoor.col == 0 && myCoor.row >0){ if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart)){ return true; } else return false; } - else if( myCoor.row == 1 && myCoor.col >1){ + else if( myCoor.row == 0 && myCoor.col >0){ if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ return true; } else return false; } - else if (myCoor.col > 1 && myCoor.row >1){ + else if (myCoor.col > 0 && myCoor.row >0){ if( localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart) && localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ return true; diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 3beec78..240e204 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -35,8 +35,6 @@ bool next(vector& log,Puzzle& puzzleMat) setsolution(log,puzzleMat); else setsolution(log,puzzleMat); - if(log.back().myCoor.row==26) - cout << log.back().myCoor.row << ", " << log.back().myCoor.col << endl; return true; } @@ -81,7 +79,7 @@ void solve(vector& log,Puzzle& puzzleMat) case 0://pömpel puzzleMat.a1.EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; - case 1:// + case 1://histogram puzzleMat.a3.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; case -1://random @@ -111,8 +109,6 @@ void setsolution(vector& log, Puzzle& puzzleMat) puzzleMat.combinedQualityVector.clear(); //clear data from temp variable //tell log entry that it is set log.back().Set(); - if(log.back().myCoor.row==27) - cout << "hello" << endl; puzzleMat.setConstraints(log.back().myCoor,log.back().PieceCollector.begin()->second); //cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; } From 7a3b33455d64c98926612c5421d087ebe53a2417 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Sat, 20 Jan 2018 09:40:03 +0100 Subject: [PATCH 5/7] Changed dp calculation --- Source/CMakeLists.txt | 1 - .../DestructionPower/DestructionPower.cpp | 6 +- .../DestructionPower/DestructionPower.h | 6 +- .../DestructionPower_Properties.h | 3 - .../Layer1/AbstractionLayer_1.cpp | 20 +++ .../Layer1/AbstractionLayer_1.h | 2 + .../AbstractionLayer_Histogram.cpp | 158 ------------------ .../AbstractionLayer_Histogram.h | 40 ----- .../AbstractionLayer_Histogram_Properties.h | 29 ---- Source/functions/solve/puzzleExtension.cpp | 14 +- Source/functions/solve/structure.cpp | 30 +++- Source/header/input.h | 2 - Source/header/solve.h | 5 +- 13 files changed, 57 insertions(+), 259 deletions(-) delete mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp delete mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h delete mode 100644 Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 658b4fd..26ef6a8 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -10,7 +10,6 @@ set(SOURCE_FILES functions/solve/structure.cpp functions/AbstractionLayers/AbstraktionLayer_Base.h functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp - functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp functions/AbstractionLayers/DestructionPower/DestructionPower.cpp header/solve.h header/input.h diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index eeea3a8..6c76477 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -1,7 +1,3 @@ -// -// Created by mpapa on 05.12.2017. -// - #include "DestructionPower.h" //TODO! Add more layers here! @@ -39,6 +35,8 @@ bool DestructionPower::RemoveConstraintOnPosition(const coor constraintCoordinat //gets destruction power from left and from top if possible and normalizes void DestructionPower::DestructionOfSurrounding(const coor constraintCoordinate) { + for(int i = 0; i < m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray.size(); ++i) + m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray.pop_back(); for (int i = 0; i < DESTRUCTION_COUNT; ++i) { m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].DestructionArray.push_back(0); diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h index 4f8b506..8d8eb9f 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.h @@ -1,10 +1,6 @@ -// -// Created by mpapa on 05.12.2017. -// - #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/DestructionPower/DestructionPower_Properties.h b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h index d2f294c..33b2d4c 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h @@ -1,6 +1,3 @@ -// -// Created by mpapa on 05.12.2017. -// #pragma once #define DESTRUCTION_INIT 0.5 diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index 286cc93..65976ea 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -46,6 +46,8 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr //it through qualityVector and removes all that do not trigger PlaceOfPartGood bool AbstractionLayer_1::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector) { + if(constraintCoordinate.row==23 && constraintCoordinate.col==35) + cout << "in" << endl; //evaluateQuality = evaluateProbabilaty for(int i = 0;im_a1.m_connections==constraints) + qVector.erase(qVector.begin()+i); + else + i++; + + } + +} + void AbstractionLayer_1::CreateRandomPuzzle() { std::minstd_rand simple_rand; @@ -182,7 +198,11 @@ bool AbstractionLayer_1::PlaceOfPartGood(coor myCoor, uint8_t& myPart) || ((((negativePart & 0b00000011) == 0b00000011) || ((myPart & 0b00000011) == 0b00000011)) && (((myPart & 0b00000011) != 0b00000000) && (negativePart & 0b00000011) != 0b00000000)) || (((negativePart & 0b00000011) == 0b00000000) && ((myPart & 0b00000011) == 0b00000000)) ) ) + { + if(myCoor.row==18 && myCoor.col==35) + cout << "gud: " << std::bitset<8>(myPart) << endl; return true; + } return false; } diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h index 7889a14..15a49b8 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h @@ -49,6 +49,8 @@ public: bool EvaluateQuality ( coor constraintCoordinate, qualityVector& qVector)override; bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_1_Properties constraint)override; bool RemoveConstraintOnPosition( coor constraintCoordinate)override; + int RemoveSimilar(qualityVector&,uint8_t&); + bool PlaceOfPartGood(coor myCoor, uint8_t& myPart); void shift(uint8_t& Part, int shifts); void setEdgeZero(); diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp deleted file mode 100644 index d1177c9..0000000 --- a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.cpp +++ /dev/null @@ -1,158 +0,0 @@ -// -// 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); - 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; - analyseParts 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 = localImage.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_Histogram.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_Histogram.image)) - { - qVector[i].first=1; - - continue; - } - qVector[i].first=0; - } - - -} -bool AbstractionLayer_Histogram::PlaceOfPartGood(coor myCoor, Mat& myPart) -{ - - HistogramComparer localComparer; - //sets coordinates to correct position for layer - - if( myCoor.row == 0 && myCoor.col == 0){return true;} - else if(myCoor.col == 0 && myCoor.row >0){ - if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart)){ - return true; - } - else return false; - } - else if( myCoor.row == 0 && myCoor.col >0){ - if(localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ - return true; - } - else return false; - } - else if (myCoor.col > 0 && myCoor.row >0){ - if( localComparer.CompareHistogram(m_constraintMatrix[myCoor.col][myCoor.row-1].image, myPart) && - localComparer.CompareHistogram(m_constraintMatrix[myCoor.col-1][myCoor.row].image, myPart)){ - return true; - } - else return false; - }else return false; - - -} - -bool HistogramComparer::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; -} diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h deleted file mode 100644 index 5fb2a82..0000000 --- a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h +++ /dev/null @@ -1,40 +0,0 @@ -// -// 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(); - -private: -}; - -class HistogramComparer{ -public: - Mat readImages(int); - bool CompareHistogram(Mat Part, Mat RefPart); -private: - -}; - -#endif //MPK_PUZZLE_ABSTRACTIONLAYER_HISTOGRAM_H - diff --git a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h b/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h deleted file mode 100644 index bb1270a..0000000 --- a/Source/functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h +++ /dev/null @@ -1,29 +0,0 @@ -// -// 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 40bb3a5..7037108 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -66,7 +66,6 @@ void Puzzle::shuffle() void Puzzle::removeConstrains(coor removeCoordinates) { this->a1.RemoveConstraintOnPosition(removeCoordinates); - this->a3.RemoveConstraintOnPosition(removeCoordinates); //TODO!! Add other layer remove here } void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) @@ -78,12 +77,17 @@ void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) //a1 this->a1.SetConstraintOnPosition(setConstraints,constraintPiece->m_a1); - //a3 - this->a3.SetConstraintOnPosition(setConstraints,constraintPiece->m_Histogram); //TODO!! Add other layer remove here } +int Puzzle::removeSimilar(qualityVector& qVector, Part& myPart) +{ + //a1 + uint8_t tmpConnections=myPart.m_a1.getConnections(); + a1.RemoveSimilar(qVector,tmpConnections); +} + void Puzzle::createRandomPuzzle() { a1.CreateRandomPuzzle(); @@ -156,8 +160,6 @@ Mat Puzzle::resultImage( vector& log){ imageW = imageH; imageH = temp; - cout<<"imageW "<& log){ // imshow("result",result); // waitKey(0); } - cout << log.size() << endl; - cout << log[0].PieceCollector.size() << endl; cout << it.PieceCollector[0].second->GetPartID() << endl; diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 240e204..27bed9e 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -44,11 +44,15 @@ void createNextLogElement(vector& log, Puzzle& puzzleMat) log.emplace_back(LogEntry(coor(0, 0))); log.back().myCoor = calculateNextCoor(log, puzzleMat); puzzleMat.dp.DestructionOfSurrounding(log.back().myCoor);//calculate dp from surrounding + cout << "-----------------------" << endl; + cout << "destr-array:" << endl; + for(auto it:puzzleMat.dp.m_constraintMatrix[log.back().myCoor.col][log.back().myCoor.row].DestructionArray) + cout << it << endl; //get all not set pieces for(auto it:puzzleMat.p_myBox) if(!it->set) log.back().PieceCollector.emplace_back(pair(0,it)); - solve(log,puzzleMat); + solve(log,puzzleMat); } @@ -72,7 +76,8 @@ coor calculateNextCoor(vector& log, Puzzle& puzzleMat) void solve(vector& log,Puzzle& puzzleMat) { log.back().abstractionLevel = puzzleMat.dp.getNextAbstractionLayer(log.back().myCoor,log.back().abstractionLevel); //sets in abstractionLevel - //status(log,p_Box,puzzleMat); + cout << "ab: " << log.back().abstractionLevel << endl; + //status(log,p_Box,puzzleMat); //TODO!! Add more layers here switch(log.back().abstractionLevel) { @@ -80,7 +85,7 @@ void solve(vector& log,Puzzle& puzzleMat) puzzleMat.a1.EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; case 1://histogram - puzzleMat.a3.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); + return; break; case -1://random setsolution(log,puzzleMat); @@ -88,7 +93,6 @@ void solve(vector& log,Puzzle& puzzleMat) default: break; } - float worth = capLogElements(log); calculateTrueDestructionPower(log,puzzleMat, worth); CalculateNewCombinedQuality(log, log.back().PieceCollector, puzzleMat.combinedQualityVector); @@ -110,11 +114,16 @@ void setsolution(vector& log, Puzzle& puzzleMat) //tell log entry that it is set log.back().Set(); puzzleMat.setConstraints(log.back().myCoor,log.back().PieceCollector.begin()->second); - //cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; + cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; + cout << "ID: " << log.back().PieceCollector[0].second->GetPartID() << endl; + cout << "log:" << endl; + for(auto it:log.back().PieceCollector) + cout << std::bitset<8>(it.second->m_a1.getConnections()) << "|" << it.second->GetPartID() << endl; } bool backtrack(vector& log, Puzzle& puzzleMat) { + cout << "backtracking" ; if(log.empty()) { cout << "Puzzle not solveable!" << endl; @@ -124,10 +133,16 @@ bool backtrack(vector& log, Puzzle& puzzleMat) //if more pieces possible, tset piece as not logged if((log.back().PieceCollector.size())>1) { + cout << " next piece" << endl; for(int i=0;iGetPartID()==log.back().PieceCollector.begin()->second->GetPartID())//sets all with partid puzzleMat.p_myBox[i]->set=false; - log.back().PieceCollector.erase(log.back().PieceCollector.begin()); + + Part myPart = *log.back().PieceCollector[0].second;//tmpsaves bad part + log.back().PieceCollector.erase(log.back().PieceCollector.begin());//removes bad part from log + puzzleMat.removeSimilar(log.back().PieceCollector,myPart); //removes all pieces from log that are similar to bad part + //TODO remove this when further layers are added!!! + if(log.back().PieceCollector.size()==1) log.back().decreaseRandomed(); @@ -139,6 +154,7 @@ bool backtrack(vector& log, Puzzle& puzzleMat) //else remove log element and backtrack once more else { + cout << " no more pieces" << endl; puzzleMat.removeConstrains(log.back().myCoor); //this should remove constraints from all layers if((log.back().PieceCollector.size())) for(int i=0;i& log, Puzzle& puzzleMat) //this is addon stuff that should later all be extracted into a sererate cpp as it is not core dispatcher functionality - void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, float Layerworth) { float destructionPower = sqrt( Layerworth * puzzleMat.dp.m_constraintMatrix[0][0].SpeedTable[log.back().abstractionLevel]); @@ -170,6 +185,7 @@ void calculateTrueDestructionPower(vector& log, Puzzle& puzzleMat, flo // PART RAUER_WEIDINGER float capLogElements(vector& log) { + // Till Now only ground structure -> incorrect variable ans vector names double limit = 0.6; double diff = 0; diff --git a/Source/header/input.h b/Source/header/input.h index 8fd6181..6cee3da 100755 --- a/Source/header/input.h +++ b/Source/header/input.h @@ -8,7 +8,6 @@ #include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1_Properties.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower_Properties.h" -#include "../functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram_Properties.h" class LayerContainer; @@ -43,7 +42,6 @@ public: bool set; AbstractionLayer_1_Properties m_a1; - AbstractionLayer_Histogram_Properties m_Histogram; private: int32_t m_partID; uint8_t m_numOfRotations; diff --git a/Source/header/solve.h b/Source/header/solve.h index a27bef6..c0a48ca 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -9,7 +9,6 @@ #include "../functions/AbstractionLayers/Layer1/AbstractionLayer_1.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h" -#include "../functions/AbstractionLayers/LayerHistogram/AbstractionLayer_Histogram.h" using namespace std; @@ -47,7 +46,6 @@ public: createBox(); createp_box(); dp.PreProcessing({cols,rows}, nullptr); a1.PreProcessing({cols,rows}, &p_myBox); - a3.PreProcessing({cols,rows},&p_myBox); return true; } @@ -55,10 +53,11 @@ public: DestructionPower dp; AbstractionLayer_1 a1; - AbstractionLayer_Histogram a3; void removeConstrains(coor removeCoordinates); void setConstraints(coor setConstraints, Part *constraintPiece); + int removeSimilar(qualityVector&, Part&); + void printPuzzle(); void printBox(); Mat resultImage(vector&); From 9863c8b9e8c89a5af5b7f65b1ebc92ec56c110d4 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Sat, 20 Jan 2018 11:58:32 +0100 Subject: [PATCH 6/7] created Preprocessing check if true deactivate in header.h --- .../Layer1/AbstractionLayer_1.cpp | 41 +++++++++++++++---- .../Layer1/AbstractionLayer_1.h | 1 + Source/functions/solve/puzzleExtension.cpp | 3 -- Source/header.h | 1 + Source/header/solve.h | 5 ++- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index 65976ea..e4c0100 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -1,7 +1,3 @@ -// -// Created by mpapa on 05.12.2017. -// - #include "AbstractionLayer_1.h" #include "../../../header.h" #include @@ -13,6 +9,7 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr const vector& ref_partArray = *partArray; analyseParts analyse(mySize.row*mySize.col); Part buf; + int PSum=0; int iterator=0; if(!analyse.getImages()) { @@ -23,7 +20,8 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr //TODO rows and cols for(int i = 0; i < mySize.row*mySize.col; i++) { - unsigned char poempel = analyse.getTabs(i);; + unsigned char poempel = analyse.getTabs(i); + PSum+=PoempelSum(poempel); //preprocess correct check for (int j=0;j<4;j++) { ref_partArray[iterator]->m_a1.m_connections=poempel; @@ -32,10 +30,12 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr } } + if(PREPRO_CHECK && PSum) + return false; + + //Zugriff auf den vector mit den einzelnen teilen: part[0].getConnenctions() entspricht pömpel von bild 0.jpg und liefert ein unsigned char, poempl Belegung wie ausgemacht - - InitialiseConstraintMatrixSize(mySize.col+2, mySize.row+2); //col row switched in this function setEdgeZero(); @@ -43,6 +43,33 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector* partAr return true; } +int AbstractionLayer_1::PoempelSum(uint8_t constraint) +{ + int PoempelSum=0; + if((constraint & 0b11000000)==0b01000000) + PoempelSum--; + else if((constraint & 0b11000000)==0b10000000) + PoempelSum++; + + if((constraint & 0b00110000)==0b00010000) + PoempelSum--; + else if((constraint & 0b00110000)==0b00100000) + PoempelSum++; + + if((constraint & 0b00001100)==0b00000100) + PoempelSum--; + else if((constraint & 0b00001100)==0b00001000) + PoempelSum++; + + if((constraint & 0b00000011)==0b00000001) + PoempelSum--; + else if((constraint & 0b00000011)==0b00000010) + PoempelSum++; + + return PoempelSum; + +} + //it through qualityVector and removes all that do not trigger PlaceOfPartGood bool AbstractionLayer_1::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector) { diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h index 15a49b8..42189c8 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h @@ -55,6 +55,7 @@ public: void shift(uint8_t& Part, int shifts); void setEdgeZero(); + int PoempelSum(uint8_t constraint); void CreateRandomPuzzle(); qualityVector returnInBox(vector& PuzzleBox); diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 7037108..82a9c64 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -1,6 +1,3 @@ -// -// Created by Raphael Maenle on 21/12/2017. -// #include "../../header/solve.h" #include "../../header/input.h" diff --git a/Source/header.h b/Source/header.h index 533aff6..bcf9329 100755 --- a/Source/header.h +++ b/Source/header.h @@ -7,6 +7,7 @@ #include #include +#define PREPRO_CHECK false using namespace std; #include "header/input.h" diff --git a/Source/header/solve.h b/Source/header/solve.h index c0a48ca..08485f3 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -44,8 +44,9 @@ public: bool PreProcessing() { createBox(); createp_box(); - dp.PreProcessing({cols,rows}, nullptr); - a1.PreProcessing({cols,rows}, &p_myBox); + if(!dp.PreProcessing({cols,rows}, nullptr)) return false; + if(!a1.PreProcessing({cols,rows}, &p_myBox)) return false; + return true; } From fe04ebcd0137312394fdd7b734d70cce98eb6758 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Sat, 20 Jan 2018 15:05:54 +0100 Subject: [PATCH 7/7] removed bloat --- .../AbstractionLayer_MeanDifference.h | 35 ------------------- ...stractionLayer_MeanDifference_Properties.h | 20 ----------- Source/functions/solve/structure.cpp | 6 +--- 3 files changed, 1 insertion(+), 60 deletions(-) delete mode 100644 Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h delete mode 100644 Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h diff --git a/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h deleted file mode 100644 index b3556c2..0000000 --- a/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by Niko on 1/15/2018. -// - -#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H -#define MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H -#define DISPLAY false -#define PATH "..\\..\\..\\pieces\\%04d.jpg" - -using namespace std; -using namespace cv; - -class AbstractionLayer_MeanDifference : public AbstractionLayer_Base -{ -public: - bool PreProcessing(coor mySize, const vector* partArray) override ; - bool EvaluateQuality ( coor constraintCoordinate, qualityVector& qVector)override; - bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_1_Properties constraint)override; - bool RemoveConstraintOnPosition( coor constraintCoordinate)override; - bool PlaceOfPartGood(coor myCoor, Mat& myPart); - - qualityVector returnInBox(vector& PuzzleBox); - void printConstraintMatrix(); - -private: -}; - -class cMeanDifference{ -public: - Mat readImages(int); - bool calculateMeanDifference(Mat Part, Mat RefPart); -private: - -}; -#endif //MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_H diff --git a/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h b/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h deleted file mode 100644 index 2a1cdd9..0000000 --- a/Source/functions/AbstractionLayers/LayerMeanDifference/AbstractionLayer_MeanDifference_Properties.h +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by Niko on 1/15/2018. -// - -#ifndef MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H -#define MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H - -class AbstractionLayer_MeanDifference_Properties -{ -public: - AbstractionLayer_MeanDifference_Properties() : MeanDifference(-1){} - double getMeanDifference(){return MeanDifference;}; - -private: - - double MeanDifference; - friend class AbstractionLayer_MeanDifference; - Mat image; -}; -#endif //MPK_PUZZLE_ABSTRACTIONLAYER_MEANDIFFERENCE_PROPERTIES_H diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index 27bed9e..8ec6b68 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -45,10 +45,7 @@ void createNextLogElement(vector& log, Puzzle& puzzleMat) log.back().myCoor = calculateNextCoor(log, puzzleMat); puzzleMat.dp.DestructionOfSurrounding(log.back().myCoor);//calculate dp from surrounding cout << "-----------------------" << endl; - cout << "destr-array:" << endl; - for(auto it:puzzleMat.dp.m_constraintMatrix[log.back().myCoor.col][log.back().myCoor.row].DestructionArray) - cout << it << endl; - //get all not set pieces + //get all not set pieces for(auto it:puzzleMat.p_myBox) if(!it->set) log.back().PieceCollector.emplace_back(pair(0,it)); @@ -76,7 +73,6 @@ coor calculateNextCoor(vector& log, Puzzle& puzzleMat) void solve(vector& log,Puzzle& puzzleMat) { log.back().abstractionLevel = puzzleMat.dp.getNextAbstractionLayer(log.back().myCoor,log.back().abstractionLevel); //sets in abstractionLevel - cout << "ab: " << log.back().abstractionLevel << endl; //status(log,p_Box,puzzleMat); //TODO!! Add more layers here switch(log.back().abstractionLevel)