Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnObject.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnObject.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <cassert>
17 #include <vector>
18 #include <map>
19 
20 #include "cv.h"
21 #include "cxcore.h"
22 
23 #include "drwnBase.h"
24 #include "drwnIO.h"
25 
26 using namespace std;
27 
28 // drwnObject ---------------------------------------------------------------
30 
31 class drwnObject {
32  public:
33  string name;
34  cv::Rect extent;
35  double score;
36  int ref;
37 
38  public:
40  drwnObject();
42  drwnObject(const cv::Rect& ext, const char *n = NULL);
43  //drwnObject(const vector<cv::Point>& polygon, const char *n = NULL);
45  drwnObject(const drwnObject& obj);
46  ~drwnObject();
47 
49  bool save(drwnXMLNode& xml) const;
51  bool load(drwnXMLNode& xml);
52 
54  bool hit(double x, double y) const;
56  bool hit(const cv::Point& pt) const { return hit(pt.x, pt.y); }
57 
59  double area() const { return fabs((double)(extent.width * extent.height)); }
61  double aspect() const { return (double)extent.width / (double)extent.height; }
62 
64  double overlap(const cv::Rect& roi) const;
66  double overlap(const drwnObject& obj) const { return overlap(obj.extent); }
67 
69  double areaOverlap(const cv::Rect& roi) const {
70  const double ov = overlap(roi);
71  return (ov / (area() + (roi.width * roi.height) - ov));
72  }
74  double areaOverlap(const drwnObject& obj) const { return areaOverlap(obj.extent); }
75 
77  bool inside(const drwnObject& obj) const {
78  return (fabs(overlap(obj) - area()) < DRWN_EPSILON);
79  }
80 
82  void scale(double x_scale, double y_scale) {
83  extent.x = (int)(x_scale * extent.x);
84  extent.y = (int)(y_scale * extent.y);
85  extent.width = (int)(x_scale * extent.width);
86  extent.height = (int)(y_scale * extent.height);
87  }
89  void scale(double sc) { scale(sc, sc); }
90 
91  drwnObject& operator=(const drwnObject& obj);
92  bool operator==(const drwnObject& obj) const;
93  bool operator!=(const drwnObject& obj) const { return !(*this == obj); }
94  bool operator<(const drwnObject& obj) const;
95  bool operator>(const drwnObject& obj) const;
96 };
97 
98 // drwnObjectList -----------------------------------------------------------
100 
101 class drwnObjectList : public drwnWriteable, public std::vector<drwnObject> {
102  public:
103  drwnObjectList();
104  ~drwnObjectList();
105 
106  // i/o
107  const char *type() const { return "drwnObjectList"; }
108  bool save(drwnXMLNode& xml) const;
109  bool load(drwnXMLNode& xml);
110 
112  void sort();
113 
114  // filtering
116  int removeNonMatching(const string& name);
118  int removeMatching(const string& name);
120  int nonMaximalSuppression(double threshold = 0.5, bool bByName = true);
122  int keepHighestScoring(int nKeep);
124  int keepAboveScore(double threshold);
125 
127 };
128 
129 // drwnObjectSequence -------------------------------------------------------
131 
132 class drwnObjectSequence : public drwnWriteable, public std::map<string, drwnObjectList> {
133  public:
136 
137  // i/o
138  const char *type() const { return "drwnObjectSequence"; }
139  bool save(drwnXMLNode& xml) const;
140  bool load(drwnXMLNode& xml);
141 };
142 
143 // utility functions
144 void printObjectStatistics(const drwnObjectSequence& objects);
double areaOverlap(const drwnObject &obj) const
return the area of intersection divided by the area of union
Definition: drwnObject.h:74
interface for objects that can serialize and de-serialize themselves
Definition: drwnInterfaces.h:48
List of objects for the same image (see drwnObject)
Definition: drwnObject.h:101
cv::Rect extent
object bounding box (in pixels)
Definition: drwnObject.h:34
double area(const cv::Rect &r)
area defined by the rectangle
Definition: drwnOpenCVUtils.h:76
double areaOverlap(const cv::Rect &roi) const
return the area of intersection divided by the area of union
Definition: drwnObject.h:69
void scale(double sc)
scale the object bounding box
Definition: drwnObject.h:89
bool inside(const drwnObject &obj) const
return true if obj is within the object
Definition: drwnObject.h:77
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnObject.h:138
double score
score (probability) for the object (higher is better)
Definition: drwnObject.h:35
bool operator<(const CvSize &r, const CvSize &s)
inequality operator for CvSize objects (allows partial sorting)
Definition: drwnOpenCVUtils.cpp:88
double overlap(const drwnObject &obj) const
return the number of pixels that overlap between two objects
Definition: drwnObject.h:66
double aspect() const
return the aspect ratio (width/height) of the object
Definition: drwnObject.h:61
double area() const
return the area of the object
Definition: drwnObject.h:59
int ref
external reference
Definition: drwnObject.h:36
void scale(double x_scale, double y_scale)
scale the object bounding box
Definition: drwnObject.h:82
Encapsulates a 2D object in an image for object detection.
Definition: drwnObject.h:31
Sequence of images, each with a list of objects (see drwnObjectList)
Definition: drwnObject.h:132
string name
name of the object category, e.g., "car"
Definition: drwnObject.h:33
bool hit(const cv::Point &pt) const
return true if the point pt is within the object
Definition: drwnObject.h:56
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnObject.h:107
bool operator==(const CvRect &r, const CvRect &s)
equality operator for CvRect objects
Definition: drwnOpenCVUtils.cpp:83