SMACC2
Loading...
Searching...
No Matches
cl_rgb Namespace Reference

Namespaces

namespace  anonymous_namespace{box_fit.cpp}
 
namespace  anonymous_namespace{colour.cpp}
 
namespace  anonymous_namespace{table_survey.cpp}
 

Classes

struct  Blob
 
struct  BlobParams
 
struct  BoxFit
 
struct  BoxFitParams
 
struct  CameraParams
 
class  CbRgbClientBehaviorBase
 
class  CbSurveyTable
 
class  CbWaitForFrame
 
class  ClRgb
 
class  CpCamera
 
class  CpSceneSurvey
 
class  CpTablePlane
 
class  CpTableSegmenter
 
class  CpTransforms
 
struct  Frame
 
struct  NamedColour
 
struct  PinholeCamera
 
struct  RgbParams
 
struct  SegmenterParams
 
struct  Survey
 
struct  SurveyTableParams
 
struct  TableParams
 
struct  WaitForFrameParams
 

Typedefs

using FramePtr = std::shared_ptr<const Frame>
 
using SurveyPtr = std::shared_ptr<const Survey>
 

Functions

std::optional< BoxFit > fitBoxOnTable (const Blob &blob, const PinholeCamera &camera, double tableZ, const BoxFitParams &params)
 
const std::vector< NamedColour > & basicColours ()
 
std::string colourName (const cv::Vec3d &lab)
 
double colourCost (const cv::Vec3d &lab, const std::string &name)
 
std::vector< int > matchColours (const std::vector< cv::Vec3d > &seen, const std::vector< std::string > &names)
 
cv::Mat tableRegionMask (const PinholeCamera &camera, const cv::Size &size, const TableParams &table)
 
cv::Mat segmentForeground (const cv::Mat &bgr, const cv::Mat &tableMask, const SegmenterParams &params)
 
cv::Mat segmentAgainstReference (const cv::Mat &bgr, const cv::Mat &referenceBgr, const cv::Mat &tableMask, const SegmenterParams &params)
 
std::vector< Blob > findBlobs (const cv::Mat &bgr, const cv::Mat &foreground, const PinholeCamera &camera, const TableParams &table, const BlobParams &params)
 

Typedef Documentation

◆ FramePtr

using cl_rgb::FramePtr = std::shared_ptr<const Frame>

Definition at line 39 of file types.hpp.

◆ SurveyPtr

using cl_rgb::SurveyPtr = std::shared_ptr<const Survey>

Definition at line 50 of file types.hpp.

Function Documentation

◆ basicColours()

const std::vector< NamedColour > & cl_rgb::basicColours ( )

Definition at line 23 of file colour.cpp.

24{
25 static const std::vector<NamedColour> colours = {
26 {"red", {53.2, 80.1, 67.2}}, {"orange", {74.9, 23.9, 78.9}},
27 {"yellow", {97.1, -21.6, 94.5}}, {"green", {87.7, -86.2, 83.2}},
28 {"cyan", {91.1, -48.1, -14.1}}, {"blue", {32.3, 79.2, -107.9}},
29 {"purple", {29.8, 58.9, -36.5}}, {"magenta", {60.3, 98.3, -60.8}},
30 {"pink", {83.6, 24.1, 3.3}}, {"brown", {37.5, 26.4, 40.9}},
31 };
32 return colours;
33}

Referenced by colourCost(), and colourName().

Here is the caller graph for this function:

◆ colourCost()

double cl_rgb::colourCost ( const cv::Vec3d & lab,
const std::string & name )

Definition at line 56 of file colour.cpp.

57{
58 for (const auto & colour : basicColours())
59 {
60 if (colour.name != name)
61 {
62 continue;
63 }
64 const double chroma = std::hypot(lab[1], lab[2]);
65 double difference =
66 std::abs(std::atan2(lab[2], lab[1]) - std::atan2(colour.lab[2], colour.lab[1]));
67 if (difference > CV_PI)
68 {
69 difference = 2.0 * CV_PI - difference;
70 }
71 // Hue decides; lightness and chroma separate the words that share a hue (orange and
72 // brown, red and pink, blue and purple).
73 return difference * 60.0 + 0.5 * std::abs(lab[0] - colour.lab[0]) +
74 0.25 * std::abs(chroma - std::hypot(colour.lab[1], colour.lab[2]));
75 }
76 return std::numeric_limits<double>::infinity();
77}
const std::vector< NamedColour > & basicColours()
Definition colour.cpp:23

References basicColours().

Referenced by colourName(), and matchColours().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ colourName()

std::string cl_rgb::colourName ( const cv::Vec3d & lab)

Definition at line 35 of file colour.cpp.

36{
37 const double chroma = std::hypot(lab[1], lab[2]);
38 if (chroma < 12.0)
39 {
40 return lab[0] < 25.0 ? "black" : (lab[0] > 75.0 ? "white" : "gray");
41 }
42 std::string best;
43 double bestCost = std::numeric_limits<double>::max();
44 for (const auto & colour : basicColours())
45 {
46 const double cost = colourCost(lab, colour.name);
47 if (cost < bestCost)
48 {
49 bestCost = cost;
50 best = colour.name;
51 }
52 }
53 return best;
54}

References basicColours(), and colourCost().

Here is the call graph for this function:

◆ findBlobs()

std::vector< Blob > cl_rgb::findBlobs ( const cv::Mat & bgr,
const cv::Mat & foreground,
const PinholeCamera & camera,
const TableParams & table,
const BlobParams & params )

Definition at line 188 of file table_survey.cpp.

191{
192 cv::Mat floatImage, lab;
193 bgr.convertTo(floatImage, CV_32FC3, 1.0 / 255.0);
194 cv::cvtColor(floatImage, lab, cv::COLOR_BGR2Lab);
195
196 cv::Mat regions = foreground;
197 if (params.colourEdgeHueDeg > 0.0)
198 {
199 regions = foreground.clone();
200 for (int r = 0; r < lab.rows; ++r)
201 {
202 for (int c = 0; c < lab.cols; ++c)
203 {
204 if (!foreground.at<uchar>(r, c))
205 {
206 continue;
207 }
208 const cv::Vec3f here = lab.at<cv::Vec3f>(r, c);
209 // the pixel two to the right, and the pixel two below
210 for (int k = 0; k < 2; ++k)
211 {
212 const int nr = k == 0 ? r : r + 2;
213 const int nc = k == 0 ? c + 2 : c;
214 if (nr >= lab.rows || nc >= lab.cols || !foreground.at<uchar>(nr, nc))
215 {
216 continue;
217 }
218 const cv::Vec3f there = lab.at<cv::Vec3f>(nr, nc);
219 if (
220 std::hypot(here[1], here[2]) < params.colourEdgeMinChroma ||
221 std::hypot(there[1], there[2]) < params.colourEdgeMinChroma)
222 {
223 continue;
224 }
225 double turn = std::abs(std::atan2(there[2], there[1]) - std::atan2(here[2], here[1]));
226 turn = turn > CV_PI ? 2.0 * CV_PI - turn : turn;
227 if (turn * 180.0 / CV_PI > params.colourEdgeHueDeg)
228 {
229 // the pixel between the two is the seam
230 regions.at<uchar>((r + nr) / 2, (c + nc) / 2) = 0;
231 }
232 }
233 }
234 }
235 }
236
237 cv::Mat labels, stats, centroids;
238 const int count = cv::connectedComponentsWithStats(
239 regions, labels, stats, centroids, params.colourEdgeHueDeg > 0.0 ? 4 : 8);
240 const cv::Matx33d H = camera.imageToPlaneHomography(table.height);
241
242 std::vector<Blob> blobs;
243 for (int i = 1; i < count; ++i)
244 {
245 const int area = stats.at<int>(i, cv::CC_STAT_AREA);
246 if (area < params.minAreaPx)
247 {
248 continue;
249 }
250 Blob blob;
251 blob.areaPx = area;
252 blob.box = cv::Rect(
253 stats.at<int>(i, cv::CC_STAT_LEFT), stats.at<int>(i, cv::CC_STAT_TOP),
254 stats.at<int>(i, cv::CC_STAT_WIDTH), stats.at<int>(i, cv::CC_STAT_HEIGHT));
255 blob.centroidPx = cv::Point2d(centroids.at<double>(i, 0), centroids.at<double>(i, 1));
256 blob.mask = (labels(blob.box) == i);
257 blob.crop = bgr(blob.box).clone();
258 const cv::Scalar meanBgr = cv::mean(bgr(blob.box), blob.mask);
259 const cv::Scalar meanLab = cv::mean(lab(blob.box), blob.mask);
260 blob.meanBgr = cv::Vec3d(meanBgr[0], meanBgr[1], meanBgr[2]);
261 blob.meanLab = cv::Vec3d(meanLab[0], meanLab[1], meanLab[2]);
262
263 if (const auto point = camera.pixelToPlane(blob.centroidPx.x, blob.centroidPx.y, table.height))
264 {
265 blob.tablePoint = *point;
266 }
267
268 // The blob's pixels on the table plane: its footprint as this camera sees it.
269 std::vector<cv::Point2f> footprint;
270 footprint.reserve(static_cast<size_t>(area));
271 double nearest = std::numeric_limits<double>::max();
272 for (int r = 0; r < blob.box.height; ++r)
273 {
274 const uchar * m = blob.mask.ptr<uchar>(r);
275 for (int c = 0; c < blob.box.width; ++c)
276 {
277 if (!m[c])
278 {
279 continue;
280 }
281 const cv::Vec3d p = H * cv::Vec3d(blob.box.x + c, blob.box.y + r, 1.0);
282 if (std::abs(p[2]) < 1e-12)
283 {
284 continue;
285 }
286 const cv::Point2f onTable(static_cast<float>(p[0] / p[2]), static_cast<float>(p[1] / p[2]));
287 footprint.push_back(onTable);
288 const double dx = onTable.x - camera.position[0];
289 const double dy = onTable.y - camera.position[1];
290 const double distance = dx * dx + dy * dy;
291 if (distance < nearest)
292 {
293 nearest = distance;
294 blob.nearPoint = cv::Vec3d(onTable.x, onTable.y, table.height);
295 }
296 }
297 }
298 if (footprint.size() >= 3)
299 {
300 const cv::RotatedRect extent = cv::minAreaRect(footprint);
301 const bool widthIsLong = extent.size.width >= extent.size.height;
302 blob.lengthM = widthIsLong ? extent.size.width : extent.size.height;
303 blob.widthM = widthIsLong ? extent.size.height : extent.size.width;
304 double angle = extent.angle * CV_PI / 180.0 + (widthIsLong ? 0.0 : CV_PI / 2.0);
305 while (angle >= CV_PI / 2.0) angle -= CV_PI;
306 while (angle < -CV_PI / 2.0) angle += CV_PI;
307 blob.axisRad = angle;
308 }
309 blobs.push_back(std::move(blob));
310 }
311
312 std::sort(
313 blobs.begin(), blobs.end(), [](const Blob & a, const Blob & b) { return a.areaPx > b.areaPx; });
314 if (static_cast<int>(blobs.size()) > params.maxBlobs)
315 {
316 blobs.resize(static_cast<size_t>(params.maxBlobs));
317 }
318 for (size_t i = 0; i < blobs.size(); ++i)
319 {
320 blobs[i].id = static_cast<int>(i);
321 }
322 return blobs;
323}
std::optional< cv::Vec3d > pixelToPlane(double u, double v, double z) const
Definition geometry.cpp:35
cv::Matx33d imageToPlaneHomography(double z) const
Definition geometry.cpp:50

References cl_rgb::Blob::areaPx, cl_rgb::Blob::axisRad, cl_rgb::Blob::box, cl_rgb::Blob::centroidPx, cl_rgb::BlobParams::colourEdgeHueDeg, cl_rgb::BlobParams::colourEdgeMinChroma, cl_rgb::Blob::crop, cl_rgb::TableParams::height, cl_rgb::PinholeCamera::imageToPlaneHomography(), cl_rgb::Blob::lengthM, cl_rgb::Blob::mask, cl_rgb::BlobParams::maxBlobs, cl_rgb::Blob::meanBgr, cl_rgb::Blob::meanLab, cl_rgb::BlobParams::minAreaPx, cl_rgb::Blob::nearPoint, cl_rgb::PinholeCamera::pixelToPlane(), cl_rgb::PinholeCamera::position, cl_rgb::Blob::tablePoint, and cl_rgb::Blob::widthM.

Referenced by cl_rgb::CbSurveyTable::onEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fitBoxOnTable()

std::optional< BoxFit > cl_rgb::fitBoxOnTable ( const Blob & blob,
const PinholeCamera & camera,
double tableZ,
const BoxFitParams & params )

Definition at line 130 of file box_fit.cpp.

132{
133 if (seen.mask.empty() || seen.areaPx <= 0)
134 {
135 return std::nullopt;
136 }
137 const Blob blob = strongColourPart(seen, params.minChromaFraction);
138 // the centroid of the outline lies near the box's centre at half its height
139 const auto guess =
140 camera.pixelToPlane(blob.centroidPx.x, blob.centroidPx.y, tableZ + params.sizeM[2] / 2.0);
141 if (!guess)
142 {
143 return std::nullopt;
144 }
145 const bool square = std::abs(params.sizeM[0] - params.sizeM[1]) < 1e-6;
146 const double yawEnd = square ? CV_PI / 2.0 : CV_PI;
147 const int steps = static_cast<int>(std::round(params.searchM / params.stepM));
148
149 BoxFit best;
150 best.overlap = -1.0;
151 for (const bool topOnly : {false, true})
152 {
153 if (topOnly && best.overlap >= params.tryTopBelow)
154 {
155 break;
156 }
157 // the centroid of the top face lies at the box's centre at its full height
158 const auto centre =
159 topOnly ? camera.pixelToPlane(blob.centroidPx.x, blob.centroidPx.y, tableZ + params.sizeM[2])
160 : guess;
161 if (!centre)
162 {
163 continue;
164 }
165 for (double yaw = 0.0; yaw < yawEnd - 1e-9; yaw += params.yawStepRad)
166 {
167 for (int ix = -steps; ix <= steps; ++ix)
168 {
169 for (int iy = -steps; iy <= steps; ++iy)
170 {
171 const double x = (*centre)[0] + ix * params.stepM;
172 const double y = (*centre)[1] + iy * params.stepM;
173 const cv::Mat drawn = outline(camera, blob.box, x, y, yaw, tableZ, params.sizeM, topOnly);
174 if (drawn.empty())
175 {
176 continue;
177 }
178 const double both = cv::countNonZero(drawn & blob.mask);
179 // the outline's full area, so that a box partly outside the window is not favoured
180 const double whole =
181 std::max(hullArea(camera, x, y, yaw, tableZ, params.sizeM, topOnly), 1.0);
182 const double overlap = both / (whole + blob.areaPx - both);
183 if (overlap > best.overlap)
184 {
185 best = BoxFit{x, y, yaw, overlap, topOnly};
186 }
187 }
188 }
189 }
190 }
191 if (best.overlap < params.minOverlap)
192 {
193 return std::nullopt;
194 }
195 return best;
196}
double hullArea(const PinholeCamera &camera, double x, double y, double yaw, double tableZ, const cv::Vec3d &size, bool topOnly)
Definition box_fit.cpp:54
cv::Mat outline(const PinholeCamera &camera, const cv::Rect &box, double x, double y, double yaw, double tableZ, const cv::Vec3d &size, bool topOnly)
Definition box_fit.cpp:28

References cl_rgb::Blob::areaPx, cl_rgb::Blob::box, cl_rgb::Blob::centroidPx, cl_rgb::Blob::mask, cl_rgb::BoxFitParams::minChromaFraction, cl_rgb::BoxFitParams::minOverlap, cl_rgb::BoxFit::overlap, cl_rgb::PinholeCamera::pixelToPlane(), cl_rgb::BoxFitParams::searchM, cl_rgb::BoxFitParams::sizeM, cl_rgb::BoxFitParams::stepM, cl_rgb::BoxFitParams::tryTopBelow, and cl_rgb::BoxFitParams::yawStepRad.

Here is the call graph for this function:

◆ matchColours()

std::vector< int > cl_rgb::matchColours ( const std::vector< cv::Vec3d > & seen,
const std::vector< std::string > & names )

Definition at line 108 of file colour.cpp.

110{
111 if (seen.size() < names.size() || names.empty())
112 {
113 return {};
114 }
115 std::vector<std::vector<double>> cost(names.size(), std::vector<double>(seen.size(), 0.0));
116 for (size_t n = 0; n < names.size(); ++n)
117 {
118 for (size_t i = 0; i < seen.size(); ++i)
119 {
120 cost[n][i] = colourCost(seen[i], names[n]);
121 }
122 }
123 std::vector<int> current(names.size(), -1);
124 std::vector<int> best;
125 std::vector<bool> used(seen.size(), false);
126 double bestSum = std::numeric_limits<double>::max();
127 assign(cost, 0, 0.0, current, used, bestSum, best);
128 return best;
129}
void assign(const std::vector< std::vector< double > > &cost, size_t name, double sum, std::vector< int > &current, std::vector< bool > &used, double &bestSum, std::vector< int > &best)
Definition colour.cpp:81
double colourCost(const cv::Vec3d &lab, const std::string &name)
Definition colour.cpp:56

References colourCost().

Here is the call graph for this function:

◆ segmentAgainstReference()

cv::Mat cl_rgb::segmentAgainstReference ( const cv::Mat & bgr,
const cv::Mat & referenceBgr,
const cv::Mat & tableMask,
const SegmenterParams & params )

Definition at line 129 of file table_survey.cpp.

132{
133 cv::Mat image, reference, imageLab, referenceLab;
134 bgr.convertTo(image, CV_32FC3, 1.0 / 255.0);
135 referenceBgr.convertTo(reference, CV_32FC3, 1.0 / 255.0);
136 cv::cvtColor(image, imageLab, cv::COLOR_BGR2Lab);
137 cv::cvtColor(reference, referenceLab, cv::COLOR_BGR2Lab);
138
139 std::vector<cv::Mat> difference;
140 cv::split(imageLab - referenceLab, difference);
141 difference[0] *= params.referenceLightnessWeight;
142 cv::Mat distanceSquared = difference[0].mul(difference[0]) + difference[1].mul(difference[1]) +
143 difference[2].mul(difference[2]);
144
145 cv::Mat foreground = (distanceSquared > params.referenceThreshold * params.referenceThreshold);
146 cv::bitwise_and(foreground, tableMask, foreground);
147 if (params.shadowDarkest > 0.0)
148 {
149 for (int r = 0; r < foreground.rows; ++r)
150 {
151 for (int c = 0; c < foreground.cols; ++c)
152 {
153 if (!foreground.at<uchar>(r, c))
154 {
155 continue;
156 }
157 const cv::Vec3f seen = image.at<cv::Vec3f>(r, c);
158 const cv::Vec3f empty = reference.at<cv::Vec3f>(r, c);
159 float least = 10.0F, most = 0.0F;
160 for (int k = 0; k < 3; ++k)
161 {
162 const float ratio = (seen[k] + 0.004F) / (empty[k] + 0.004F);
163 least = std::min(least, ratio);
164 most = std::max(most, ratio);
165 }
166 if (most <= 1.03F && least >= params.shadowDarkest && most - least <= params.shadowSpread)
167 {
168 foreground.at<uchar>(r, c) = 0;
169 }
170 }
171 }
172 }
173 if (params.openPx > 1)
174 {
175 cv::morphologyEx(
176 foreground, foreground, cv::MORPH_OPEN,
177 cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.openPx, params.openPx)));
178 }
179 if (params.closePx > 1)
180 {
181 cv::morphologyEx(
182 foreground, foreground, cv::MORPH_CLOSE,
183 cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.closePx, params.closePx)));
184 }
185 return foreground;
186}

References cl_rgb::SegmenterParams::closePx, cl_rgb::SegmenterParams::openPx, cl_rgb::SegmenterParams::referenceLightnessWeight, cl_rgb::SegmenterParams::referenceThreshold, cl_rgb::SegmenterParams::shadowDarkest, and cl_rgb::SegmenterParams::shadowSpread.

Referenced by cl_rgb::CpTableSegmenter::foreground().

Here is the caller graph for this function:

◆ segmentForeground()

cv::Mat cl_rgb::segmentForeground ( const cv::Mat & bgr,
const cv::Mat & tableMask,
const SegmenterParams & params )

Definition at line 91 of file table_survey.cpp.

93{
94 cv::Mat floatImage, lab;
95 bgr.convertTo(floatImage, CV_32FC3, 1.0 / 255.0);
96 cv::cvtColor(floatImage, lab, cv::COLOR_BGR2Lab);
97 std::vector<cv::Mat> channels;
98 cv::split(lab, channels);
99
100 cv::Mat distanceSquared(bgr.size(), CV_32FC1, cv::Scalar(0.0f));
101 const double weights[3] = {params.lightnessWeight, 1.0, 1.0};
102 for (int c = 0; c < 3; ++c)
103 {
104 double median = 0.0, spread = 1.0;
105 robustStats(channels[c], tableMask, median, spread);
106 // A floor on the spread: a table of one flat colour must not turn noise into foreground.
107 spread = std::max(spread, 1.0);
108 cv::Mat normalized = (channels[c] - median) * (weights[c] / spread);
109 distanceSquared += normalized.mul(normalized);
110 }
111
112 cv::Mat foreground = (distanceSquared > params.threshold * params.threshold);
113 cv::bitwise_and(foreground, tableMask, foreground);
114 if (params.openPx > 1)
115 {
116 cv::morphologyEx(
117 foreground, foreground, cv::MORPH_OPEN,
118 cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.openPx, params.openPx)));
119 }
120 if (params.closePx > 1)
121 {
122 cv::morphologyEx(
123 foreground, foreground, cv::MORPH_CLOSE,
124 cv::getStructuringElement(cv::MORPH_RECT, cv::Size(params.closePx, params.closePx)));
125 }
126 return foreground;
127}

References cl_rgb::SegmenterParams::closePx, cl_rgb::SegmenterParams::lightnessWeight, cl_rgb::SegmenterParams::openPx, and cl_rgb::SegmenterParams::threshold.

Referenced by cl_rgb::CpTableSegmenter::foreground().

Here is the caller graph for this function:

◆ tableRegionMask()

cv::Mat cl_rgb::tableRegionMask ( const PinholeCamera & camera,
const cv::Size & size,
const TableParams & table )

Definition at line 63 of file table_survey.cpp.

65{
66 cv::Mat mask(size, CV_8UC1, cv::Scalar(0));
67 const cv::Matx33d H = camera.imageToPlaneHomography(table.height);
68 const double drop = table.height - camera.position[2];
69 for (int v = 0; v < size.height; ++v)
70 {
71 uchar * row = mask.ptr<uchar>(v);
72 for (int u = 0; u < size.width; ++u)
73 {
74 const cv::Vec3d p = H * cv::Vec3d(u, v, 1.0);
75 // p[2] is the vertical component of the ray; the ray must travel towards the plane
76 if (std::abs(p[2]) < 1e-12 || drop / p[2] <= 0.0)
77 {
78 continue;
79 }
80 const double x = p[0] / p[2];
81 const double y = p[1] / p[2];
82 if (x >= table.xMin && x <= table.xMax && y >= table.yMin && y <= table.yMax)
83 {
84 row[u] = 255;
85 }
86 }
87 }
88 return mask;
89}

References cl_rgb::TableParams::height, cl_rgb::PinholeCamera::imageToPlaneHomography(), cl_rgb::PinholeCamera::position, cl_rgb::TableParams::xMin, cl_rgb::TableParams::yMax, and cl_rgb::TableParams::yMin.

Referenced by cl_rgb::CpTablePlane::regionMask().

Here is the call graph for this function:
Here is the caller graph for this function: