diff --git a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp index 5a901c6..a9756aa 100644 --- a/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp +++ b/Source/functions/AbstractionLayers/DestructionPower/DestructionPower.cpp @@ -71,10 +71,10 @@ float DestructionPower::defaultDestructionPower(int i) //gets next highest valued abstraction layer down from current one (if first, get highest) int DestructionPower::getNextAbstractionLayer(coor newCoordinate, int currentAbstractionLayer) { - if(++currentAbstractionLayer>=DESTRUCTION_COUNT) - return -1; - else - return currentAbstractionLayer; + //hardcode advance + if(currentAbstractionLayer &ref_partArray = *partArray; - int iterator = 0; - - for(int count =0;count< mySize.row*mySize.col*4;count++)// multiplied by 4 for all rotations + for(int count =0;count< mySize.row*mySize.col;count++)// multiplied by 4 for all rotations { Scalar tempHue, tempSaturation, tempValue; - if(count%4) { - char name[100]; - sprintf(name, PATH, count); - Mat Part = imread(name, 1); - if (!Part.data) { - cerr << "Could not open or find the image" << endl; - return -1; - } + char name[100]; - Mat PartCropped = Part(Rect(Part.size().width / 3, Part.size().height / 3, Part.size().width / 3, - Part.size().height / 3)); - - //namedWindow("PartCropped", WINDOW_AUTOSIZE); // Create a window for display. - //imshow("PartCropped", PartCropped); - //waitKey(0); // Wait for a keystroke in the window - - // Create a new matrix to hold the HSV image - Mat HSVPart; - - // convert RGB image to HSV - cvtColor(PartCropped, HSVPart, CV_BGR2HSV); - - vector hsv_planes; - split(HSVPart, hsv_planes); - - tempHue = mean(hsv_planes[0]); - tempSaturation = mean(hsv_planes[1]); - tempValue = mean(hsv_planes[2]); - - //cout << "Hue: " << tempHue.val[0] << endl; - //cout << "Saturation: " << tempSaturation.val[0] << endl; - //cout << "Value: " << tempValue.val[0] << endl; + sprintf(name, PATH, count); + Mat Part = imread(name, 1); + if (!Part.data) { + cerr << "Could not open or find the image" << endl; + return -1; } + Scalar value(255, 255, 255 ); + copyMakeBorder( Part, Part, 3, 3, 3, 3, BORDER_CONSTANT, value ); - ref_partArray[iterator]->m_acm.m_centerColor.h = tempHue.val[0]; - ref_partArray[iterator]->m_acm.m_centerColor.s = tempSaturation.val[1]; - ref_partArray[iterator]->m_acm.m_centerColor.v = tempValue.val[2]; + Mat PartCropped = Part;//(Rect(Part.size().width / 2, Part.size().height / 2, Part.size().width / 2, Part.size().height / 2)); + Mat gray; - iterator ++; + //Prepare the image for findContours + cvtColor(PartCropped, gray, CV_BGR2GRAY); + threshold(gray, gray, 250, 255, CV_THRESH_BINARY_INV); + + + //namedWindow("PartCropped", WINDOW_AUTOSIZE); // Create a window for display. + //imshow("PartCropped", gray); + //waitKey(0); // Wait for a keystroke in the window + + //Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten + std::vector > contours; + //cv::Mat contourOutput = gray.clone(); + cv::findContours( gray.clone(), contours, CV_RETR_LIST , CV_CHAIN_APPROX_SIMPLE ); + + //Draw the contours + Scalar colors[3]; + colors[0] = cv::Scalar(255, 0, 0); + colors[1] = cv::Scalar(0, 255, 0); + colors[2] = cv::Scalar(0, 0, 255); + + double length; + int max=0; + int maxidx=0; + for (size_t idx = 0; idx < contours.size(); idx++) { + length=arcLength(contours[idx], true); + if(length>max && (contours[idx].max_size()>10)) { + max = length; + maxidx = idx; + } + } + //cv::drawContours(PartCropped, contours, maxidx, colors[maxidx % 3],3); + + //namedWindow("PartCropped", WINDOW_AUTOSIZE); // Create a window for display. + //imshow("PartCropped", PartCropped); + //waitKey(0); // Wait for a keystroke in the window + + // Create a new matrix to hold the HSV image + Mat HSVPart; + + // convert RGB image to HSV + cvtColor(PartCropped, HSVPart, CV_BGR2HSV); + + vector hsv_planes; + split(HSVPart, hsv_planes); + + Rect roi = boundingRect(contours[maxidx]); + + Mat resImageMeanRGB = PartCropped.clone(); + Scalar mean_RGB = mean(PartCropped(roi), gray(roi)); + //drawContours( resImageMeanRGB, contours, maxidx, mean_RGB, FILLED ); + //imshow( "MeanRGB", resImageMeanRGB ); + + tempHue = mean(hsv_planes[0](roi), gray(roi)); + tempSaturation = mean(hsv_planes[1](roi), gray(roi)); + tempValue = mean(hsv_planes[2](roi), gray(roi)); + + /*Mat restempHue = PartCropped.clone(); + drawContours( restempHue, contours, maxidx, tempHue, FILLED ); + imshow( "MeanHue", restempHue ); + Mat restempSaturation = PartCropped.clone(); + drawContours( restempSaturation, contours, maxidx, tempSaturation, FILLED ); + imshow( "MeanSat", restempSaturation ); + Mat restempValue = PartCropped.clone(); + drawContours( restempValue, contours, maxidx, tempValue, FILLED ); + imshow( "MeanValue", restempValue ); + + cout << "RGB: " << mean_RGB << endl; + cout << "Hue: " << tempHue.val[0] << endl; + cout << "Saturation: " << tempSaturation.val[0] << endl; + cout << "Value: " << tempValue.val[0] << endl; + + imshow("Input Image", PartCropped); + cvMoveWindow("Input Image", 0, 0); + waitKey(0);*/ + + for(int rotate=0;rotate<4;rotate++) + { + ref_partArray[count*4+rotate]->m_acm.m_centerColor.h = tempHue.val[0]; + ref_partArray[count*4+rotate]->m_acm.m_centerColor.s = tempSaturation.val[0]; + ref_partArray[count*4+rotate]->m_acm.m_centerColor.v = tempValue.val[0]; + } } return true; } - bool AbstractionLayer_ColorMatching::EvaluateQuality (const coor constraintCoordinate, qualityVector& qVector) { for(int i = 0;im_acm.m_partColor); - qVector[i].first = value; + float value1 = PlaceOfPartGood(constraintCoordinate, qVector[i].second->m_acm.m_centerColor); + + + float value2 = (float)(1-(abs(m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].m_centerColor.s-qVector[i].second->m_acm.m_centerColor.s))/255); + float value3 = (float)(1-(abs(m_constraintMatrix[constraintCoordinate.col][constraintCoordinate.row].m_centerColor.v-qVector[i].second->m_acm.m_centerColor.v))/255); + + + qVector[i].first = (value1+value2+value3)/3; } } float AbstractionLayer_ColorMatching::PlaceOfPartGood(coor myCoor, HSV myPart) { - //Hue max 360° - if(m_constraintMatrix[myCoor.col][myCoor.row].m_partColor.h >= 180) + //Hue max 180° + if(m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h >= myPart.h) { - return 1-abs((m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h-myPart.h) - /m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h); + if((m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h-myPart.h) > 90) + { + return 1-((myPart.h+180-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h) + /90); + } else + { + return 1-((m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h-myPart.h) + /90); + } } - else if(m_constraintMatrix[myCoor.col][myCoor.row].m_partColor.h < 180) + else if(m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h < myPart.h) { - return 1-((myPart.h-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h) - /(360-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h)); + if((myPart.h-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h) > 90) + { + return 1-abs((myPart.h-180-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h) + /90); + } else + { + return 1-((myPart.h-m_constraintMatrix[myCoor.col][myCoor.row].m_centerColor.h) + /90); + } } -} \ No newline at end of file +} diff --git a/Source/functions/solve/puzzleExtension.cpp b/Source/functions/solve/puzzleExtension.cpp index 457b715..5859c14 100644 --- a/Source/functions/solve/puzzleExtension.cpp +++ b/Source/functions/solve/puzzleExtension.cpp @@ -177,12 +177,6 @@ Mat Puzzle::resultImage( vector& log){ char name[100]; for (auto it:log) { - if (it.myCoor.col == 27 && it.myCoor.row == 5) - { - ; - // imshow("result",result); - // waitKey(0); - } //cout << it.PieceCollector[0].second->GetPartID() << endl; diff --git a/Source/functions/solve/structure.cpp b/Source/functions/solve/structure.cpp index e0787c5..6197c38 100755 --- a/Source/functions/solve/structure.cpp +++ b/Source/functions/solve/structure.cpp @@ -43,7 +43,7 @@ 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 + puzzleMat.dp.DestructionOfSurrounding(log.back().myCoor);//calculate dp from surrounding //get all not set pieces for(auto it:puzzleMat.p_myBox) if(!it->set) @@ -80,17 +80,15 @@ void solve(vector& log,Puzzle& puzzleMat) { case 0://pömpel puzzleMat.a1.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); - //puzzleMat.a1.EvaluateQuality(log.back().myCoor, log.back().PieceCollector); break; case 1://SURFFeature // return; puzzleMat.a4.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; - case 3://poempelposition - return; + case 2://poempelposition puzzleMat.a3.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; - case 2://color + case 3://color puzzleMat.acm.EvaluateQuality(log.back().myCoor,log.back().PieceCollector); break; case -1://random @@ -101,7 +99,7 @@ void solve(vector& log,Puzzle& puzzleMat) } float worth = capLogElements(log); cout << "remaining: " << log.back().PieceCollector.size() << endl; -// calculateTrueDestructionPower(log,puzzleMat, worth); + calculateTrueDestructionPower(log,puzzleMat, worth); CalculateNewCombinedQuality(log, log.back().PieceCollector, puzzleMat.combinedQualityVector); } @@ -123,12 +121,14 @@ void setsolution(vector& log, Puzzle& puzzleMat) puzzleMat.setConstraints(log.back().myCoor,log.back().PieceCollector.begin()->second); cout << "set:" << log.back().myCoor.col << "," << log.back().myCoor.row << endl; //cout << "ID: " << log.back().PieceCollector[0].second->GetPartID() << endl; + if(log.back().myCoor.col>=31 && log.back().myCoor.row==5) + puzzleMat.resultImage(log); + } bool backtrack(vector& log, Puzzle& puzzleMat) { - puzzleMat.resultImage(log); - cout << "backtrack" << endl; + cout << "backtrack" << endl; if(log.empty()) { cout << "Puzzle not solveable!" << endl; @@ -164,8 +164,11 @@ bool backtrack(vector& log, Puzzle& puzzleMat) puzzleMat.p_myBox[i]->set=false; log.pop_back(); - if(!backtrack(log,puzzleMat)) + if(!backtrack(log,puzzleMat)) + { return false; + + } return true; } @@ -189,7 +192,7 @@ float capLogElements(vector& log) { // Till Now only ground structure -> incorrect variable ans vector names - double limit = 0.9; + double limit = 0.6; double diff = 0; int id=0;