Commits

Anonymous committed e9d53d2

Adding wrapper classes for contour finding.

Comments (0)

Files changed (6)

Cosc428_NPR/contour.cpp

+#include "contour.h"
+
+#define NEXT 0
+#define PREV 1
+#define FIRST_CHILD 2
+#define PARENT 3
+
+int Contours::getIndex(int myIndex, int linkType) {
+    return this->hierarchy[myIndex][linkType];
+}
+
+void Contours::Find(Mat& image, int approximation) {
+    findContours(image, this->contours, this->hierarchy, CV_RETR_TREE, approximation);
+}
+
+Contours::Contour Contours::root() {
+    return Contour(this, 0);
+}
+
+Contours::Contour::Contour(Contours* container, int index) {
+    this->container = container;
+    this->index = index;
+}
+
+Contours::Contour::Contour(const Contour& c) {
+    this->container = c.container;
+    this->index = c.index;
+}
+
+Contours::Contour Contours::Contour::next() {
+    this->index = this->container->getIndex(this->index, NEXT);
+}
+
+Contours::Contour Contours::Contour::prev() {
+    this->index = this->container->getIndex(this->index, PREV);
+}
+
+Contours::Contour Contours::Contour::parent() {
+    this->index = this->container->getIndex(this->index, PARENT);
+}
+
+Contours::Contour Contours::Contour::firstChild() {
+    this->index = this->container->getIndex(this->index, FIRST_CHILD);
+}

Cosc428_NPR/contour.h

+#ifndef CONTOUR_H
+#define CONTOUR_H
+
+#include "highgui.h"
+#include "cv.h"
+
+using namespace cv;
+
+class Contours {
+private:
+    vector<vector<Point> > contours;
+    vector<Vec4i> hierarchy;
+    
+    int getIndex(int myIndex, int linkType); // linkType is NEXT, PREV, PARENT or FIRST_CHILD
+public:
+    class Contour {
+        Contours* container;
+        int index;
+        
+        Contour(Contours* container, int index);
+    public:
+        Contour(const Contour& c);
+        
+        Contour next();
+        Contour prev();
+        Contour parent();
+        Contour firstChild();
+        
+        friend class Contours;
+    };
+    friend class Contours::Contour;
+    
+    void Find(Mat& image, int approximation = CV_CHAIN_APPROX_SIMPLE);
+    Contour root();
+};
+
+
+#endif

Cosc428_NPR/hierarchy.cpp

-#include "hierarchy.h"
-
-Hierarchy::Hierarchy() {
-	this->current = 0;
-}
-
-Hierarchy::Hierarchy(Hierarchy& h) : hierarchy(h.hierarchy), contours(h.contours) {
-	this->current = 0;
-}
-
-Hierarchy::Hierarchy(const Hierarchy& h) : hierarchy(h.hierarchy), contours(h.contours) {
-	this->current = 0;
-}
-	
-Hierarchy::Hierarchy(vector<Vec4i>& hierarchy, vector<vector<Point> >& contours) : hierarchy(hierarchy), contours(contours) {
-	this->current = 0;
-	if (hierarchy.size() <= 0) {
-		throw 0;
-	}
-}
-
-void Hierarchy::add(vector<Vec4i>& hierarchy, vector<vector<Point> >& contours) {
-	this->hierarchy.insert(this->hierarchy.end(), hierarchy.begin(), hierarchy.end());
-	this->contours.insert(this->contours.end(), contours.begin(), contours.end());
-}
-
-void Hierarchy::add(Vec4i& hierarchyNode, vector<Point>& contour) {
-	this->hierarchy.insert(this->hierarchy.end(), hierarchyNode);
-	this->contours.insert(this->contours.end(), contour);
-}
-
-void Hierarchy::clear() {
-	this->hierarchy.clear();
-	this->contours.clear();
-}
-
-void Hierarchy::remove(int i) {
-	if (i = CURR_NODE) {
-		i = this->current;
-	}
-
-	vector<Vec4i>::iterator hiter = this->hierarchy.begin();
-	vector<vector<Point> >::iterator citer = this->contours.begin();
-	int j, k;
-
-	hiter += i;
-	citer += i;
-
-	int pnt = (*hiter)[PARENT];
-	int fch = (*hiter)[FCHILD];
-	int nxt = (*hiter)[NEXT];
-	int prv = (*hiter)[PREV];
-
-	// RETIE the children nodes
-	// if this is its parents first child
-	if (pnt > 0 && this->hierarchy[pnt][FCHILD] == i) {
-		if (fch >= 0) {
-			j = this->hierarchy[i][FCHILD];
-				
-			this->hierarchy[pnt][FCHILD] = j;
-
-			for (k = j; j >= 0; j = this->hierarchy[j][NEXT]) {
-				this->hierarchy[j][PARENT] = pnt;
-				k = j;
-			}
-				
-			this->hierarchy[k][NEXT] = this->hierarchy[i][NEXT];
-		} else {
-			if (nxt >= 0) {
-				this->hierarchy[pnt][FCHILD] = nxt;
-			} else if (prv >= 0) {
-				this->hierarchy[pnt][FCHILD] = prv;
-			} else {
-				this->hierarchy[pnt][FCHILD] = -1;
-			}
-		}
-	} else { // if it is not the parents first child
-		if (prv >= 0) {
-			this->hierarchy[prv][NEXT] = this->hierarchy[i][NEXT];
-		} 
-				
-		if (nxt >= 0) {
-			this->hierarchy[nxt][PREV] = this->hierarchy[i][PREV];
-		}
-	}
-
-	this->hierarchy.erase(hiter);
-	this->contours.erase(citer);
-
-	// Adjust for smaller size
-	if (i <= this->current && this->current > 0) {
-		this->current--;
-	}
-
-	// Adjust indexes for a lower size
-	for (k = 0; k < this->hierarchy.size(); k++) {
-		if (this->hierarchy[k][PARENT] == i || 
-			this->hierarchy[k][FCHILD] == i ||
-			this->hierarchy[k][NEXT] == i || 
-			this->hierarchy[k][PREV] == i) {
-			throw 0;
-		}
-
-		if (this->hierarchy[k][PARENT] > i) {
-			this->hierarchy[k][PARENT]--;
-		}
-		if (this->hierarchy[k][FCHILD] > i) {
-			this->hierarchy[k][FCHILD]--;
-		}
-		if (this->hierarchy[k][NEXT] > i) {
-			this->hierarchy[k][NEXT]--;
-		}
-		if (this->hierarchy[k][PREV] > i) {
-			this->hierarchy[k][PREV]--;
-		}
-	}
-}
-
-void Hierarchy::replaceContour(vector<Point> new_contour, int i) {
-	if (i == CURR_NODE) {
-		i = this->current;
-	}
-
-	this->contours[i].clear();
-	this->contours[i].insert(this->contours[i].begin(), new_contour.begin(), new_contour.end());
-}
-
-int Hierarchy::size() {
-	int h, c;
-
-	h = this->hierarchySize();
-	c = this->contourSize();
-
-	return (h < c)?h:c;
-}
-
-bool Hierarchy::hasParent() {
-	return hierarchy[current][PARENT] >= 0;
-}
-
-bool Hierarchy::hasNext() {
-	return hierarchy[current][NEXT] >= 0;
-}
-
-bool Hierarchy::hasPrev() {
-	return hierarchy[current][PREV] >= 0;
-}
-
-bool Hierarchy::hasChildren() {
-	return hierarchy[current][FCHILD] >= 0;
-}
-
-// getting
-int Hierarchy::getParent(int i) {
-	if (i < 0) {
-		i = current;
-	}
-
-	return hierarchy[current][PARENT];
-}
-
-int Hierarchy::getFirstChild(int i) {
-	if (i < 0) {
-		i = current;
-	}
-
-	return hierarchy[current][FCHILD];
-}
-
-int Hierarchy::getNext(int i) {
-	if (i < 0) {
-		i = current;
-	}
-
-	return hierarchy[current][NEXT];
-}
-
-int Hierarchy::getPrev(int i) {
-	if (i < 0) {
-		i = current;
-	}
-
-	return hierarchy[current][PREV];
-}
-
-int Hierarchy::getCurrent() {
-	return this->current;
-}
-
-int Hierarchy::getLast(int i) {
-	int j;
-
-	if (i == CURR_NODE) {
-		i = this->current;
-	}
-
-	for (j = i; this->hierarchy[j][NEXT] >= 0; j = this->hierarchy[j][NEXT]);
-
-	return j;
-}
-
-int Hierarchy::getFirst(int i) {
-	int j;
-
-	if (i == CURR_NODE) {
-		i = this->current;
-	}
-
-	for (j = i; this->hierarchy[j][PREV] >= 0; j = this->hierarchy[j][PREV]);
-
-	return j;
-}
-
-// traversal
-void Hierarchy::gotoNext() {
-	this->current = this->getNext();
-}
-
-void Hierarchy::gotoPrev() {
-	this->current = this->getPrev();
-}
-
-void Hierarchy::gotoFirstChild() {
-	this->current = this->getFirstChild();
-}
-
-void Hierarchy::gotoParent() {
-	this->current = this->getParent();
-}
-
-void Hierarchy::gotoRoot() {
-	this->current = 0;
-}
-
-int Hierarchy::moveTo(int current) {
-	return this->current = current;
-}
-
-int Hierarchy::getLevel(int i) {
-	int j;
-
-	if (i = CURR_NODE) {
-		i = this->current;
-	}
-
-	for (j = 0; this->hierarchy[i][PARENT] >= 0; j++) {
-		i = this->hierarchy[i][PARENT];
-	}
-
-	return j;
-}
-
-vector<Point> Hierarchy::getContour(int i) {
-	if (i == CURR_NODE) {
-		i = this->current;
-	}
-
-	return this->contours[i];
-}
-
-Vec4i Hierarchy::getHierarchyNode(int i) {
-	if (i == CURR_NODE) {
-		i = this->current;
-	}
-
-	return this->hierarchy[i];
-}
-
-vector<vector<Point> > Hierarchy::getContours() {
-	return this->contours;
-}
-
-vector<Vec4i> Hierarchy::getHierarchy() {
-	return this->hierarchy;
-}
-
-int Hierarchy::contourSize() {
-	return this->contours.size();
-}
-
-int Hierarchy::hierarchySize() {
-	return this->hierarchy.size();
-}

Cosc428_NPR/hierarchy.h

-#ifndef HIERARCHY_H
-#define HIERARCHY_H
-
-#include "highgui.h"
-#include "cv.h"
-
-using namespace cv;
-
-#define NEXT 0
-#define PREV 1
-#define FCHILD 2
-#define PARENT 3
-#define CURR_NODE -1
-
-class Hierarchy {
-	vector<Vec4i> hierarchy;
-	vector<vector<Point> > contours;
-	int current;
-
-public:
-	Hierarchy();
-	Hierarchy(Hierarchy& h);
-	Hierarchy(const Hierarchy& h);
-	Hierarchy(vector<Vec4i>& hierarchy, vector<vector<Point> >& contours);
-
-	void add(vector<Vec4i>& hierarchy, vector<vector<Point> >& contours);
-	void add(Vec4i& hierarchyNode, vector<Point>& contour);
-	void clear();
-	void remove(int i = CURR_NODE);
-	void replaceContour(vector<Point> new_contour, int i = CURR_NODE);
-	int size();
-
-	bool hasParent();
-	bool hasNext();
-	bool hasPrev();
-	bool hasChildren();
-
-	// getting
-	int getParent(int i = CURR_NODE);
-	int getFirstChild(int i = CURR_NODE);
-	int getNext(int i = CURR_NODE);
-	int getPrev(int i = CURR_NODE);
-	int getCurrent();
-
-	int getFirst(int i = CURR_NODE);
-	int getLast(int i = CURR_NODE);
-
-	// traversal
-	void gotoNext();
-	void gotoPrev();
-	void gotoFirstChild();
-	void gotoParent();
-	void gotoRoot();
-
-	int moveTo(int current);
-	int getLevel(int i = CURR_NODE);
-
-	vector<Point> getContour(int i = CURR_NODE);
-	Vec4i getHierarchyNode(int i = CURR_NODE);
-	vector<vector<Point> > getContours();
-	vector<Vec4i> getHierarchy();
-
-	int contourSize();
-	int hierarchySize();
-};
-
-#endif

Cosc428_NPR/start.cpp

 	cout << "finding plate candidates..." << endl;
 	//findPolygons(plate_cand, plate_candidates, 50, 11);
 	//findPolygons(plate_cand, plate_candidates, hierarchy, -1, 7);
-	findPolygons(plate_cand, plate_candidates, hierarchy, 50, 7);
+	findPolygons(hsv, plate_candidates, hierarchy, 50, 7);
 	cout << "found " << plate_candidates.size() << " polygons. filtering..." << endl;
 	
 	PolygonFilter tempFilter(4);
 BINDIR=bin/
 OBJDIR=obj/
 
-OBJECTS=$(SRCDIR)start.cpp $(OBJDIR)contours.o $(OBJDIR)hierarchy.o $(OBJDIR)image.o $(OBJDIR)segmentation.o $(OBJDIR)util.o $(OBJDIR)perspective.o
+OBJECTS=$(SRCDIR)start.cpp $(OBJDIR)contour.o $(OBJDIR)contours.o $(OBJDIR)image.o $(OBJDIR)segmentation.o $(OBJDIR)util.o $(OBJDIR)perspective.o
 
 $(BINDIR)$(PROG): $(OBJECTS)
 	$(CC) $(CFLAGS) $(CINCLUDES) $(CLIBPATH) $(CLIBS) $(OBJECTS) -o $(BINDIR)$(PROG)
 $(OBJDIR)contours.o: $(SRCDIR)contours.cpp $(SRCDIR)contours.h
 	$(CC) $(CINCLUDES) $(OBJFLAG) $(SRCDIR)contours.cpp -o $(OBJDIR)contours.o
 
-$(OBJDIR)hierarchy.o: $(SRCDIR)hierarchy.cpp $(SRCDIR)hierarchy.cpp
-	$(CC) $(CINCLUDES) $(OBJFLAG) $(SRCDIR)hierarchy.cpp -o $(OBJDIR)hierarchy.o
+$(OBJDIR)contour.o: $(SRCDIR)contour.cpp $(SRCDIR)contour.h
+	$(CC) $(CINCLUDES) $(OBJFLAG) $(SRCDIR)contour.cpp -o $(OBJDIR)contour.o
 
 $(OBJDIR)image.o: $(SRCDIR)image.cpp $(SRCDIR)image.h
 	$(CC) $(CINCLUDES) $(OBJFLAG) $(SRCDIR)image.cpp -o $(OBJDIR)image.o