diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp index ff2cab6..d3847f1 100644 --- a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp @@ -1,7 +1,5 @@ #include "MooreBoundaryTrackingAlgorithm.h" #include "avansvisionlib.h" -#include - //direction values // starting at position 0. Definition of relative positions: @@ -11,44 +9,24 @@ int rotateX[] = { 0, 1, 1, 1, 0, -1, -1, -1 }; int rotateY[] = { -1, -1, 0, 1, 1, 1, 0, -1 }; -struct pointCompare -{ - bool operator() (const Point &a, const Point &b) - { - if (a.x < b.x) - return true; - return a.y < b.y; - } -}; - 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 (int y = 0; y < binaryImage.rows; y++) { - for (int x = 0; x < binaryImage.cols; x++) - { - Point p = Point(x, y); + for (Point2d *ptr : firstPixelVec2) { + vector contourVec; - if (getEntryImage(binaryImage, y, x) != 1) - { - continue; - } + 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); - if (isInsideContours(contourVecVec, p)) - { - continue; - } - - - vector contourVec; - - firstb0 = p; - b0 = p; - c0 = Point(x - 1, y); - - int firstDirection = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); + int firstDirection = discoverNext(binaryImage, b0, 6); b1 = getDirPos(b0, firstDirection); firstb1 = getDirPos(b0, firstDirection); c1 = getDirPos(b0, (firstDirection + 7) % 8); @@ -58,15 +36,14 @@ int allContours(Mat binaryImage, vector>& contourVecVec) do { - b0 = b1; - c0 = c1; + b0 = Point(b1.x, b1.y); + c0 = Point(c1.x, c1.y); - int cdir = discoverNextRelativeDirection(b0, c0); - int dir = discoverNext(binaryImage, b0, cdir); + int dir = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); if (dir < 0) { - cout << "Het gaat eventjes niet goed hoor: " << cdir << " : " << b0 << " - " << c0 << endl; + cout << "Er is niks aan de hand! " << " : " << b0 << " - " << c0 << endl; } b1 = getDirPos(b0, dir); @@ -78,17 +55,18 @@ int allContours(Mat binaryImage, vector>& contourVecVec) } while (b0 != firstb0 && b1 != firstb1); - if(contourVec.size() > 5) + cout << "Klaar met het vinden van contouren" << endl; + + if(contourVec.size() > 4) contourVecVec.push_back(contourVec); - } } - flip(contourVecVec); + //flip(contourVecVec); - return contourVecVec.size(); //number of objects + return numberOfBlobs; //number of objects } -int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection) +int discoverNext(Mat &binaryImage, const cv::Point &pos, int beginDirection) { if (beginDirection < 0 || beginDirection > 8) { cout << "Begin direction is invalid : " << beginDirection << endl; @@ -100,7 +78,7 @@ int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection) int newX = pos.x + rotateX[dir]; int newY = pos.y + rotateY[dir]; - if (getEntryImage(binaryImage, newX, newY) == 1) { + if (getEntryImage(binaryImage, newY, newX) == 1) { return dir; } } @@ -108,7 +86,7 @@ int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection) return -1; } -int discoverNextRelativeDirection(cv::Point pos, cv::Point target) +int discoverNextRelativeDirection(const cv::Point &pos, const cv::Point &target) { for (int i = 0; i < 8; i++) { int dir = i; @@ -123,7 +101,7 @@ int discoverNextRelativeDirection(cv::Point pos, cv::Point target) return -1; } -cv::Point getDirPos(cv::Point pos, int dir) +cv::Point getDirPos(const cv::Point &pos, int dir) { int x, y; x = pos.x + rotateX[dir]; @@ -169,6 +147,19 @@ bool isInsideContours(vector>& vec, Point point) } return false; +} - -} \ No newline at end of file +double getBendingEnergy(const vector& contour) +{ + double energy = 0; + + Point previousPoint = contour[contour.size() - 1]; + + for (Point p : contour) + { + energy += discoverNextRelativeDirection(previousPoint, p); + previousPoint = p; + } + + return energy; +} diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.h b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h index 2b9e380..966e153 100644 --- a/AdvancedVision/MooreBoundaryTrackingAlgorithm.h +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h @@ -17,16 +17,18 @@ int allContours(Mat binaryImage, vector> &contourVecVec); // func: discover next b // return_value: new direction -int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection); +int discoverNext(Mat &binaryImage, const cv::Point &pos, int beginDirection); // func: discover next c // return_value : new direction -int discoverNextRelativeDirection(cv::Point pos, cv::Point target); +int discoverNextRelativeDirection(const cv::Point &pos, const cv::Point &target); // func: get a point with a given position and direction // return_value : new point -cv::Point getDirPos(cv::Point pos, int dir); +cv::Point getDirPos(const cv::Point &pos, int dir); bool contains(vector vec, Point p); void flip(vector> &vec); -bool isInsideContours(vector>& vec, Point point); \ No newline at end of file +bool isInsideContours(vector>& vec, Point point); + +double getBendingEnergy(const vector &contour); \ No newline at end of file diff --git a/AdvancedVision/allContoursTestprogramma.cpp b/AdvancedVision/allContoursTestprogramma.cpp index 0eefd3d..acf9667 100644 --- a/AdvancedVision/allContoursTestprogramma.cpp +++ b/AdvancedVision/allContoursTestprogramma.cpp @@ -101,6 +101,17 @@ int main(int argc, char *argv[]) imshow("Found contours", contourImage); waitKey(0); + //haal bending energy waarde op + + + cout << "Bending Energy" << endl << "-----------" << endl; + + for (int i = 0; i < contours.size(); i++) + { + cout << "[" << i << "] " << getBendingEnergy(contours[i]) << endl; + } + + string pipo; cin >> pipo; diff --git a/AdvancedVision/input/Week 2/monsters.jpg b/AdvancedVision/input/Week 2/monsters.jpg index 3013a3f..7655643 100644 Binary files a/AdvancedVision/input/Week 2/monsters.jpg and b/AdvancedVision/input/Week 2/monsters.jpg differ