diff --git a/AdvancedVision/BoundingBox.cpp b/AdvancedVision/BoundingBox.cpp new file mode 100644 index 0000000..468af8e --- /dev/null +++ b/AdvancedVision/BoundingBox.cpp @@ -0,0 +1,89 @@ +#include "BoundingBox.h" +#include + + +BoundingBox::BoundingBox() +{ +} + + +BoundingBox::~BoundingBox() +{ +} + +// func: delivers bounding Boxes of contours +// pre: contours contains the contours for which bounding boxes have to be delivered +// post: bbs contains all bounding boxes. The index corresponds to the index of contours. +// I.e. bbs[i] belongs to contours[i] +int BoundingBox::allBoundingBoxes(const vector> & contours, vector> & bbs) +{ + for (vector contour : contours) + { + int maxx = INT_MIN, maxy = INT_MIN, minx = INT_MAX, miny = INT_MAX; + + for (Point p : contour) + { + if (p.x > maxx) + maxx = p.x; + + if (p.y > maxy) + maxy = p.y; + + if (p.x < minx) + minx = p.x; + + if (p.y < miny) + miny = p.y; + } + + vector bounding; + bounding.push_back(Point(minx, miny)); + bounding.push_back(Point(maxx, miny)); + bounding.push_back(Point(maxx, maxy)); + bounding.push_back(Point(minx, maxy)); + bbs.push_back(bounding); + } + + return contours.size(); +} + +int BoundingBox::split(const vector> & contours, const Mat &image, string filename) +{ + //Create filenames + std::string base_filename = filename.substr(filename.find_last_of("/\\") + 1); + std::string::size_type const p(base_filename.find_last_of('.')); + std::string name = base_filename.substr(0, p); + + vector> bounds; + allBoundingBoxes(contours, bounds); + + //Find largest bounding box + Size largest = Size(0,0); + for (vector box : bounds) + { + int height = abs(box[2].y - box[1].y); + int width = abs(box[1].x - box[0].x); + + if (height > largest.height) + largest.height = height; + if (width > largest.width) + largest.width = width; + } + + int nr = 0; + + //Create every image + for (vector box : bounds) + { + Point center = Point((box[0].x + box[2].x) / 2, (box[0].y + box[2].y) / 2); + + Rect r = Rect(center.x - (largest.width / 2), center.y - (largest.height / 2),largest.width, largest.height); + Mat roi(image, r); + + imwrite("output/" + name + "_" + to_string(nr) + ".bmp", roi); + + nr++; + } + + return 1; +} \ No newline at end of file diff --git a/AdvancedVision/BoundingBox.h b/AdvancedVision/BoundingBox.h new file mode 100644 index 0000000..805ae6b --- /dev/null +++ b/AdvancedVision/BoundingBox.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace cv; + +class BoundingBox +{ +public: + BoundingBox(); + ~BoundingBox(); + static int allBoundingBoxes(const vector> & contours, vector> & bbs); + static int split(const vector> & contours, const Mat &image, string filename); +}; + diff --git a/AdvancedVision/FloodFill.cpp b/AdvancedVision/FloodFill.cpp new file mode 100644 index 0000000..af3e458 --- /dev/null +++ b/AdvancedVision/FloodFill.cpp @@ -0,0 +1,174 @@ +#include "FloodFill.h" +#include "BoundingBox.h" + + +FloodFill::FloodFill() +{ +} + + +FloodFill::~FloodFill() +{ +} + +int FloodFill::enclosedPixels(const vector& contourVec, vector & regionPixels) +{ + Point p = findStartPoint(contourVec); + regionPixels.push_back(p); + + floodfill(contourVec, regionPixels, p); + + return 1; +} + +int FloodFill::floodfill(const vector & contourVec, vector & regionPixels, Point &p) +{ + //Check four-connected + int pointsx[] = {0, 1, 0, -1}; + int pointsy[] = {-1, 0, 1, 0}; + + for (int i = 0; i < 4; i++) + { + Point check = Point(p.x + pointsx[i], p.y + pointsy[i]); + + if (contains(contourVec, check)) + { + continue; + } + else if (contains(regionPixels, check)) + { + continue; + } + else + { + regionPixels.push_back(check); + floodfill(contourVec, regionPixels, check); + } + } + + return 1; +} + +Point FloodFill::findStartPoint(vector vec) +{ + int maxy = INT_MIN, maxx = INT_MIN, miny = INT_MAX, minx = INT_MAX; + + for (Point p : vec) + { + if (p.y > maxy) + maxy = p.y; + + if (p.y < miny) + miny = p.y; + + if (p.x > maxx) + maxx = p.x; + + if (p.x < minx) + minx = p.x; + } + + int centery = (maxy + miny) / 2; + + int x1 = 0, x2 = 0; + + for (int x = minx; x <= maxx; x++) + { + if (contains(vec, Point(x, centery))) + { + if (x1 == 0) + { + x1 = x; + } + else + { + x2 = x; + break; + } + } + } + + int centerx = (x1 + x2) / 2; + Point start = Point(centerx, centery); + + return start; +} + +bool FloodFill::contains(vector vec, Point p) +{ + for (Point pnt : vec) + { + if (pnt.x == p.x && pnt.y == p.y) + return true; + } + + return false; +} + + +int FloodFill::split(const vector> & contours, const Mat &image, string filename) +{ + //Create filenames + std::string base_filename = filename.substr(filename.find_last_of("/\\") + 1); + std::string::size_type const p(base_filename.find_last_of('.')); + std::string name = base_filename.substr(0, p); + + vector> regions; + regions.resize(contours.size()); + + for (int i = 0; i < contours.size(); i++) + { + vector temp; + enclosedPixels(contours[i], temp); + regions[i] = temp; + } + + + vector> bounds; + BoundingBox::allBoundingBoxes(contours, bounds); + + //Find largest bounding box + Size largest = Size(0, 0); + for (vector box : bounds) + { + int height = abs(box[2].y - box[1].y); + int width = abs(box[1].x - box[0].x); + + if (height > largest.height) + largest.height = height; + if (width > largest.width) + largest.width = width; + } + + int nr = 0; + + //Create every image + for (int i = 0; i < contours.size(); i++) + { + // Creeer een witte image + IplImage* iplimage = cvCreateImage(cvSize(image.cols, image.rows), IPL_DEPTH_8U, 3); + Mat resultImage = cvarrToMat(iplimage); + resultImage = Scalar(255, 255, 255); + + //Kopieren van pixels + for (Point p : regions[i]) + { + resultImage.at(p) = image.at(p); + } + for (Point p : contours[i]) + { + resultImage.at(p) = image.at(p); + } + + Point center = Point((bounds[i][0].x + bounds[i][2].x) / 2, (bounds[i][0].y + bounds[i][2].y) / 2); + + Rect r = Rect(center.x - (largest.width / 2), center.y - (largest.height / 2), largest.width, largest.height); + Mat roi(resultImage, r); + + imwrite("output/" + name + "_" + to_string(nr) + ".bmp", roi); + + nr++; + } + + return 1; +} \ No newline at end of file diff --git a/AdvancedVision/FloodFill.h b/AdvancedVision/FloodFill.h new file mode 100644 index 0000000..33002bd --- /dev/null +++ b/AdvancedVision/FloodFill.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +using namespace cv; +using namespace std; + +class FloodFill +{ +public: + FloodFill(); + ~FloodFill(); + static int enclosedPixels(const vector & contourVec, vector & regionPixels); + static bool contains(vector vec, Point p); + static Point findStartPoint(vector vec); + static int floodfill(const vector & contourVec, vector & regionPixels, Point &p); + static int split(const vector> & contours, const Mat &image, string filename); +}; + diff --git a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp index 568f425..9e088cd 100644 --- a/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp +++ b/AdvancedVision/MooreBoundaryTrackingAlgorithm.cpp @@ -2,7 +2,7 @@ #include "avansvisionlib.h" //direction values -// starting at position 0. Definition of relative positions: +//starting at position 0. Definition of relative positions: // 7 0 1 // 6 X 2 // 5 4 3 @@ -41,15 +41,9 @@ int allContours(Mat binaryImage, vector>& contourVecVec) int dir = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0)); - //if (dir < 0) - //{ - // cout << "Er is niks aan de hand! " << " : " << b0 << " - " << c0 << endl; - //} - b1 = getDirPos(b0, dir); c1 = getDirPos(b0, (dir + 7) % 8); - //TODO if (!contains(contourVec, b1)) contourVec.push_back(b1); @@ -61,8 +55,6 @@ int allContours(Mat binaryImage, vector>& contourVecVec) contourVecVec.push_back(contourVec); } - //flip(contourVecVec); - return numberOfBlobs; //number of objects } diff --git a/AdvancedVision/allContoursTestprogramma.cpp b/AdvancedVision/allContoursTestprogramma.cpp index ec0f607..8d372c3 100644 --- a/AdvancedVision/allContoursTestprogramma.cpp +++ b/AdvancedVision/allContoursTestprogramma.cpp @@ -6,8 +6,6 @@ // // Jan Oostindie, dd 8-11-2016 // -//#pragma GCC push_options -//#pragma GCC optimize ("O0") #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" @@ -15,6 +13,8 @@ #include #include "avansvisionlib.h" #include "MooreBoundaryTrackingAlgorithm.h" +#include "BoundingBox.h" +#include "FloodFill.h" using namespace cv; using namespace std; @@ -81,8 +81,9 @@ int main(int argc, char *argv[]) // druk alle punten van alle gevonden contouren af string line; - cout << "Aantal gevonden contouren = " << contours.size() << endl; + cout << "Aantal gevonden contouren = " << aantal << endl; getline(cin, line); + /* for (int i = 0; i < contours.size(); i++) { cout << "*** Contour " << i + 1 << " ***" << endl; cout << "Press ENTER to continue...."; @@ -91,6 +92,26 @@ int main(int argc, char *argv[]) cout << "(" << contours[i][j].x << "," << contours[i][j].y << ")" << endl; } } + */ + + /* + cout << "Bounding boxes bepalen..." << endl; + vector> bounds; + int aantalB = BoundingBox::allBoundingBoxes(contours, bounds); + + cout << "Aantal gevonden boundaries = " << aantalB << endl; + */ + + //Split images + FloodFill::split(contours, image, argv[1]); + + /* + cout << "Fill" << endl; + vector region; + Point center = FloodFill::findStartPoint(contours[0]); + cout << "Center point: " << center.x << ", " << center.y << endl; + FloodFill::enclosedPixels(contours[0], region); + */ // Creeer een witte image IplImage* iplimage = cvCreateImage(cvSize(binary16S.cols, binary16S.rows), IPL_DEPTH_8U, 3); @@ -99,13 +120,19 @@ int main(int argc, char *argv[]) // teken de contouren op de witte image drawContours(contourImage, contours, -1, CV_RGB(255, 0, 0)); + /* + drawContours(contourImage, bounds, -1, CV_RGB(0, 0, 255)); + + for (Point p : region) + { + circle(contourImage, p, 1, CV_RGB(0, 255, 0)); + }*/ // druk het image met de contouren af imshow("Found contours", contourImage); waitKey(0); //haal bending energy waarde op - cout << "Bending Energy" << endl << "-----------" << endl; @@ -115,10 +142,7 @@ int main(int argc, char *argv[]) } - string pipo; - cin >> pipo; + waitKey(0); return 0; -} - -//#pragma GCC pop_options \ No newline at end of file +} \ No newline at end of file diff --git a/AdvancedVision/input/basisfiguren.jpg b/AdvancedVision/input/basisfiguren.jpg new file mode 100644 index 0000000..8f843a2 Binary files /dev/null and b/AdvancedVision/input/basisfiguren.jpg differ diff --git a/AdvancedVision/input/besteplaatjeooit.png b/AdvancedVision/input/besteplaatjeooit.png new file mode 100644 index 0000000..06a87a8 Binary files /dev/null and b/AdvancedVision/input/besteplaatjeooit.png differ diff --git a/AdvancedVision/input/bolenpijl.jpg b/AdvancedVision/input/bolenpijl.jpg new file mode 100644 index 0000000..422b24f Binary files /dev/null and b/AdvancedVision/input/bolenpijl.jpg differ diff --git a/AdvancedVision/input/monsters.jpg b/AdvancedVision/input/monsters.jpg new file mode 100644 index 0000000..3013a3f Binary files /dev/null and b/AdvancedVision/input/monsters.jpg differ diff --git a/AdvancedVision/input/monsters2.jpg b/AdvancedVision/input/monsters2.jpg new file mode 100644 index 0000000..7655643 Binary files /dev/null and b/AdvancedVision/input/monsters2.jpg differ diff --git a/AdvancedVision/input/rummikub.JPG b/AdvancedVision/input/rummikub.JPG new file mode 100644 index 0000000..5b7cca7 Binary files /dev/null and b/AdvancedVision/input/rummikub.JPG differ diff --git a/AdvancedVision/input/rummikub0.jpg b/AdvancedVision/input/rummikub0.jpg new file mode 100644 index 0000000..c329e4c Binary files /dev/null and b/AdvancedVision/input/rummikub0.jpg differ diff --git a/AdvancedVision/input/test.jpg b/AdvancedVision/input/test.jpg new file mode 100644 index 0000000..3525ac8 Binary files /dev/null and b/AdvancedVision/input/test.jpg differ diff --git a/AdvancedVision/input/test.png b/AdvancedVision/input/test.png new file mode 100644 index 0000000..79787f8 Binary files /dev/null and b/AdvancedVision/input/test.png differ diff --git a/AdvancedVision/input/test2.png b/AdvancedVision/input/test2.png new file mode 100644 index 0000000..eb6d297 Binary files /dev/null and b/AdvancedVision/input/test2.png differ diff --git a/AdvancedVision/input/test3.png b/AdvancedVision/input/test3.png new file mode 100644 index 0000000..8d7b71a Binary files /dev/null and b/AdvancedVision/input/test3.png differ diff --git a/AdvancedVision/input/test4.png b/AdvancedVision/input/test4.png new file mode 100644 index 0000000..e70bf39 Binary files /dev/null and b/AdvancedVision/input/test4.png differ diff --git a/AdvancedVision/input/test5.png b/AdvancedVision/input/test5.png new file mode 100644 index 0000000..2d0f775 Binary files /dev/null and b/AdvancedVision/input/test5.png differ diff --git a/AdvancedVision/input/testset.png b/AdvancedVision/input/testset.png new file mode 100644 index 0000000..cd0bb58 Binary files /dev/null and b/AdvancedVision/input/testset.png differ diff --git a/AdvancedVision/input/testset2.png b/AdvancedVision/input/testset2.png new file mode 100644 index 0000000..1992c15 Binary files /dev/null and b/AdvancedVision/input/testset2.png differ diff --git a/AdvancedVision/input/testset3.png b/AdvancedVision/input/testset3.png new file mode 100644 index 0000000..b29f2dd Binary files /dev/null and b/AdvancedVision/input/testset3.png differ diff --git a/AdvancedVision/output/testset_0.bmp b/AdvancedVision/output/testset_0.bmp new file mode 100644 index 0000000..bc9429f Binary files /dev/null and b/AdvancedVision/output/testset_0.bmp differ diff --git a/AdvancedVision/output/testset_1.bmp b/AdvancedVision/output/testset_1.bmp new file mode 100644 index 0000000..290ba3e Binary files /dev/null and b/AdvancedVision/output/testset_1.bmp differ diff --git a/AdvancedVision/output/testset_2.bmp b/AdvancedVision/output/testset_2.bmp new file mode 100644 index 0000000..6488367 Binary files /dev/null and b/AdvancedVision/output/testset_2.bmp differ diff --git a/AdvancedVision/output/testset_3.bmp b/AdvancedVision/output/testset_3.bmp new file mode 100644 index 0000000..4a6f3a3 Binary files /dev/null and b/AdvancedVision/output/testset_3.bmp differ diff --git a/AdvancedVision/output/testset_4.bmp b/AdvancedVision/output/testset_4.bmp new file mode 100644 index 0000000..8ae9909 Binary files /dev/null and b/AdvancedVision/output/testset_4.bmp differ diff --git a/AdvancedVision/output/testset_5.bmp b/AdvancedVision/output/testset_5.bmp new file mode 100644 index 0000000..86f2ebb Binary files /dev/null and b/AdvancedVision/output/testset_5.bmp differ diff --git a/AdvancedVision/output/testset_6.bmp b/AdvancedVision/output/testset_6.bmp new file mode 100644 index 0000000..cfec7ec Binary files /dev/null and b/AdvancedVision/output/testset_6.bmp differ diff --git a/AdvancedVision/output/testset_7.bmp b/AdvancedVision/output/testset_7.bmp new file mode 100644 index 0000000..8c4cee8 Binary files /dev/null and b/AdvancedVision/output/testset_7.bmp differ