Niet werkende commit

This commit is contained in:
2017-11-29 15:48:37 +01:00
parent d7180b7db3
commit 89d739665b
30 changed files with 341 additions and 18 deletions
+89
View File
@@ -0,0 +1,89 @@
#include "BoundingBox.h"
#include <cmath>
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<vector<Point>> & contours, vector<vector<Point>> & bbs)
{
for (vector<Point> 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<Point> 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<vector<Point>> & 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<vector<Point>> bounds;
allBoundingBoxes(contours, bounds);
//Find largest bounding box
Size largest = Size(0,0);
for (vector<Point> 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<Point> 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;
}
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <string>
#include <limits>
using namespace std;
using namespace cv;
class BoundingBox
{
public:
BoundingBox();
~BoundingBox();
static int allBoundingBoxes(const vector<vector<Point>> & contours, vector<vector<Point>> & bbs);
static int split(const vector<vector<Point>> & contours, const Mat &image, string filename);
};
+174
View File
@@ -0,0 +1,174 @@
#include "FloodFill.h"
#include "BoundingBox.h"
FloodFill::FloodFill()
{
}
FloodFill::~FloodFill()
{
}
int FloodFill::enclosedPixels(const vector<Point>& contourVec, vector<Point> & regionPixels)
{
Point p = findStartPoint(contourVec);
regionPixels.push_back(p);
floodfill(contourVec, regionPixels, p);
return 1;
}
int FloodFill::floodfill(const vector<Point> & contourVec, vector<Point> & 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<Point> 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<Point> 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<vector<Point>> & 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<vector<Point>> regions;
regions.resize(contours.size());
for (int i = 0; i < contours.size(); i++)
{
vector<Point> temp;
enclosedPixels(contours[i], temp);
regions[i] = temp;
}
vector<vector<Point>> bounds;
BoundingBox::allBoundingBoxes(contours, bounds);
//Find largest bounding box
Size largest = Size(0, 0);
for (vector<Point> 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<uchar>(p) = image.at<uchar>(p);
}
for (Point p : contours[i])
{
resultImage.at<uchar>(p) = image.at<uchar>(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;
}
+23
View File
@@ -0,0 +1,23 @@
#pragma once
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
class FloodFill
{
public:
FloodFill();
~FloodFill();
static int enclosedPixels(const vector<Point> & contourVec, vector<Point> & regionPixels);
static bool contains(vector<Point> vec, Point p);
static Point findStartPoint(vector<Point> vec);
static int floodfill(const vector<Point> & contourVec, vector<Point> & regionPixels, Point &p);
static int split(const vector<vector<Point>> & contours, const Mat &image, string filename);
};
@@ -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<vector<Point>>& 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<vector<Point>>& contourVecVec)
contourVecVec.push_back(contourVec);
}
//flip(contourVecVec);
return numberOfBlobs; //number of objects
}
+33 -9
View File
@@ -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 <string>
#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<vector<Point>> 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<Point> 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
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 693 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB