Added KantenLaenge

This commit is contained in:
Raphael Maenle
2018-01-30 18:45:54 +01:00
parent 6f02bf4f97
commit dd69066c76
14 changed files with 179 additions and 280 deletions

View File

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

View File

@ -27,8 +27,10 @@ bool AbstractionLayer_1::PreProcessing(coor mySize, const vector<Part*>* partAr
{
ref_partArray[iterator]->m_a1.m_connections=poempel;
ref_partArray[iterator]->m_a3.SideLength=analyse.getPoempelPosition(i); //do abstraction Layer 3 check
ref_partArray[iterator]->m_a2.SideLength=analyse.getKantenLaenge(i);//do abstraction Layer 2 check
shift(poempel,1);
ref_partArray[iterator]->m_a3.shift(j);//rotate information
ref_partArray[iterator]->m_a2.shift(j);//rotate information
iterator++;
}
}
@ -432,7 +434,9 @@ bool analyseParts::getImages(){
}
mask.setCorners(corners);
vector<double> tmpPoempelPosition={0,0,0,0,0,0,0,0};//TODO somehow make this code section bearable
mask.setTabs(analyseContour(corners,contours[0],tmpPoempelPosition));
vector<double> tmpKantenLaenge= {0,0,0,0,0,0,0,0};
mask.setTabs(analyseContour(corners,contours[0],tmpPoempelPosition,tmpKantenLaenge));
mask.setKantenLaenge(tmpKantenLaenge);
mask.setPoempelPosition(tmpPoempelPosition);
mask.setLens(analyseLens(lens, corners));
mask.setMidpoint(calcMidpoint(corners));
@ -638,7 +642,7 @@ vector<Point> analyseParts::findCorners(vector<Point> contour, Point center){
if(DISPLAY) imshow("draw",drawing);
return corners;
}
unsigned char analyseParts::analyseContour(vector<Point> corners, vector<Point> contour,vector<double>& PoempelPosition){
unsigned char analyseParts::analyseContour(vector<Point> corners, vector<Point> contour,vector<double>& PoempelPosition, vector<double>& KantenLaenge){
vector<Point> contour_right;
vector<Point> contour_top;
@ -1078,6 +1082,7 @@ unsigned char analyseParts::analyseContour(vector<Point> corners, vector<Point>
diff = corners[0] - contour_right_new[max_dist_idx];
PoempelPosition[3] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);
/*-------------------------------------*/
unsigned char tabs = 0;
int poembel_threshold = 15;
@ -1160,7 +1165,7 @@ unsigned char analyseParts::analyseContour(vector<Point> corners, vector<Point>
max_dist_idx = i;
}
}
//saves length between Corners and Poempel left
//saves length between Corners and Poempel bottom
diff = corners[0] - contour_bottom_new[max_dist_idx];
PoempelPosition[4] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);
diff = corners[1] - contour_bottom_new[max_dist_idx];
@ -1175,6 +1180,15 @@ unsigned char analyseParts::analyseContour(vector<Point> corners, vector<Point>
if (abs(ref_bottom - contour_bottom_new[max_dist_idx].y) < poembel_threshold) {
tabs |= (0 << BOTTOM);
}
diff=corners[2] - corners[0];
KantenLaenge[2] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);//right
diff=corners[1] - corners[3];
KantenLaenge[6] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);//left
diff=corners[0] - corners[1];
KantenLaenge[4] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);
diff=corners[3] - corners[2];
KantenLaenge[0] = cv::sqrt(diff.x*diff.x + diff.y*diff.y);
return tabs;
}

View File

@ -79,12 +79,15 @@ public:
Point getMidpoint(){return midpoint;}
void setMidpoint(Point m ){midpoint = m;}
vector<double> getPoempelPosition(){return PoempelPosition;}
vector<double> getKantenLaenge(){return KantenLaenge;}
void setPoempelPosition(vector<double>& newPP){PoempelPosition=newPP;}
void setKantenLaenge(vector<double>& newKL){KantenLaenge=newKL;}
vector<Point> getCorners(){return corners;}
private:
Mat image;
vector<double> PoempelPosition;
vector<double> KantenLaenge;
vector<Point> corners;
vector<vector<Point>> contour;
vector<Vec4i> hierarchy;
@ -104,12 +107,13 @@ public:
unsigned char getTabs(int i){if(i>= nr_parts)return masks[nr_parts-1].getTabs(); else return masks[i].getTabs();}
vector<double> getLen(int i ){return masks[i].getLen();}
vector<double> getPoempelPosition(int i){return masks[i].getPoempelPosition();}
vector<double> getKantenLaenge(int i){return masks[i].getKantenLaenge();}
vector<double> analyseLens(vector<double>, vector<Point>);
Point calcMidpoint(vector<Point>);
Point getMidpoint(int i){return masks[i].getMidpoint();}
Point findCenter(Mat);
vector<Point> findCorners(vector<Point>,Point);
unsigned char analyseContour(vector<Point>, vector<Point>,vector<double>&);
unsigned char analyseContour(vector<Point>, vector<Point>,vector<double>&,vector<double>&);
Mat makeBorder(Mat&);
Mat readImages(int);
Mat morphDilateErode(Mat&);

View File

@ -0,0 +1,90 @@
#include "AbstractionLayer_KantenLaenge.h"
#include "../../../header.h"
bool AbstractionLayer_KantenLaenge::PreProcessing(coor mySize, const vector<Part*>* partArray)
{
//get layer information here
cout << "Abstraction Poempel Position Preprocessing... " << flush;
InitialiseConstraintMatrixSize(mySize.col, mySize.row);
cout << "Done" << endl;
return true;
}
//it through qualityVector and removes all that do not trigger PlaceOfPartGood
bool AbstractionLayer_KantenLaenge::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector)
{
for(int i = 0;i<qVector.size();i++)
{
float value = PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_a2.SideLength);
qVector[i].first=value;
}
}
bool AbstractionLayer_KantenLaenge::SetConstraintOnPosition(const coor constraintCoordinate, const AbstractionLayer_KantenLaenge_Properties constraint)
{
m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SideLength=constraint.SideLength;
}
bool AbstractionLayer_KantenLaenge::RemoveConstraintOnPosition(const coor constraintCoordinate)
{
m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].SideLength={0,0,0,0,0,0,0,0};
}
float AbstractionLayer_KantenLaenge::PlaceOfPartGood(coor myCoor, vector<double> myPart)
{
vector<double> comparePosition={0,0,0,0,0,0,0,0};
float sum=0;
//create negativePart, watch out for edges
if(myCoor.col>0 && m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[3])//get right data from left
{
comparePosition[6]=m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[3];
comparePosition[7]=m_constraintMatrix[myCoor.col-1][myCoor.row].SideLength[2];
//sum absolute difference
sum+=abs(comparePosition[6]-myPart[6]);
sum+=abs(comparePosition[7]-myPart[7]);
}
if(myCoor.row>0 && m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[5])//get bot data from top
{
comparePosition[0]=m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[5];
comparePosition[1]=m_constraintMatrix[myCoor.col][myCoor.row-1].SideLength[4];
//sum absolute difference
sum+=abs(comparePosition[0]-myPart[0]);
sum+=abs(comparePosition[1]-myPart[1]);
}
if(myCoor.col<m_constraintMatrix.size()-1 && m_constraintMatrix[myCoor.col+1][myCoor.row].SideLength[7])// get left data from right
{
comparePosition[2]=m_constraintMatrix[myCoor.col+1][myCoor.row].SideLength[7];
comparePosition[3]=m_constraintMatrix[myCoor.col+1][myCoor.row].SideLength[6];
//sum absolute difference
sum+=abs(comparePosition[2]-myPart[2]);
sum+=abs(comparePosition[3]-myPart[3]);
}
if(myCoor.row<m_constraintMatrix[0].size()-1 && m_constraintMatrix[myCoor.col][myCoor.row+1].SideLength[1])//get top data from bot
{
comparePosition[4]=m_constraintMatrix[myCoor.col][myCoor.row+1].SideLength[1];
comparePosition[5]=m_constraintMatrix[myCoor.col][myCoor.row+1].SideLength[0];
//sum absolute difference
sum+=abs(comparePosition[4]-myPart[4]);
sum+=abs(comparePosition[5]-myPart[5]);
}
if(sum>500)
return 0;
if(sum==0)
return 1;
sum/=500;
return 1-sum;
//check vector against negative part, use sad
//return of well it fits within threshold
}
//shifts vector i steps to the right (8-2i to the left)
void AbstractionLayer_KantenLaenge_Properties::shift(int i)
{
rotate(SideLength.begin(),SideLength.begin()+(8-2*i),SideLength.end());
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "AbstractionLayer_KantenLaenge_Properties.h"
#include "../AbstraktionLayer_Base.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
class AbstractionLayer_KantenLaenge : public AbstractionLayer_Base<AbstractionLayer_KantenLaenge_Properties>
{
public:
bool PreProcessing(coor mySize, const vector<Part*>* partArray) final ;
bool EvaluateQuality ( coor constraintCoordinate, qualityVector& qVector)final;
bool SetConstraintOnPosition( coor constraintCoordinate, AbstractionLayer_KantenLaenge_Properties constraint)final;
bool RemoveConstraintOnPosition( coor constraintCoordinate)final;
float PlaceOfPartGood(coor myCoor, vector<double> myPart);
private:
};

View File

@ -0,0 +1,19 @@
#pragma once
#include <vector>
using namespace std;
class AbstractionLayer_KantenLaenge_Properties
{
public:
AbstractionLayer_KantenLaenge_Properties():SideLength({0,0,0,0,0,0,0,0}){}
float getSideLength(int i){if (i<8 && i>=0)return SideLength[i]; else return -1;};
void shift(int i);
private:
vector<double> SideLength;
friend class AbstractionLayer_KantenLaenge;
friend class AbstractionLayer_PoempelPosition;
friend class AbstractionLayer_1;
};

View File

@ -1,176 +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);
// 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

@ -1,43 +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<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

@ -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 <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