From 4701720d3aba699fdc88e38c13ac85b596c4f4bc Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Tue, 16 Jan 2018 22:53:05 +0100 Subject: [PATCH 1/9] edited in puzzlecreator --- Source/functions/solve/puzzleExtension.cpp | 74 ++++++++++++++++++++-- Source/functions/solve/structure.cpp | 2 +- Source/header/solve.h | 2 + Source/main.cpp | 3 + 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index e46999c..c7e9a00 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -3,12 +3,13 @@ // #include "../../header/solve.h" - +#include "../../header/input.h" void Puzzle::printPuzzle() { cout << "a1: " << endl; a1.printConstraintMatrix(); + } void Puzzle::printBox() @@ -22,6 +23,7 @@ void Puzzle::printBox() } } + //puts a puzzlepiece back into its box void Puzzle::putIntoBox() { @@ -92,10 +94,7 @@ void Puzzle::createBox(){ temp.SetNumOfRotations(j); myBox.push_back(temp); } - - } - } bool Puzzle::allSet() { @@ -115,3 +114,70 @@ void Puzzle::clearMat() } } } + +Mat Puzzle::readImage(int fileIndex, const char* inputDir){ + + char indexstr[12]; + sprintf(indexstr,"%d.jpg",fileIndex); + char * inputstr = (char *) malloc(1 + strlen(inputDir)+ strlen(indexstr) ); + strcpy(inputstr, inputDir); + strcat(inputstr,indexstr); + //cout<& log){ + + int Y_size = 600; // chose this to fit your monitor! + int separator = 1; + int partHeight = 90; + int partWidth; + auto imageH = int(round(partHeight* cols)); + + if(imageH > Y_size){ + imageH = Y_size; + } + partHeight = int(round(imageH / cols)); + partWidth= partHeight; + int imageW = int(round( partWidth*rows)); + + cout<<"imageW "<GetPartID() << endl; + + int imageNumber = it.PieceCollector[0].second->GetPartID(); + //cout<<"imageIndex: "<< imageNumber << endl; + + Mat img = readImage(imageNumber,PATH); + int angle = it.PieceCollector[0].second->GetNumOfRotations()*90; + Point2f center; + center.x = img.cols/2; + center.y = img.rows/2; + Mat RotMatrix = getRotationMatrix2D(center,angle,1); + warpAffine(img,img,RotMatrix, img.size()); +// imshow("readImg",img); // you can comment with Ctrl + / did you know? :D +// waitKey(0); + + auto ROI_X = int(round(it.myCoor.col*partWidth)); + auto ROI_Y = int(round(it.myCoor.row*partHeight)); +// cout<<"ROI X: "<< ROI_X<& 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; } bool backtrack(vector& log, Puzzle& puzzleMat) diff --git a/Source/header/solve.h b/Source/header/solve.h index 0c1a0e5..7bbc49a 100755 --- a/Source/header/solve.h +++ b/Source/header/solve.h @@ -58,6 +58,7 @@ public: void setConstraints(coor setConstraints, Part *constraintPiece); void printPuzzle(); void printBox(); + Mat resultImage(vector&); void createRandomBox(){ myBox.clear();p_myBox.clear(); @@ -81,6 +82,7 @@ public: private: + Mat readImage(int fileIndex, const char* inputDir); unsigned int cols; unsigned int rows; }; diff --git a/Source/main.cpp b/Source/main.cpp index 7f71bf6..001c09c 100644 --- a/Source/main.cpp +++ b/Source/main.cpp @@ -21,6 +21,9 @@ int main() while(next(log, puzzleMat)); cout << "Done!" << endl; + + cout << log.size() << endl; + puzzleMat.resultImage(log); puzzleMat.printPuzzle(); return 0; } From 835cba33dd80d62c4fe64a87fbd7e861f82f65f9 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Thu, 18 Jan 2018 21:36:38 +0100 Subject: [PATCH 2/9] added differenct mat read --- Source/functions/solve/puzzleExtension.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index c7e9a00..d3ced5c 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -146,6 +146,7 @@ Mat Puzzle::resultImage( vector& log){ cout<<"partW "<& log){ int imageNumber = it.PieceCollector[0].second->GetPartID(); //cout<<"imageIndex: "<< imageNumber << endl; - Mat img = readImage(imageNumber,PATH); + sprintf(name, PATH, imageNumber); + Mat img = imread(name, 1); + int angle = it.PieceCollector[0].second->GetNumOfRotations()*90; Point2f center; center.x = img.cols/2; From 0a2de22e63a9903ab2258c534600c2249d68ac64 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Thu, 18 Jan 2018 21:47:53 +0100 Subject: [PATCH 3/9] cw: added multios --- .../AbstractionLayers/Layer1/AbstractionLayer_1.cpp | 2 +- .../AbstractionLayers/Layer1/AbstractionLayer_1.h | 8 ++++++++ Source/functions/solve/puzzleExtension.cpp | 1 - 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp index b594cff..fe9faef 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.cpp @@ -72,7 +72,7 @@ bool AbstractionLayer_1::RemoveConstraintOnPosition(const coor constraintCoordin void AbstractionLayer_1::CreateRandomPuzzle() { std::minstd_rand simple_rand; - simple_rand.seed((unsigned int)"dumbo"); + simple_rand.seed((unsigned int)time(nullptr)); //dumbo for(int col = 0; col < m_constraintMatrix.size()-2; col++){ for(int row = 0; row < (m_constraintMatrix[col].size() - 2);) diff --git a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h index d079332..7889a14 100644 --- a/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h +++ b/Source/functions/AbstractionLayers/Layer1/AbstractionLayer_1.h @@ -24,7 +24,15 @@ #include #define DISPLAY false + +#ifdef _WIN32 #define PATH "..\\..\\..\\pieces\\%04d.jpg" +#elif defined __unix__ +#define PATH "..//..//..//pieces//%04d.jpg" +#elif defined __APPLE__ + #define PATH "..//..//..//pieces//%04d.jpg" +#endif + #define IMG_SIZE 400 #define TOP 6 #define RIGHT 4 diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index d3ced5c..cf38fcf 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -128,7 +128,6 @@ Mat Puzzle::readImage(int fileIndex, const char* inputDir){ } Mat Puzzle::resultImage( vector& log){ - int Y_size = 600; // chose this to fit your monitor! int separator = 1; int partHeight = 90; From a3e2b2a10b6b24a9680d7b829172e71647acdd96 Mon Sep 17 00:00:00 2001 From: Raphael Maenle <17550607+g-spacewhale@users.noreply.github.com> Date: Thu, 18 Jan 2018 22:14:45 +0100 Subject: [PATCH 4/9] manual debugger action program seems to break debugger. not sure why. --- Source/functions/solve/puzzleExtension.cpp | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index cf38fcf..5fb8514 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -1,7 +1,6 @@ // // Created by Raphael Maenle on 21/12/2017. // - #include "../../header/solve.h" #include "../../header/input.h" @@ -41,7 +40,7 @@ void Puzzle::putIntoBox() for(int rotations=0;rotations<4;rotations++) { tmpPart.m_a1.shift(1); - //TODO! add all other layerswith their rotaionvariance here + //TODO! add all other layers with their rotaionvariance here myBox.emplace_back(tmpPart); } @@ -148,36 +147,39 @@ Mat Puzzle::resultImage( vector& log){ char name[100]; for (auto it:log) { - cout << log.size() << endl; - cout << log[0].PieceCollector.size() << endl; - cout << it.PieceCollector[0].second->GetPartID() << endl; + cout << "row: " << it.myCoor.row << ", col: " << it.myCoor.col << endl; + cout << "ID:" << it.PieceCollector[0].second->GetPartID() << endl; + int imageNumber = it.PieceCollector[0].second->GetPartID(); + //cout<<"imageIndex: "<< imageNumber << endl; - int imageNumber = it.PieceCollector[0].second->GetPartID(); - //cout<<"imageIndex: "<< imageNumber << endl; - - sprintf(name, PATH, imageNumber); - Mat img = imread(name, 1); - - int angle = it.PieceCollector[0].second->GetNumOfRotations()*90; - Point2f center; - center.x = img.cols/2; - center.y = img.rows/2; - Mat RotMatrix = getRotationMatrix2D(center,angle,1); - warpAffine(img,img,RotMatrix, img.size()); -// imshow("readImg",img); // you can comment with Ctrl + / did you know? :D + sprintf(name, PATH, imageNumber); + Mat img = imread(name, 1); + cout << "loc: " << name << endl; + cout << "img size: " << img.size() << endl; + int angle = it.PieceCollector[0].second->GetNumOfRotations()*90; + Point2f center; + center.x = img.cols/2; + center.y = img.rows/2; + Mat RotMatrix = getRotationMatrix2D(center,angle,1); + warpAffine(img,img,RotMatrix, img.size()); + cout << "warped" << endl; +// imshow("readImg",img); // you can comment with Ctrl + / did you know? :D nop, but that's useful, tx // waitKey(0); - auto ROI_X = int(round(it.myCoor.col*partWidth)); - auto ROI_Y = int(round(it.myCoor.row*partHeight)); + auto ROI_X = int(round(it.myCoor.col*partWidth)); + auto ROI_Y = int(round(it.myCoor.row*partHeight)); // cout<<"ROI X: "<< ROI_X< Date: Fri, 19 Jan 2018 15:26:53 +0100 Subject: [PATCH 5/9] First Working Version with Result Image --- Source/functions/solve/puzzleExtension.cpp | 24 ++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index cf38fcf..d3604fa 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -128,7 +128,7 @@ Mat Puzzle::readImage(int fileIndex, const char* inputDir){ } Mat Puzzle::resultImage( vector& log){ - int Y_size = 600; // chose this to fit your monitor! + int Y_size = 1200; // chose this to fit your monitor! int separator = 1; int partHeight = 90; int partWidth; @@ -141,6 +141,10 @@ Mat Puzzle::resultImage( vector& log){ partWidth= partHeight; int imageW = int(round( partWidth*rows)); + int temp = imageW; + imageW = imageH; + imageH = temp; + cout<<"imageW "<& log){ char name[100]; for (auto it:log) { + if (it.myCoor.col == 12 && it.myCoor.row == 0) + { + ; + // imshow("result",result); + // waitKey(0); + } cout << log.size() << endl; cout << log[0].PieceCollector.size() << endl; @@ -159,7 +169,14 @@ Mat Puzzle::resultImage( vector& log){ sprintf(name, PATH, imageNumber); Mat img = imread(name, 1); - int angle = it.PieceCollector[0].second->GetNumOfRotations()*90; + if (it.myCoor.col == 12 && it.myCoor.row == 0) + { + //imshow("img",img); + //waitKey(0); + ; + } + + int angle = ((int)it.PieceCollector[0].second->GetNumOfRotations())*-90; Point2f center; center.x = img.cols/2; center.y = img.rows/2; @@ -180,6 +197,9 @@ Mat Puzzle::resultImage( vector& log){ // imshow("result",result); // waitKey(0); + } + imshow("result",result); + waitKey(0); return result; } From 9b0bacf6d7b8f549bf5eb9bfdfe1d10ac4f2df31 Mon Sep 17 00:00:00 2001 From: g-spacewhale <17550607+g-spacewhale@users.noreply.github.com> Date: Fri, 19 Jan 2018 15:57:48 +0100 Subject: [PATCH 6/9] stash --- Source/functions/solve/puzzleExtension.cpp | 67 ++++++++++++++-------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index c142853..13535b6 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -4,6 +4,9 @@ #include "../../header/solve.h" #include "../../header/input.h" + +Mat crop2ContourInv(const Mat & img); + void Puzzle::printPuzzle() { cout << "a1: " << endl; @@ -125,8 +128,7 @@ Mat Puzzle::readImage(int fileIndex, const char* inputDir){ Mat source = imread(inputstr,1); return source; } - -Mat Puzzle::resultImage( vector& log){ +Mat Puzzle::resultImage_for_merging( vector& log){ int Y_size = 1200; // chose this to fit your monitor! int separator = 1; int partHeight = 90; @@ -154,46 +156,63 @@ Mat Puzzle::resultImage( vector& log){ if (it.myCoor.col == 12 && it.myCoor.row == 0) { ; - // imshow("result",result); - // waitKey(0); + // imshow("result",result); + // waitKey(0); } cout << log.size() << endl; cout << log[0].PieceCollector.size() << endl; - cout << it.PieceCollector[0].second->GetPartID() << endl; + cout << it.PieceCollector[0].second->GetPartID() << endl; - int imageNumber = it.PieceCollector[0].second->GetPartID(); - //cout<<"imageIndex: "<< imageNumber << endl; + int imageNumber = it.PieceCollector[0].second->GetPartID(); + //cout<<"imageIndex: "<< imageNumber << endl; - sprintf(name, PATH, imageNumber); - Mat img = imread(name, 1); + sprintf(name, PATH, imageNumber); + Mat img = imread(name, 1); - if (it.myCoor.col == 12 && it.myCoor.row == 0) - { - //imshow("img",img); - //waitKey(0); - ; - } + copyMakeBorder(img,img,200,200,200,200,BORDER_CONSTANT,Scalar(255,255,255)); + Mat invert = Mat::ones(img.size(), CV_8UC3); // invert for rotation to work correctly + bitwise_not ( img, invert ); + if (it.myCoor.col == 12 && it.myCoor.row == 0) + { + //imshow("img",img); + //waitKey(0); + ; + } - int angle = ((int)it.PieceCollector[0].second->GetNumOfRotations())*-90; - Point2f center; - center.x = img.cols/2; - center.y = img.rows/2; - Mat RotMatrix = getRotationMatrix2D(center,angle,1); - warpAffine(img,img,RotMatrix, img.size()); -// imshow("readImg",img); // you can comment with Ctrl + / did you know? :D -// waitKey(0); + int angle = ((int)it.PieceCollector[0].second->GetNumOfRotations())*-90; + Point2f center; + center.x = img.cols/2; + center.y = img.rows/2; + Mat RotMatrix = getRotationMatrix2D(center,angle,1); + warpAffine(img,img,RotMatrix, img.size()); + bitwise_not(invert,img); + Mat cropped = crop2ContourInv(img); auto ROI_X = int(round(it.myCoor.col*partWidth)); auto ROI_Y = int(round(it.myCoor.row*partHeight)); // cout<<"ROI X: "<< ROI_X< Date: Fri, 19 Jan 2018 15:59:19 +0100 Subject: [PATCH 7/9] stash2 --- Source/functions/solve/puzzleExtension.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 13535b6..d6327dc 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -4,6 +4,8 @@ #include "../../header/solve.h" #include "../../header/input.h" +typedef std::vector> Contour_t; +typedef std::vector Hierarchy_t; //hierarchy object is needed for drawContours apparently Mat crop2ContourInv(const Mat & img); From 6bfa5b1f7ccd338a6e5ef2b94d5b55ccb19ee6cd Mon Sep 17 00:00:00 2001 From: g-spacewhale <17550607+g-spacewhale@users.noreply.github.com> Date: Fri, 19 Jan 2018 16:00:38 +0100 Subject: [PATCH 8/9] stash3 --- Source/functions/solve/puzzleExtension.cpp | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index d6327dc..af6082d 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -8,6 +8,7 @@ typedef std::vector> Contour_t; typedef std::vector Hierarchy_t; //hierarchy object is needed for drawContours apparently Mat crop2ContourInv(const Mat & img); +Contour_t getLongestContour(Mat bw); void Puzzle::printPuzzle() { @@ -218,3 +219,31 @@ Mat crop2ContourInv(const Mat & img){ // for white background images // imshow("cropped", cropped); return cropped; } + +Contour_t getLongestContour(Mat bw){ + + Mat mask = Mat::zeros(bw.size(), CV_8UC1); + Hierarchy_t hierarchy; + Contour_t contours; + findContours(bw, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); + double area = 0; + double largestArea = 0; + size_t largestIndex = 0; + for (size_t i = 0; i < contours.size(); ++i) + { + area = contourArea(contours[i]); + if (area < 1e2 || 1e5 < area) continue; // + if (area > largestArea) { + largestArea = area; + largestIndex = i; // i is type size_t, should be converted to int. + } + } + + Contour_t largestContour; + largestContour.resize(contours.size()); //Otherwise it is just a null pointer and the program crashes! + size_t index = 0; + int strength = 2; // distance between the original contour and the approximated contour + approxPolyDP(Mat(contours[largestIndex]), largestContour[index], strength, true); + return(largestContour); +} + From 229afbc68a5bcb28d04406a645cd06a48cf6e3a2 Mon Sep 17 00:00:00 2001 From: g-spacewhale <17550607+g-spacewhale@users.noreply.github.com> Date: Fri, 19 Jan 2018 16:12:45 +0100 Subject: [PATCH 9/9] added better visualization --- Source/functions/solve/puzzleExtension.cpp | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index af6082d..84de8d3 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -6,9 +6,9 @@ typedef std::vector> Contour_t; typedef std::vector Hierarchy_t; //hierarchy object is needed for drawContours apparently - Mat crop2ContourInv(const Mat & img); Contour_t getLongestContour(Mat bw); +int getLongestContourIndex(Contour_t contours); void Puzzle::printPuzzle() { @@ -131,7 +131,7 @@ Mat Puzzle::readImage(int fileIndex, const char* inputDir){ Mat source = imread(inputstr,1); return source; } -Mat Puzzle::resultImage_for_merging( vector& log){ +Mat Puzzle::resultImage( vector& log){ int Y_size = 1200; // chose this to fit your monitor! int separator = 1; int partHeight = 90; @@ -189,7 +189,7 @@ Mat Puzzle::resultImage_for_merging( vector& log){ center.x = img.cols/2; center.y = img.rows/2; Mat RotMatrix = getRotationMatrix2D(center,angle,1); - warpAffine(img,img,RotMatrix, img.size()); + warpAffine(invert,invert,RotMatrix, invert.size()); bitwise_not(invert,img); Mat cropped = crop2ContourInv(img); auto ROI_X = int(round(it.myCoor.col*partWidth)); @@ -220,12 +220,7 @@ Mat crop2ContourInv(const Mat & img){ // for white background images return cropped; } -Contour_t getLongestContour(Mat bw){ - - Mat mask = Mat::zeros(bw.size(), CV_8UC1); - Hierarchy_t hierarchy; - Contour_t contours; - findContours(bw, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); +int getLongestContourIndex(Contour_t contours){ double area = 0; double largestArea = 0; size_t largestIndex = 0; @@ -238,12 +233,5 @@ Contour_t getLongestContour(Mat bw){ largestIndex = i; // i is type size_t, should be converted to int. } } - - Contour_t largestContour; - largestContour.resize(contours.size()); //Otherwise it is just a null pointer and the program crashes! - size_t index = 0; - int strength = 2; // distance between the original contour and the approximated contour - approxPolyDP(Mat(contours[largestIndex]), largestContour[index], strength, true); - return(largestContour); -} - + return(largestIndex); +} \ No newline at end of file