Skip to content

Commit 0e5eb4f

Browse files
committed
added holes to ContourFinder, closes #21
1 parent 70abc5f commit 0e5eb4f

File tree

8 files changed

+32
-7
lines changed

8 files changed

+32
-7
lines changed

example-contours-advanced/src/ofApp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ void ofApp::setup() {
1212
gui.setup();
1313
gui.add(threshold.set("Threshold", 128, 0, 255));
1414
gui.add(trackHs.set("Track Hue/Saturation", false));
15+
gui.add(holes.set("Holes", false));
1516
}
1617

1718
void ofApp::update() {
1819
cam.update();
1920
if(cam.isFrameNew()) {
2021
contourFinder.setTargetColor(targetColor, trackHs ? TRACK_COLOR_HS : TRACK_COLOR_RGB);
2122
contourFinder.setThreshold(threshold);
23+
contourFinder.setFindHoles(holes);
2224
contourFinder.findContours(cam);
2325
}
2426
}
@@ -91,11 +93,15 @@ void ofApp::draw() {
9193
ofScale(5, 5);
9294
ofDrawLine(0, 0, balance.x, balance.y);
9395
ofPopMatrix();
96+
97+
if(contourFinder.getHole(i)) {
98+
ofDrawBitmapStringHighlight("hole", center.x, center.y);
99+
}
94100
}
95101

96102
gui.draw();
97103

98-
ofTranslate(8, 75);
104+
ofTranslate(8, 90);
99105
ofFill();
100106
ofSetColor(0);
101107
ofDrawRectangle(-3, -3, 64+6, 64+6);

example-contours-advanced/src/ofApp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ class ofApp : public ofBaseApp {
1818
ofxPanel gui;
1919
ofParameter<float> threshold;
2020
ofParameter<bool> trackHs;
21+
ofParameter<bool> holes;
2122
};

example-contours-basic/src/ofApp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void ofApp::setup() {
99
gui.add(minArea.set("Min area", 10, 1, 100));
1010
gui.add(maxArea.set("Max area", 200, 1, 500));
1111
gui.add(threshold.set("Threshold", 128, 0, 255));
12+
gui.add(holes.set("Holes", false));
1213
}
1314

1415
void ofApp::update() {
@@ -18,6 +19,7 @@ void ofApp::update() {
1819
contourFinder.setMaxAreaRadius(maxArea);
1920
contourFinder.setThreshold(threshold);
2021
contourFinder.findContours(cam);
22+
contourFinder.setFindHoles(holes);
2123
}
2224
}
2325

example-contours-basic/src/ofApp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ class ofApp : public ofBaseApp {
1515

1616
ofxPanel gui;
1717
ofParameter<float> minArea, maxArea, threshold;
18+
ofParameter<bool> holes;
1819
};

libs/CLD/addons.make

Whitespace-only changes.

libs/ofxCv/addons.make

Whitespace-only changes.

libs/ofxCv/include/ofxCv/ContourFinder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ namespace ofxCv {
6464
cv::RotatedRect getMinAreaRect(unsigned int i) const;
6565
cv::Point2f getMinEnclosingCircle(unsigned int i, float& radius) const;
6666
cv::RotatedRect getFitEllipse(unsigned int i) const;
67-
std::vector<cv::Point> getFitQuad(unsigned int i) const;
67+
std::vector<cv::Point> getFitQuad(unsigned int i) const;
68+
bool getHole(unsigned int i) const;
6869
cv::Vec2f getVelocity(unsigned int i) const;
6970

7071
RectTracker& getTracker();
@@ -107,7 +108,8 @@ namespace ofxCv {
107108
std::vector<ofPolyline> polylines;
108109

109110
RectTracker tracker;
110-
std::vector<cv::Rect> boundingRects;
111+
std::vector<cv::Rect> boundingRects;
112+
std::vector<bool> holes;
111113

112114
int contourFindingMode;
113115
bool sortBySize;

libs/ofxCv/src/ContourFinder.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,19 @@ namespace ofxCv {
7575
bool needMaxFilter = maxAreaNorm ? (maxArea < 1) : (maxArea < numeric_limits<float>::infinity());
7676
vector<size_t> allIndices;
7777
vector<double> allAreas;
78+
vector<bool> allHoles;
7879
if(needMinFilter || needMaxFilter) {
7980
double imgArea = img.rows * img.cols;
8081
double imgMinArea = minAreaNorm ? (minArea * imgArea) : minArea;
8182
double imgMaxArea = maxAreaNorm ? (maxArea * imgArea) : maxArea;
8283
for(size_t i = 0; i < allContours.size(); i++) {
83-
double curArea = contourArea(Mat(allContours[i]));
84+
double curArea = contourArea(Mat(allContours[i]), true);
85+
bool hole = true;
86+
if(curArea < 0) {
87+
curArea = -curArea;
88+
hole = false;
89+
}
90+
allHoles.push_back(hole);
8491
allAreas.push_back(curArea);
8592
if((!needMinFilter || curArea >= imgMinArea) &&
8693
(!needMaxFilter || curArea <= imgMaxArea)) {
@@ -104,11 +111,13 @@ namespace ofxCv {
104111
// generate polylines and bounding boxes from the contours
105112
contours.clear();
106113
polylines.clear();
107-
boundingRects.clear();
114+
boundingRects.clear();
115+
holes.clear();
108116
for(size_t i = 0; i < allIndices.size(); i++) {
109117
contours.push_back(allContours[allIndices[i]]);
110118
polylines.push_back(toOf(contours[i]));
111119
boundingRects.push_back(boundingRect(contours[i]));
120+
holes.push_back(allHoles[allIndices[i]]);
112121
}
113122

114123
// track bounding boxes
@@ -248,12 +257,16 @@ namespace ofxCv {
248257
}
249258

250259
return quad;
251-
}
260+
}
261+
262+
bool ContourFinder::getHole(unsigned int i) const {
263+
return holes[i];
264+
}
252265

253266
cv::Vec2f ContourFinder::getVelocity(unsigned int i) const {
254267
return tracker.getVelocity(i);
255268
}
256-
269+
257270
unsigned int ContourFinder::getLabel(unsigned int i) const {
258271
return tracker.getCurrentLabels()[i];
259272
}

0 commit comments

Comments
 (0)