From acff1c02b177cbefe3b3c246cbc2b5413457fd0b Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Wed, 22 Nov 2017 16:36:07 +0100 Subject: [PATCH] Werkt nog steeds niet --- .../MooreBoundaryTrackingAlgorithm.cpp | 100 +++++++++++------- .../MooreBoundaryTrackingAlgorithm.h | 5 +- 2 files changed, 68 insertions(+), 37 deletions(-) diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp index d3a9246..ff2cab6 100644 --- a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp @@ -23,56 +23,69 @@ struct pointCompare int allContours(Mat binaryImage, vector>& contourVecVec) { - Mat labeledImage; - vector firstPixelVec2; - vector posVec2; - vector areaVec2; - int numberOfBlobs = labelBLOBsInfo(binaryImage, labeledImage, firstPixelVec2, posVec2, areaVec2); - Point b0, b1, c0, c1, firstb0, firstb1; - for (Point2d *ptr : firstPixelVec2) { - vector contourVec; + for (int y = 0; y < binaryImage.rows; y++) { + for (int x = 0; x < binaryImage.cols; x++) + { + Point p = Point(x, y); - firstb0 = Point((int)ptr->y, (int)ptr->x); - b0 = Point((int)ptr->y, (int)ptr->x); - c0 = Point((int)(ptr->y - 1), (int)ptr->x); - - int firstDirection = discoverNext(binaryImage, b0, 6); - b1 = getDirPos(b0, firstDirection); - firstb1 = getDirPos(b0, firstDirection); - c1 = getDirPos(b0, (firstDirection + 7) % 8); - - contourVec.push_back(b0); - contourVec.push_back(b1); - - - do { - b0 = b1; - c0 = c1; - - int dir = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); - - if (dir < 0) + if (getEntryImage(binaryImage, y, x) != 1) { - cout << "Het gaat eventjes niet goed hoor: " << b0 << " - " << c0 << endl; + continue; } - b1 = getDirPos(b0, dir); - c1 = getDirPos(b0, (dir + 7) % 8); + if (isInsideContours(contourVecVec, p)) + { + continue; + } - if(!contains(contourVec, b1)) - contourVec.push_back(b1); - } while (b0 != firstb0 && b1 != firstb1); + vector contourVec; - contourVecVec.push_back(contourVec); + firstb0 = p; + b0 = p; + c0 = Point(x - 1, y); + + int firstDirection = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); + b1 = getDirPos(b0, firstDirection); + firstb1 = getDirPos(b0, firstDirection); + c1 = getDirPos(b0, (firstDirection + 7) % 8); + + contourVec.push_back(b0); + contourVec.push_back(b1); + + do { + + b0 = b1; + c0 = c1; + + int cdir = discoverNextRelativeDirection(b0, c0); + int dir = discoverNext(binaryImage, b0, cdir); + + if (dir < 0) + { + cout << "Het gaat eventjes niet goed hoor: " << cdir << " : " << b0 << " - " << c0 << endl; + } + + b1 = getDirPos(b0, dir); + c1 = getDirPos(b0, (dir + 7) % 8); + + //TODO + if (!contains(contourVec, b1)) + contourVec.push_back(b1); + + } while (b0 != firstb0 && b1 != firstb1); + + if(contourVec.size() > 5) + contourVecVec.push_back(contourVec); + } } flip(contourVecVec); - return numberOfBlobs; //number of objects + return contourVecVec.size(); //number of objects } int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection) @@ -143,4 +156,19 @@ void flip(vector> &vec) p.y = x; } } +} + +bool isInsideContours(vector>& vec, Point point) +{ + for (vector cont : vec) + { + int i = pointPolygonTest(cont, point, false); + + if (i >= 0) + return true; + } + + return false; + + } \ No newline at end of file diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.h b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h index aa28c4e..2b9e380 100644 --- a/AdvancedVision/MooreBoundaryTrackingAlgorithm.h +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h @@ -1,9 +1,11 @@ #include #include +#include #include #include #include + using namespace cv; using namespace std; @@ -26,4 +28,5 @@ int discoverNextRelativeDirection(cv::Point pos, cv::Point target); cv::Point getDirPos(cv::Point pos, int dir); bool contains(vector vec, Point p); -void flip(vector> &vec); \ No newline at end of file +void flip(vector> &vec); +bool isInsideContours(vector>& vec, Point point); \ No newline at end of file