Direct Graphical Models  v.1.7.0
Global.cpp
1 #include "Global.h"
2 
3 namespace DirectGraphicalModels { namespace fex { namespace global
4 {
5 size_t getNumLines(const Mat &img, int threshold1, int threshold2)
6 {
7  // Converting to one channel image
8  Mat I;
9  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
10  else img.copyTo(I);
11 
12  GaussianBlur(I, I, Size(5, 5), 0.75, 0.75); // smooth it, otherwise a lot of false circles may be detected
13 
14  Mat canny8b;
15  Canny(I, canny8b, threshold1 / 2, threshold1, 3);
16 
17 #if 1
18  std::vector<Vec2f> vLines;
19  HoughLines( canny8b // image
20  , vLines // lines
21  , 1 // distance resolution
22  , 5 * CV_PI / 180 // angle resolution
23  , threshold2 // lower -> more lines
24  );
25 #else
26  std::vector<Vec4i> vLines;
27  HoughLinesP( canny8b // image
28  , vLines // lines
29  , 1 // distance resolution
30  , 1 * CV_PI / 180 // angle resolution
31  , threshold2 // lower -> more lines
32  , 2 // min line length
33  , canny8b.rows / 2 // max line length
34  );
35 #endif
36 
37 
38  if (false) { // Visualization
39  imshow("Canny", canny8b);
40 
41  Mat tmp;
42  if (img.channels() == 1) cvtColor(img, tmp, cv::ColorConversionCodes::COLOR_GRAY2RGB);
43  else img.copyTo(tmp);
44 
45 #if 1
46  for (Vec2f &l : vLines) {
47  float rho = l[0];
48  float theta = l[1];
49  double a = cos(theta);
50  double b = sin(theta);
51  double x0 = a * rho;
52  double y0 = b * rho;
53  Point pt1, pt2;
54  pt1.x = cvRound(x0 - 1000 * b);
55  pt1.y = cvRound(y0 + 1000 * a);
56  pt2.x = cvRound(x0 + 1000 * b);
57  pt2.y = cvRound(y0 - 1000 * a);
58  line(tmp, pt1, pt2, CV_RGB(255, 0, 0), 1, cv::LineTypes::LINE_AA);
59  }
60 #else
61  for (Vec4i &l : vLines)
62  line(tmp, Point(l[0], l[1]), Point(l[2], l[3]), CV_RGB(255, 0, 0), 1, cv::LineTypes::LINE_AA);
63 #endif
64  imshow("detected lines", tmp);
65  waitKey();
66  }
67 
68  return vLines.size();
69 }
70 
71 size_t getNumCircles(const Mat &img, int threshold1, int threshold2)
72 {
73  // Converting to one channel image
74  Mat I;
75  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
76  else img.copyTo(I);
77 
78  GaussianBlur(I, I, Size(9, 9), 2, 2); // smooth it, otherwise a lot of false circles may be detected
79 
80  std::vector<Vec3f> vCircles;
81  HoughCircles(I // image
82  , vCircles // circles
83  , cv::HoughModes::HOUGH_GRADIENT // method
84  , 1 // dp
85  , 1 // min distance between centers
86  , threshold1 // high treshold of the canny
87  , threshold2 // lower -> more circles
88  );
89 
90  if (false) { // Visualization
91  Mat canny8b;
92  Canny(I, canny8b, threshold1 / 2, threshold1, 3);
93  imshow("Canny", canny8b);
94 
95  Mat tmp;
96  if (img.channels() == 1) cvtColor(img, tmp, cv::ColorConversionCodes::COLOR_GRAY2RGB);
97  else img.copyTo(tmp);
98 
99  for (Vec3f &c : vCircles) {
100  Point center(cvRound(c[0]), cvRound(c[1]));
101  int radius = cvRound(c[2]);
102  circle(tmp, center, 3, CV_RGB(0, 255, 0), -1, 8, 0); // draw the circle center
103  circle(tmp, center, radius, CV_RGB(255, 0, 0), 1, 8, 0); // draw the circle outline
104  }
105  imshow("circles", tmp);
106  waitKey();
107  }
108 
109  return vCircles.size();
110 }
111 
112 float getOpacity(const Mat &img)
113 {
114  int width = img.cols;
115  int height = img.rows;
116  float R = -1.0f;
117 
118  // Converting to one channel image
119  Mat I;
120  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
121  else img.copyTo(I);
122 
123  float _mean = static_cast<float>(mean(I)[0]);
124  float res = 0.0f;
125 
126  for (int y = 0; y < height; y++) {
127  byte *pI = I.ptr<byte>(y);
128  for (int x = 0; x < width; x++) {
129  float dx = x - 0.5f * width;
130  float dy = y - 0.5f * height;
131  float dist = sqrtf(dx*dx + dy*dy);
132  if (R < 0) R = dist;
133  float weight = 1.0f - dist / R;
134  res += weight * fabs(static_cast<float>(pI[x]) - _mean);
135  } // x
136  } // y
137 
138  return res / (width * height);
139 }
140 
141 float getVariance(const Mat &img)
142 {
143  // Converting to one channel image
144  Mat I;
145  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
146  else img.copyTo(I);
147 
148  Scalar mean, stddev;
149  meanStdDev(I, mean, stddev);
150  float res = static_cast<float>(stddev[0] * stddev[0]);
151 
152  return res;
153 }
154 
155 int getArea(const Mat &img)
156 {
157  // Converting to one channel image
158  Mat I;
159  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
160  else img.copyTo(I);
161 
162  int res = 0;
163  for (int y = 0; y < I.rows; y++) {
164  byte *pI = I.ptr<byte>(y);
165  for (int x = 0; x < I.cols; x++)
166  if (pI[x] > 0) res++;
167  } // y
168 
169  return res;
170 }
171 
172 int getPerimeter(const Mat &img)
173 {
174  // Converting to one channel image
175  Mat I;
176  if (img.channels() != 1) cvtColor(img, I, cv::ColorConversionCodes::COLOR_RGB2GRAY);
177  else img.copyTo(I);
178 
179  Mat tmp(I.size(), CV_8UC1, Scalar(0));
180 
181  for (int y = 1; y < I.rows; y++) {
182  byte *pTmp = tmp.ptr<byte>(y);
183  byte *pI = I.ptr<byte>(y);
184  byte *pI1 = I.ptr<byte>(y - 1);
185  for (int x = 1; x < I.cols; x++)
186  if ((pI[x] != pI[x - 1]) || (pI[x] != pI1[x])) pTmp[x] = 255;
187  } // y
188 
189  imshow("tmp", tmp);
190 
191  return getArea(tmp);
192 }
193 
194 float getCompactness(const Mat &img)
195 {
196  float S = static_cast<float>(getArea(img));
197  float P = static_cast<float>(getPerimeter(img));
198  return (S > 0) ? P * P / (S * 4 * Pif) : 0;
199 }
200 
201 } } }
float getCompactness(const Mat &img)
Returns the compactness of the object in the source image.
Definition: Global.cpp:194
float getOpacity(const Mat &img)
Returns the weighted-mean transparancy of the source image.
Definition: Global.cpp:112
int getArea(const Mat &img)
Returns the number of non-zero pixels in the source image.
Definition: Global.cpp:155
int getPerimeter(const Mat &img)
Returns the perimeter of an object in the source image.
Definition: Global.cpp:172
float getVariance(const Mat &img)
Retunrs the variance of the source image.
Definition: Global.cpp:141
size_t getNumCircles(const Mat &img, int threshold1, int threshold2)
Returns the number of circles in the source image.
Definition: Global.cpp:71
size_t getNumLines(const Mat &img, int threshold1, int threshold2)
Returns the number of staight lines in the image.
Definition: Global.cpp:5