Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnOpenCVUtils.h
Go to the documentation of this file.
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: drwnOpenCVUtils.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
22 #pragma once
23 
24 #include <cstdlib>
25 #include <vector>
26 
27 #include "cv.h"
28 #include "highgui.h"
29 
30 #include "drwnBase.h"
31 
32 using namespace std;
33 using namespace Eigen;
34 
35 // Global parameters that can be set from the command line
36 namespace drwnOpenCVUtils {
38  extern unsigned SHOW_IMAGE_MAX_HEIGHT;
40  extern unsigned SHOW_IMAGE_MAX_WIDTH;
41 };
42 
43 // Convert opencv data structures to a string
44 string toString(const cv::Mat& m);
45 string toString(const vector<cv::Mat>& vm);
46 string toString(const cv::Rect& r);
47 string toString(const cv::Point& pt);
48 string toString(const cv::Size& sz);
49 string toString(const cv::Scalar& slr);
50 
51 // Operators
53 bool operator==(const CvRect& r, const CvRect& s);
55 bool operator<(const CvSize& r, const CvSize& s);
57 bool operator<(const CvPoint& p, const CvPoint& q);
58 
60 int drwnCmpCount(const cv::Mat& s, const cv::Mat& t, int cmpOp = CV_CMP_EQ);
61 
63 int drwnShowDebuggingImage(const cv::Mat& img, const std::string& name, bool bWait);
65 int drwnShowDebuggingImage(const vector<cv::Mat>& views, const std::string& name, bool bWait, int rows = -1);
66 
68 cv::Rect drwnFitBoundingBox(const vector<cv::Rect> &rects);
70 cv::Rect drwnFitBoundingBox(const vector<cv::Point> &points);
73 cv::Rect drwnFitBoundingBox(cv::Rect r, double aspectRatio);
74 
76 inline double area(const cv::Rect& r) { return (double)r.area(); }
78 inline double aspect(const cv::Rect& r) { return (double)r.width / (double)r.height; }
80 double areaOverlap(const cv::Rect& r, const cv::Rect& s);
82 inline double areaUnion(const cv::Rect& r, const cv::Rect& s) {
83  return area(r) + area(s) - areaOverlap(r, s);
84 }
85 
87 cv::Mat drwnGreyImage(const cv::Mat& src);
89 cv::Mat drwnColorImage(const cv::Mat& src);
91 cv::Mat drwnSoftEdgeMap(const cv::Mat& src, bool bNormalize = false);
93 void drwnGreyImageInplace(cv::Mat& img);
95 void drwnColorImageInplace(cv::Mat& img);
96 
99 cv::Mat drwnPixelwiseMean(const vector<cv::Mat>& imgStack);
103 cv::Mat drwnPixelwiseMedian(const vector<cv::Mat>& imgStack);
104 
106 cv::Mat drwnPadImage(const cv::Mat& src, int margin);
109 cv::Mat drwnPadImage(const cv::Mat& src, const cv::Rect& page);
110 
112 cv::Mat drwnTranslateMatrix(const cv::Mat& matrix, const cv::Point& origin,
113  double fillValue = 0.0);
114 
116 cv::Mat drwnRotateImage(const cv::Mat& img, float theta);
117 
119 void drwnScaleToRange(cv::Mat& m, double minValue = 0.0, double maxValue = 1.0);
121 void drwnResizeInPlace(cv::Mat& m, const cv::Size& size, int interpolation = CV_INTER_LINEAR);
123 void drwnResizeInPlace(cv::Mat& m, int rows, int cols, int interpolation = CV_INTER_LINEAR);
125 void drwnCropInPlace(cv::Mat& image, cv::Rect roi);
126 
128 bool drwnValidRect(const cv::Rect& r, int width, int height);
130 inline bool drwnValidRect(const cv::Rect& r, const cv::Mat& img) { return drwnValidRect(r, img.cols, img.rows); }
132 void drwnTruncateRect(cv::Rect& r, int width, int height);
134 inline void drwnTruncateRect(cv::Rect& r, const cv::Mat& img) { return drwnTruncateRect(r, img.cols, img.rows); }
135 
140 cv::Mat drwnCombineImages(const vector<cv::Mat>& images, int rows = -1, int cols = -1,
141  unsigned margin = 0, const cv::Scalar& colour = cv::Scalar(0));
142 
143 typedef enum {
149 } drwnColorMap;
150 
154 cv::Mat drwnCreateHeatMap(const cv::Mat& m, drwnColorMap cm = DRWN_COLORMAP_RAINBOW);
155 
158 cv::Mat drwnCreateHeatMap(const cv::Mat& m, cv::Scalar colourA, cv::Scalar colourB);
159 
162 cv::Mat drwnCreateHeatMap(const cv::Mat& m, const vector<cv::Scalar>& colours);
163 
165 vector<cv::Scalar> drwnCreateColorTable(unsigned n, drwnColorMap cm = DRWN_COLORMAP_RAINBOW);
166 
168 void drwnDrawBoundingBox(cv::Mat& canvas, const cv::Rect& roi, cv::Scalar fgcolor,
169  cv::Scalar bgcolor = CV_RGB(255, 255, 255), int lineWidth = 2);
170 
172 void drwnDrawPolygon(cv::Mat& canvas, const vector<cv::Point> &poly, cv::Scalar fgcolor,
173  cv::Scalar bgcolor = CV_RGB(255, 255, 255), int lineWidth = 2, bool bClose = true);
174 
176 void drwnDrawFullLinePlot(cv::Mat& canvas, const vector<double>& points,
177  const cv::Scalar& lineColour, unsigned lineWidth = 2,
178  const cv::Scalar& baseShading = CV_RGB(0, 0, 0), double baseAlpha = 0.0);
179 
181 void drwnDrawTarget(cv::Mat& canvas, const cv::Point& center,
182  cv::Scalar color = CV_RGB(255, 0, 0), int size = 5, int lineWidth = 1);
183 
187  public:
188  int event, x, y, flags;
189 
190  public:
191  drwnMouseState() : event(-1), x(0), y(0), flags(0) { /* do nothing */ }
192  ~drwnMouseState() { /* do nothing */ }
193 };
194 
197 void drwnOnMouse(int event, int x, int y, int flags, void *ptr);
198 
203 vector<cv::Point> drwnWaitMouse(const string& windowName, const cv::Mat& img,
204  int numPoints = DRWN_INT_MAX);
205 
207 cv::Rect drwnInputBoundingBox(const string& windowName, const cv::Mat& img);
208 
210 cv::Mat drwnInputScribble(const string& windowName, const cv::Mat& img,
211  const cv::Scalar& colour = CV_RGB(255, 0, 0), int width = 5);
212 
216 double drwnComparePatches(const cv::Mat& patchA, const cv::Mat& patchB, int method = CV_TM_SQDIFF);
217 
218 // Drawing functions
219 typedef enum {
223 } drwnFillType;
224 
226 void drwnOverlayImages(cv::Mat& canvas, const cv::Mat& overlay, double alpha = 0.5);
228 void drwnOverlayMask(cv::Mat& canvas, const cv::Mat& mask, const cv::Scalar& color, double alpha = 0.5);
230 void drwnShadeRectangle(cv::Mat& canvas, cv::Rect roi, const cv::Scalar& color,
231  double alpha = 0.5, drwnFillType fill = DRWN_FILL_SOLID, int thickness = 1);
233 void drwnShadeRegion(cv::Mat& canvas, const cv::Mat& mask, const cv::Scalar& color,
234  double alpha = 0.5, drwnFillType fill = DRWN_FILL_SOLID, int thickness = 1);
236 void drwnMaskRegion(cv::Mat& canvas, const cv::Mat& mask);
238 void drwnDrawRegionBoundaries(cv::Mat& canvas, const cv::Mat& mask,
239  const cv::Scalar& color, int thickness = 1);
241 void drwnDrawRegionBoundary(cv::Mat& canvas, const cv::Mat& mask,
242  int idRegionA, int idRegionB, const cv::Scalar& color, int thickness = 1);
246 void drwnAverageRegions(cv::Mat& img, const cv::Mat& seg);
double areaOverlap(const cv::Rect &r, const cv::Rect &s)
area of the intersection between two rectangles
Definition: drwnOpenCVUtils.cpp:200
void drwnColorImageInplace(cv::Mat &img)
convert an image to color in place
Definition: drwnOpenCVUtils.cpp:301
cv::Mat drwnPadImage(const cv::Mat &src, int margin)
pad an image and copy the boundary
Definition: drwnOpenCVUtils.cpp:354
fill with solid color
Definition: drwnOpenCVUtils.h:220
void drwnOverlayImages(cv::Mat &canvas, const cv::Mat &overlay, double alpha=0.5)
overlays an image on the (same size) canvas
Definition: drwnOpenCVUtils.cpp:1078
cv::Mat drwnColorImage(const cv::Mat &src)
convert image to color (8-bit)
Definition: drwnOpenCVUtils.cpp:244
int drwnCmpCount(const cv::Mat &s, const cv::Mat &t, int cmpOp=CV_CMP_EQ)
count number of entries matching comparison
Definition: drwnOpenCVUtils.cpp:98
ranges over ANU corporate colours
Definition: drwnOpenCVUtils.h:148
cv::Rect drwnInputBoundingBox(const string &windowName, const cv::Mat &img)
Waits for the user to input a bounding box by clicking two points on a canvas.
Definition: drwnOpenCVUtils.cpp:917
cv::Mat drwnCombineImages(const vector< cv::Mat > &images, int rows=-1, int cols=-1, unsigned margin=0, const cv::Scalar &colour=cv::Scalar(0))
Assemble images into one big image. All images must be of the same format and rows * cols must be sma...
Definition: drwnOpenCVUtils.cpp:515
void drwnDrawRegionBoundaries(cv::Mat &canvas, const cv::Mat &mask, const cv::Scalar &color, int thickness=1)
marks the boundary between regions
Definition: drwnOpenCVUtils.cpp:1181
void drwnResizeInPlace(cv::Mat &m, const cv::Size &size, int interpolation=CV_INTER_LINEAR)
resize an image in place
Definition: drwnOpenCVUtils.cpp:463
void drwnAverageRegions(cv::Mat &img, const cv::Mat &seg)
Fills each region with the average of the colour within the region. img can have an arbitrary number ...
Definition: drwnOpenCVUtils.cpp:1287
void drwnCropInPlace(cv::Mat &image, cv::Rect roi)
crop an image or matrix in place
Definition: drwnOpenCVUtils.cpp:480
vector< cv::Point > drwnWaitMouse(const string &windowName, const cv::Mat &img, int numPoints=DRWN_INT_MAX)
Waits for up to numPoints mouse clicks (or key press) and returns the location of the points...
Definition: drwnOpenCVUtils.cpp:858
double drwnComparePatches(const cv::Mat &patchA, const cv::Mat &patchB, int method=CV_TM_SQDIFF)
Compute distance between two image patches. method can be one of CV_TM_SQDIFF, CV_TM_SQDIFF_NORMED, CV_TM_CCORR, CV_TM_CCORR_NORMED, CV_TM_CCOEFF, or CV_TM_CCOEFF_NORMED.
Definition: drwnOpenCVUtils.cpp:1034
double area(const cv::Rect &r)
area defined by the rectangle
Definition: drwnOpenCVUtils.h:76
ranges from green to red
Definition: drwnOpenCVUtils.h:147
cv::Mat drwnSoftEdgeMap(const cv::Mat &src, bool bNormalize=false)
compute a soft edge map for an image (32-bit floating point)
Definition: drwnOpenCVUtils.cpp:268
void drwnOnMouse(int event, int x, int y, int flags, void *ptr)
Mouse callback function (populates MouseState data members passed as a pointer to void)...
Definition: drwnOpenCVUtils.cpp:848
ranges from cyan to pink
Definition: drwnOpenCVUtils.h:146
void drwnTruncateRect(cv::Rect &r, int width, int height)
truncates a rectangle to fit inside [0, 0, width - 1, height - 1]
Definition: drwnOpenCVUtils.cpp:498
cv::Mat drwnPixelwiseMean(const vector< cv::Mat > &imgStack)
Compute the pixelwise average of a stack of images. All images must be of the same size and type...
Definition: drwnOpenCVUtils.cpp:309
int drwnShowDebuggingImage(const cv::Mat &img, const std::string &name, bool bWait)
show an image (scale if not CV_8U) and wait (returns result from cv::waitKey)
Definition: drwnOpenCVUtils.cpp:106
void drwnShadeRegion(cv::Mat &canvas, const cv::Mat &mask, const cv::Scalar &color, double alpha=0.5, drwnFillType fill=DRWN_FILL_SOLID, int thickness=1)
draws a shaded region on the canvas
Definition: drwnOpenCVUtils.cpp:1123
double areaUnion(const cv::Rect &r, const cv::Rect &s)
area of the union between two rectangles
Definition: drwnOpenCVUtils.h:82
ranges from blue to red through green and yellow
Definition: drwnOpenCVUtils.h:144
bool drwnValidRect(const cv::Rect &r, int width, int height)
returns true if the rectangle has non-zero size and fits within the given image dimensions ...
Definition: drwnOpenCVUtils.cpp:491
cv::Mat drwnPixelwiseMedian(const vector< cv::Mat > &imgStack)
Compute the pixelwise median of a stack of images. All images must be of the same size and of type CV...
Definition: drwnOpenCVUtils.cpp:327
void drwnGreyImageInplace(cv::Mat &img)
convert an image to 32-bit greyscale in place
Definition: drwnOpenCVUtils.cpp:294
Mouse state and mouse callback for populating the mouse state. Used by the drwnWaitMouse function...
Definition: drwnOpenCVUtils.h:186
vector< cv::Scalar > drwnCreateColorTable(unsigned n, drwnColorMap cm=DRWN_COLORMAP_RAINBOW)
Creates a color table by uniformly sampling a colormap.
Definition: drwnOpenCVUtils.cpp:746
cv::Rect drwnFitBoundingBox(const vector< cv::Rect > &rects)
finds the smallest bounding box around these rectangles
Definition: drwnOpenCVUtils.cpp:156
cv::Mat drwnTranslateMatrix(const cv::Mat &matrix, const cv::Point &origin, double fillValue=0.0)
translate an array
Definition: drwnOpenCVUtils.cpp:419
fill with crosshatching
Definition: drwnOpenCVUtils.h:222
bool operator<(const CvSize &r, const CvSize &s)
inequality operator for CvSize objects (allows partial sorting)
Definition: drwnOpenCVUtils.cpp:88
cv::Mat drwnRotateImage(const cv::Mat &img, float theta)
rotate an image clockwise by theta
Definition: drwnOpenCVUtils.cpp:434
void drwnMaskRegion(cv::Mat &canvas, const cv::Mat &mask)
applies a soft mask (in range [0, 1]) to a region on the canvas
Definition: drwnOpenCVUtils.cpp:1165
void drwnDrawPolygon(cv::Mat &canvas, const vector< cv::Point > &poly, cv::Scalar fgcolor, cv::Scalar bgcolor=CV_RGB(255, 255, 255), int lineWidth=2, bool bClose=true)
draws a pretty polygon
Definition: drwnOpenCVUtils.cpp:774
ranges from red to yellow
Definition: drwnOpenCVUtils.h:145
cv::Mat drwnGreyImage(const cv::Mat &src)
convert image to greyscale (32-bit floating point)
Definition: drwnOpenCVUtils.cpp:218
void drwnScaleToRange(cv::Mat &m, double minValue=0.0, double maxValue=1.0)
scale all entries in the image/matrix to the given range
Definition: drwnOpenCVUtils.cpp:448
drwnColorMap
Definition: drwnOpenCVUtils.h:143
void drwnDrawRegionBoundary(cv::Mat &canvas, const cv::Mat &mask, int idRegionA, int idRegionB, const cv::Scalar &color, int thickness=1)
marks the boundary between two specific regions
Definition: drwnOpenCVUtils.cpp:1234
cv::Mat drwnCreateHeatMap(const cv::Mat &m, drwnColorMap cm=DRWN_COLORMAP_RAINBOW)
Convert a floating point matrix with entries in range [0, 1] to color image heatmap in either rainbow...
Definition: drwnOpenCVUtils.cpp:557
void drwnDrawFullLinePlot(cv::Mat &canvas, const vector< double > &points, const cv::Scalar &lineColour, unsigned lineWidth=2, const cv::Scalar &baseShading=CV_RGB(0, 0, 0), double baseAlpha=0.0)
draws a line across the image, optionally filling below the line
Definition: drwnOpenCVUtils.cpp:797
double aspect(const cv::Rect &r)
aspect ratio defined by the rectangle
Definition: drwnOpenCVUtils.h:78
fill with diagonal stripes
Definition: drwnOpenCVUtils.h:221
drwnFillType
Definition: drwnOpenCVUtils.h:219
void drwnDrawBoundingBox(cv::Mat &canvas, const cv::Rect &roi, cv::Scalar fgcolor, cv::Scalar bgcolor=CV_RGB(255, 255, 255), int lineWidth=2)
draws a pretty bounding box
Definition: drwnOpenCVUtils.cpp:763
void drwnShadeRectangle(cv::Mat &canvas, cv::Rect roi, const cv::Scalar &color, double alpha=0.5, drwnFillType fill=DRWN_FILL_SOLID, int thickness=1)
draws a shaded rectangle on the canvas
Definition: drwnOpenCVUtils.cpp:1109
void drwnOverlayMask(cv::Mat &canvas, const cv::Mat &mask, const cv::Scalar &color, double alpha=0.5)
overlays a soft mask (in range [0, 1]) on the (same size) canvas
Definition: drwnOpenCVUtils.cpp:1089
void drwnDrawTarget(cv::Mat &canvas, const cv::Point &center, cv::Scalar color=CV_RGB(255, 0, 0), int size=5, int lineWidth=1)
draws a target symbol (circle and cross-hairs)
Definition: drwnOpenCVUtils.cpp:835
bool operator==(const CvRect &r, const CvRect &s)
equality operator for CvRect objects
Definition: drwnOpenCVUtils.cpp:83
cv::Mat drwnInputScribble(const string &windowName, const cv::Mat &img, const cv::Scalar &colour=CV_RGB(255, 0, 0), int width=5)
Allows a user to scribble on a canvas. Exits when they press a key.
Definition: drwnOpenCVUtils.cpp:984