MooreBoundaryTrackingAlgorithm init (not working yet)
This commit is contained in:
@@ -164,10 +164,12 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="allContoursTestprogramma.cpp" />
|
||||
<ClCompile Include="avansvisionlib.cpp" />
|
||||
<ClCompile Include="MooreBoundaryTrackingAlgorithm.cpp" />
|
||||
<ClCompile Include="Source.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="avansvisionlib.h" />
|
||||
<ClInclude Include="MooreBoundaryTrackingAlgorithm.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -27,10 +27,16 @@
|
||||
<ClCompile Include="avansvisionlib.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="MooreBoundaryTrackingAlgorithm.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="avansvisionlib.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="MooreBoundaryTrackingAlgorithm.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,101 @@
|
||||
#include "MooreBoundaryTrackingAlgorithm.h"
|
||||
#include "avansvisionlib.h"
|
||||
#include <set>
|
||||
|
||||
//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<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;
|
||||
|
||||
for (Point2d *ptr : firstPixelVec2) {
|
||||
set<Point> 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<Point> 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);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#include <opencv/cv.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
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<vector<Point>> &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);
|
||||
@@ -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)
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "avansvisionlib.h"
|
||||
#include "MooreBoundaryTrackingAlgorithm.h"
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
Reference in New Issue
Block a user