Working algorithm

This commit is contained in:
2017-11-24 14:15:30 +01:00
parent acff1c02b1
commit 88c0a3e253
4 changed files with 56 additions and 52 deletions
@@ -1,7 +1,5 @@
#include "MooreBoundaryTrackingAlgorithm.h" #include "MooreBoundaryTrackingAlgorithm.h"
#include "avansvisionlib.h" #include "avansvisionlib.h"
#include <set>
//direction values //direction values
// starting at position 0. Definition of relative positions: // starting at position 0. Definition of relative positions:
@@ -11,44 +9,24 @@
int rotateX[] = { 0, 1, 1, 1, 0, -1, -1, -1 }; int rotateX[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
int rotateY[] = { -1, -1, 0, 1, 1, 1, 0, -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<vector<Point>>& contourVecVec) int allContours(Mat binaryImage, vector<vector<Point>>& contourVecVec)
{ {
Mat labeledImage;
vector<Point2d *> firstPixelVec2;
vector<Point2d *> posVec2;
vector<int> areaVec2;
int numberOfBlobs = labelBLOBsInfo(binaryImage, labeledImage, firstPixelVec2, posVec2, areaVec2);
Point b0, b1, c0, c1, firstb0, firstb1; Point b0, b1, c0, c1, firstb0, firstb1;
for (int y = 0; y < binaryImage.rows; y++) { for (Point2d *ptr : firstPixelVec2) {
for (int x = 0; x < binaryImage.cols; x++) vector<Point> contourVec;
{
Point p = Point(x, y);
if (getEntryImage(binaryImage, y, x) != 1) firstb0 = Point((int)ptr->y, (int)ptr->x);
{ b0 = Point((int)ptr->y, (int)ptr->x);
continue; c0 = Point((int)(ptr->y - 1), (int)ptr->x);
}
if (isInsideContours(contourVecVec, p)) int firstDirection = discoverNext(binaryImage, b0, 6);
{
continue;
}
vector<Point> contourVec;
firstb0 = p;
b0 = p;
c0 = Point(x - 1, y);
int firstDirection = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0));
b1 = getDirPos(b0, firstDirection); b1 = getDirPos(b0, firstDirection);
firstb1 = getDirPos(b0, firstDirection); firstb1 = getDirPos(b0, firstDirection);
c1 = getDirPos(b0, (firstDirection + 7) % 8); c1 = getDirPos(b0, (firstDirection + 7) % 8);
@@ -58,15 +36,14 @@ int allContours(Mat binaryImage, vector<vector<Point>>& contourVecVec)
do { do {
b0 = b1; b0 = Point(b1.x, b1.y);
c0 = c1; c0 = Point(c1.x, c1.y);
int cdir = discoverNextRelativeDirection(b0, c0); int dir = discoverNext(binaryImage, b0, discoverNextRelativeDirection(b0, c0));
int dir = discoverNext(binaryImage, b0, cdir);
if (dir < 0) 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); b1 = getDirPos(b0, dir);
@@ -78,17 +55,18 @@ int allContours(Mat binaryImage, vector<vector<Point>>& contourVecVec)
} while (b0 != firstb0 && b1 != firstb1); } 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); 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) { if (beginDirection < 0 || beginDirection > 8) {
cout << "Begin direction is invalid : " << beginDirection << endl; 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 newX = pos.x + rotateX[dir];
int newY = pos.y + rotateY[dir]; int newY = pos.y + rotateY[dir];
if (getEntryImage(binaryImage, newX, newY) == 1) { if (getEntryImage(binaryImage, newY, newX) == 1) {
return dir; return dir;
} }
} }
@@ -108,7 +86,7 @@ int discoverNext(Mat &binaryImage, cv::Point pos, int beginDirection)
return -1; 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++) { for (int i = 0; i < 8; i++) {
int dir = i; int dir = i;
@@ -123,7 +101,7 @@ int discoverNextRelativeDirection(cv::Point pos, cv::Point target)
return -1; return -1;
} }
cv::Point getDirPos(cv::Point pos, int dir) cv::Point getDirPos(const cv::Point &pos, int dir)
{ {
int x, y; int x, y;
x = pos.x + rotateX[dir]; x = pos.x + rotateX[dir];
@@ -169,6 +147,19 @@ bool isInsideContours(vector<vector<Point>>& vec, Point point)
} }
return false; return false;
}
double getBendingEnergy(const vector<Point>& contour)
} {
double energy = 0;
Point previousPoint = contour[contour.size() - 1];
for (Point p : contour)
{
energy += discoverNextRelativeDirection(previousPoint, p);
previousPoint = p;
}
return energy;
}
@@ -17,16 +17,18 @@ int allContours(Mat binaryImage, vector<vector<Point>> &contourVecVec);
// func: discover next b // func: discover next b
// return_value: new direction // 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 // func: discover next c
// return_value : new direction // 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 // func: get a point with a given position and direction
// return_value : new point // return_value : new point
cv::Point getDirPos(cv::Point pos, int dir); cv::Point getDirPos(const cv::Point &pos, int dir);
bool contains(vector<Point> vec, Point p); bool contains(vector<Point> vec, Point p);
void flip(vector<vector<Point>> &vec); void flip(vector<vector<Point>> &vec);
bool isInsideContours(vector<vector<Point>>& vec, Point point); bool isInsideContours(vector<vector<Point>>& vec, Point point);
double getBendingEnergy(const vector<Point> &contour);
@@ -101,6 +101,17 @@ int main(int argc, char *argv[])
imshow("Found contours", contourImage); imshow("Found contours", contourImage);
waitKey(0); 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; string pipo;
cin >> pipo; cin >> pipo;
Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 22 KiB