diff --git a/AdvancedVision/AdvancedVision.vcxproj b/AdvancedVision/AdvancedVision.vcxproj index 1447682..06d2699 100644 --- a/AdvancedVision/AdvancedVision.vcxproj +++ b/AdvancedVision/AdvancedVision.vcxproj @@ -164,10 +164,12 @@ + + diff --git a/AdvancedVision/AdvancedVision.vcxproj.filters b/AdvancedVision/AdvancedVision.vcxproj.filters index 168e7c1..fc83533 100644 --- a/AdvancedVision/AdvancedVision.vcxproj.filters +++ b/AdvancedVision/AdvancedVision.vcxproj.filters @@ -27,10 +27,16 @@ Source Files + + Source Files + Header Files + + Header Files + \ No newline at end of file diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp new file mode 100644 index 0000000..d978c22 --- /dev/null +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp @@ -0,0 +1,101 @@ +#include "MooreBoundaryTrackingAlgorithm.h" +#include "avansvisionlib.h" +#include + +//direction values +// starting at position 0. Definition of relative positions: +// 7 0 1 +// 6 X 2 +// 5 4 3 +int rotateX[] = { 0, 1, 1, 1, 0, -1, -1, -1 }; +int rotateY[] = { -1, -1, 0, 1, 1, 1, 0, -1 }; + +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) { + set contourVec; + + firstb0 = Point((int)ptr->x, (int)ptr->y); + b0 = Point((int)ptr->x, (int)ptr->y); + c0 = Point((int)(ptr->x - 1), (int)ptr->y); + + int firstDirection = discoverNext(binaryImage, b0, 6); + b1 = getDirPos(b0, firstDirection); + firstb1 = b1; + c1 = getDirPos(b0, (firstDirection + 7) % 8); + + contourVec.insert(b0); + contourVec.insert(b1); + + + do { + b0 = b1; + c0 = c1; + + int dir = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); + b1 = getDirPos(b0, dir); + c1 = getDirPos(b0, (dir + 7) % 8); + + contourVec.insert(b1); + + } while (b0 == firstb0 && b1 == firstb1); + + std::vector output; + std::copy(contourVec.begin(), contourVec.end(), output.begin()); + contourVecVec.push_back(output); + } + + return numberOfBlobs; //number of objects +} + +int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection) +{ + if (beginDirection > 8) { + return -1; + } + for (int i = 0; i < 8; i++) { + int dir = (i + beginDirection) % 8; + + int newX = pos.x + rotateX[dir]; + int newY = pos.y + rotateY[dir]; + + if (getEntry(binaryImage, newX, newY) == 1) { + return dir; + } + } + + return -1; +} + +int discoverNextRelativeDirection(cv::Point pos, cv::Point target) +{ + for (int i = 0; i < 8; i++) { + int dir = i; + + int newX = pos.x + rotateX[dir]; + int newY = pos.y + rotateY[dir]; + + if (target.x == newX && target.y == newY) + return dir; + } + + return -1; +} + +cv::Point getDirPos(cv::Point pos, int dir) +{ + int x, y; + x = pos.x + rotateX[dir]; + y = pos.y + rotateY[dir]; + + return cv::Point(x, y); +} diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.h b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h new file mode 100644 index 0000000..ad5a7c8 --- /dev/null +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.h @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +using namespace cv; +using namespace std; + +// func: finds all contours of all blobs in a image +// pre : binaryImage has depth 16 bits signed int. Contains only values 0 and 1. +// post: contourVec: contains the points of the contour of each blob. +// return_value: the total number of objects. +int allContours(Mat binaryImage, vector> &contourVecVec); + +// func: discover next b +// return_value: new direction +int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection); + +// func: discover next c +// return_value : new direction +int discoverNextRelativeDirection(cv::Point pos, 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); \ No newline at end of file diff --git a/AdvancedVision/Source.cpp b/AdvancedVision/Source.cpp index 3e051f8..3a7e2d0 100644 --- a/AdvancedVision/Source.cpp +++ b/AdvancedVision/Source.cpp @@ -12,7 +12,7 @@ using namespace cv; using namespace std; -int main(int argc, char** argv) +int main2(int argc, char** argv) { Mat image = imread("input/Week2/basisfiguren.jpg", CV_LOAD_IMAGE_COLOR); if (!image.data) diff --git a/AdvancedVision/allContoursTestprogramma.cpp b/AdvancedVision/allContoursTestprogramma.cpp index 1329c44..0eefd3d 100644 --- a/AdvancedVision/allContoursTestprogramma.cpp +++ b/AdvancedVision/allContoursTestprogramma.cpp @@ -11,6 +11,7 @@ #include #include #include "avansvisionlib.h" +#include "MooreBoundaryTrackingAlgorithm.h" using namespace cv; using namespace std;