SMACC2
Loading...
Searching...
No Matches
cl_rgb::anonymous_namespace{box_fit.cpp} Namespace Reference

Functions

cv::Mat outline (const PinholeCamera &camera, const cv::Rect &box, double x, double y, double yaw, double tableZ, const cv::Vec3d &size, bool topOnly)
 
double hullArea (const PinholeCamera &camera, double x, double y, double yaw, double tableZ, const cv::Vec3d &size, bool topOnly)
 
Blob strongColourPart (const Blob &whole, double minChromaFraction)
 

Function Documentation

◆ hullArea()

double cl_rgb::anonymous_namespace{box_fit.cpp}::hullArea ( const PinholeCamera & camera,
double x,
double y,
double yaw,
double tableZ,
const cv::Vec3d & size,
bool topOnly )

Definition at line 54 of file box_fit.cpp.

57{
58 const double c = std::cos(yaw), s = std::sin(yaw);
59 std::vector<cv::Point2f> corners;
60 for (int i = topOnly ? 4 : 0; i < 8; ++i)
61 {
62 const double dx = ((i & 1) != 0 ? 0.5 : -0.5) * size[0];
63 const double dy = ((i & 2) != 0 ? 0.5 : -0.5) * size[1];
64 const double dz = (i & 4) != 0 ? size[2] : 0.0;
65 const auto pixel =
66 camera.project(cv::Vec3d(x + c * dx - s * dy, y + s * dx + c * dy, tableZ + dz));
67 if (!pixel)
68 {
69 return 0.0;
70 }
71 corners.emplace_back(static_cast<float>(pixel->x), static_cast<float>(pixel->y));
72 }
73 std::vector<cv::Point2f> hull;
74 cv::convexHull(corners, hull);
75 return cv::contourArea(hull);
76}
std::optional< cv::Point2d > project(const cv::Vec3d &point) const
Definition geometry.cpp:25

References hullArea(), and cl_rgb::PinholeCamera::project().

Referenced by hullArea().

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

◆ outline()

cv::Mat cl_rgb::anonymous_namespace{box_fit.cpp}::outline ( const PinholeCamera & camera,
const cv::Rect & box,
double x,
double y,
double yaw,
double tableZ,
const cv::Vec3d & size,
bool topOnly )

Definition at line 28 of file box_fit.cpp.

31{
32 const double c = std::cos(yaw), s = std::sin(yaw);
33 std::vector<cv::Point> corners;
34 for (int i = topOnly ? 4 : 0; i < 8; ++i)
35 {
36 const double dx = ((i & 1) != 0 ? 0.5 : -0.5) * size[0];
37 const double dy = ((i & 2) != 0 ? 0.5 : -0.5) * size[1];
38 const double dz = (i & 4) != 0 ? size[2] : 0.0;
39 const auto pixel =
40 camera.project(cv::Vec3d(x + c * dx - s * dy, y + s * dx + c * dy, tableZ + dz));
41 if (!pixel)
42 {
43 return cv::Mat();
44 }
45 corners.emplace_back(cvRound(pixel->x) - box.x, cvRound(pixel->y) - box.y);
46 }
47 std::vector<cv::Point> hull;
48 cv::convexHull(corners, hull);
49 cv::Mat drawn = cv::Mat::zeros(box.size(), CV_8UC1);
50 cv::fillConvexPoly(drawn, hull, cv::Scalar(255));
51 return drawn;
52}

References outline(), and cl_rgb::PinholeCamera::project().

Referenced by outline().

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

◆ strongColourPart()

Blob cl_rgb::anonymous_namespace{box_fit.cpp}::strongColourPart ( const Blob & whole,
double minChromaFraction )

Definition at line 82 of file box_fit.cpp.

83{
84 if (minChromaFraction <= 0.0 || whole.crop.empty())
85 {
86 return whole;
87 }
88 cv::Mat floatCrop, lab;
89 whole.crop.convertTo(floatCrop, CV_32FC3, 1.0 / 255.0);
90 cv::cvtColor(floatCrop, lab, cv::COLOR_BGR2Lab);
91 std::vector<float> chromas, lightnesses;
92 cv::Mat chroma(lab.size(), CV_32FC1, cv::Scalar(0));
93 cv::Mat lightness(lab.size(), CV_32FC1, cv::Scalar(0));
94 for (int r = 0; r < lab.rows; ++r)
95 {
96 for (int c = 0; c < lab.cols; ++c)
97 {
98 if (whole.mask.at<uchar>(r, c))
99 {
100 const cv::Vec3f v = lab.at<cv::Vec3f>(r, c);
101 chroma.at<float>(r, c) = std::hypot(v[1], v[2]);
102 chromas.push_back(chroma.at<float>(r, c));
103 lightness.at<float>(r, c) = v[0];
104 lightnesses.push_back(v[0]);
105 }
106 }
107 }
108 if (chromas.empty())
109 {
110 return whole;
111 }
112 std::sort(chromas.begin(), chromas.end());
113 const float strong = chromas[static_cast<size_t>(0.9 * (chromas.size() - 1))];
114 Blob part = whole;
115 std::sort(lightnesses.begin(), lightnesses.end());
116 const float middle = lightnesses[lightnesses.size() / 2];
117 part.mask = whole.mask &
118 ((chroma >= static_cast<float>(minChromaFraction) * strong) | (lightness >= middle));
119 part.areaPx = cv::countNonZero(part.mask);
120 if (part.areaPx < whole.areaPx / 4)
121 {
122 return whole;
123 }
124 const cv::Moments m = cv::moments(part.mask, true);
125 part.centroidPx = cv::Point2d(whole.box.x + m.m10 / m.m00, whole.box.y + m.m01 / m.m00);
126 return part;
127}

References cl_rgb::Blob::areaPx, cl_rgb::Blob::box, cl_rgb::Blob::centroidPx, cl_rgb::Blob::crop, cl_rgb::Blob::mask, and strongColourPart().

Referenced by strongColourPart().

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