added Histogramm Layer

multiple errors
This commit is contained in:
Raphael Maenle 2018-01-30 18:16:26 +01:00
parent 564053797a
commit 6f02bf4f97
9 changed files with 277 additions and 17 deletions

View File

@ -14,6 +14,7 @@ set(SOURCE_FILES
functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.cpp functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.cpp
functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.cpp functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.cpp
functions/AbstractionLayers/DestructionPower/DestructionPower.cpp functions/AbstractionLayers/DestructionPower/DestructionPower.cpp
functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.cpp
header/solve.h header/solve.h
header/input.h header/input.h
functions/solve/puzzleExtension.cpp) functions/solve/puzzleExtension.cpp)

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
//TODO!! increase Destructioncount //TODO!! increase Destructioncount
#define DESTRUCTION_COUNT 2 #define DESTRUCTION_COUNT 1
#include "DestructionPower_Properties.h" #include "DestructionPower_Properties.h"
#include "../AbstraktionLayer_Base.h" #include "../AbstraktionLayer_Base.h"

View File

@ -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<Part*>* partArray){
// HistogramComparer localImage;
cout << "Abstraction 2 Preprocessing... " << flush;
const vector<Part*>& 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;
}
}

View File

@ -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<AbstractionLayer_Histogram_Properties>
{
public:
bool PreProcessing(coor mySize, const vector<Part*>* 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<Part>& 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

View File

@ -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 <stdint.h>
#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

View File

@ -70,6 +70,7 @@ void Puzzle::removeConstrains(coor removeCoordinates)
this->a3.RemoveConstraintOnPosition(removeCoordinates); this->a3.RemoveConstraintOnPosition(removeCoordinates);
this->a4.RemoveConstraintOnPosition(removeCoordinates); this->a4.RemoveConstraintOnPosition(removeCoordinates);
this->acm.RemoveConstraintOnPosition(removeCoordinates); this->acm.RemoveConstraintOnPosition(removeCoordinates);
this->his.RemoveConstraintOnPosition(removeCoordinates);
//TODO!! Add other layer remove here //TODO!! Add other layer remove here
} }
void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece) void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece)
@ -90,6 +91,9 @@ void Puzzle::setConstraints(coor setConstraints, Part* constraintPiece)
//a2 //a2
this->acm.SetConstraintOnPosition(setConstraints,constraintPiece->m_acm); this->acm.SetConstraintOnPosition(setConstraints,constraintPiece->m_acm);
//his
this->his.SetConstraintOnPosition(setConstraints,constraintPiece->m_his);
//TODO!! Add other layer remove here //TODO!! Add other layer remove here
} }

View File

@ -58,27 +58,27 @@ coor calculateNextCoor(vector<LogEntry>& log, Puzzle& puzzleMat)
{ {
if (log.size() == 1) if (log.size() == 1)
return {0,0}; // return {0,0};
return {1,1};
unsigned int col= log.rbegin()[1].myCoor.col; unsigned int col= log.rbegin()[1].myCoor.col;
unsigned int row= log.rbegin()[1].myCoor.row; unsigned int row= log.rbegin()[1].myCoor.row;
//level 2 edges first //level 2 edges first
if(col == 0 && row < 27) // if(col == 0 && row < 27)
return {col,++row}; // return {col,++row};
if(row== 27 && col < 35) // if(row== 27 && col < 35)
return {++col,row}; // return {++col,row};
if(col == 35 && row >0) // if(col == 35 && row >0)
return {col, --row}; // return {col, --row};
if(col >1&& row ==0) // if(col >1&& row ==0)
return{--col,row}; // return{--col,row};
if(col==1 && row==0) // if(col==1 && row==0)
return {1,1}; // return {1,1};
log.pop_back();//vis only!!! // log.pop_back();//vis only!!!
puzzleMat.resultImage(log);//vis only!!! // puzzleMat.resultImage(log);//vis only!!!
if(row<puzzleMat.getSizeAsCoor().row-2) row++; if(row<puzzleMat.getSizeAsCoor().row-2) row++;
@ -104,7 +104,7 @@ void solve(vector<LogEntry>& log,Puzzle& puzzleMat)
//TODO!! Add more layers here //TODO!! Add more layers here
switch(log.back().abstractionLevel) switch(log.back().abstractionLevel)
{ {
case 0://pömpel case 10://pömpel
puzzleMat.a1.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); puzzleMat.a1.EvaluateQuality(log.back().myCoor,log.back().PieceCollector);
break; break;
case 2://SURFFeature case 2://SURFFeature
@ -117,6 +117,9 @@ void solve(vector<LogEntry>& log,Puzzle& puzzleMat)
case 1://color case 1://color
puzzleMat.acm.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); puzzleMat.acm.EvaluateQuality(log.back().myCoor,log.back().PieceCollector);
break; break;
case 0://histogram
puzzleMat.his.EvaluateQuality(log.back().myCoor,log.back().PieceCollector);
break;
case -1://random case -1://random
setsolution(log,puzzleMat); setsolution(log,puzzleMat);
return; return;

View File

@ -10,7 +10,7 @@
#include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h" #include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition_Properties.h"
#include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching_Properties.h" #include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching_Properties.h"
#include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures_Properties.h" #include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures_Properties.h"
#include "../functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram_Properties.h"
class LayerContainer; class LayerContainer;
class Part class Part
@ -47,6 +47,7 @@ public:
AbstractionLayer_PoempelPosition_Properties m_a3; AbstractionLayer_PoempelPosition_Properties m_a3;
AbstractionLayer_SURFFeatures_Properties m_a4; AbstractionLayer_SURFFeatures_Properties m_a4;
AbstractionLayer_ColorMatching_Properties m_acm; AbstractionLayer_ColorMatching_Properties m_acm;
AbstractionLayer_Histogram_Properties m_his;
private: private:
int32_t m_partID; int32_t m_partID;
uint8_t m_numOfRotations; uint8_t m_numOfRotations;

View File

@ -9,6 +9,7 @@
#include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h" #include "../functions/AbstractionLayers/Layer3_PoempelPosition/AbstractionLayer_PoempelPosition.h"
#include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.h" #include "../functions/AbstractionLayers/Layer_SURFFeatures/AbstractionLayer_SURFFeatures.h"
#include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.h" #include "../functions/AbstractionLayers/Layer_ColorMatching/AbstractionLayer_ColorMatching.h"
#include "../functions/AbstractionLayers/Layer_Histogram/AbstractionLayer_Histogram.h"
#include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h" #include "../functions/AbstractionLayers/DestructionPower/DestructionPower.h"
using namespace std; using namespace std;
@ -56,6 +57,7 @@ public:
if(!a3.PreProcessing({cols,rows}, &p_myBox)) return false; if(!a3.PreProcessing({cols,rows}, &p_myBox)) return false;
if(!a4.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(!acm.PreProcessing({cols,rows}, &p_myBox)) return false;
if(!his.PreProcessing({cols,rows}, &p_myBox)) return false;
return true; return true;
} }
@ -67,6 +69,7 @@ public:
AbstractionLayer_PoempelPosition a3; AbstractionLayer_PoempelPosition a3;
AbstractionLayer_SURFFeatures a4; AbstractionLayer_SURFFeatures a4;
AbstractionLayer_ColorMatching acm; AbstractionLayer_ColorMatching acm;
AbstractionLayer_Histogram his;
void removeConstrains(coor removeCoordinates); void removeConstrains(coor removeCoordinates);
void setConstraints(coor setConstraints, Part *constraintPiece); void setConstraints(coor setConstraints, Part *constraintPiece);