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] 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;