84 if (minChromaFraction <= 0.0 || whole.
crop.empty())
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)
96 for (
int c = 0; c < lab.cols; ++c)
98 if (whole.
mask.at<uchar>(r, c))
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]);
112 std::sort(chromas.begin(), chromas.end());
113 const float strong = chromas[
static_cast<size_t>(0.9 * (chromas.size() - 1))];
115 std::sort(lightnesses.begin(), lightnesses.end());
116 const float middle = lightnesses[lightnesses.size() / 2];
118 ((chroma >=
static_cast<float>(minChromaFraction) * strong) | (lightness >= middle));
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);
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));
151 for (
const bool topOnly : {
false,
true})
165 for (
double yaw = 0.0; yaw < yawEnd - 1e-9; yaw += params.
yawStepRad)
167 for (
int ix = -steps; ix <= steps; ++ix)
169 for (
int iy = -steps; iy <= steps; ++iy)
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);
178 const double both = cv::countNonZero(drawn & blob.
mask);
181 std::max(hullArea(camera, x, y, yaw, tableZ, params.
sizeM, topOnly), 1.0);
182 const double overlap = both / (whole + blob.
areaPx - both);
185 best =
BoxFit{x, y, yaw, overlap, topOnly};
cv::Mat outline(const PinholeCamera &camera, const cv::Rect &box, double x, double y, double yaw, double tableZ, const cv::Vec3d &size, bool topOnly)