103 size_t bin = (std::max(image.
width, image.
height) + MAX_WORKING_SIZE - 1) / MAX_WORKING_SIZE;
104 if (image.
bayer && bin % 2)
106 bin = std::max<size_t>(bin, 1);
108 const size_t workWidth = image.
width / bin;
109 const size_t workHeight = image.
height / bin;
110 if (workWidth == 0 || workHeight == 0)
114 const size_t binSamples = bin * bin * image.
components;
115 std::vector<double> pixels(workWidth * workHeight);
117 for (
size_t wy = 0; wy < workHeight; wy++)
119 for (
size_t wx = 0; wx < workWidth; wx++)
122 for (
size_t y = wy * bin; y < (wy + 1) * bin; y++)
124 const size_t start = y * rowSamples + wx * bin * image.
components;
125 const size_t end = start + bin * image.
components;
128 for (
size_t i = start; i < end; i++)
129 sum += image.
data[i];
133 const uint16_t *data16 =
reinterpret_cast<const uint16_t *
>(image.
data);
134 for (
size_t i = start; i < end; i++)
139 pixels[wy * workWidth + wx] =
static_cast<double>(sum) / binSamples;
143 const auto minMax = std::minmax_element(pixels.begin(), pixels.end());
144 const double minValue = *minMax.first, maxValue = *minMax.second;
145 if (maxValue - minValue <= 0)
148 const double threshold = otsuThreshold(pixels, minValue, maxValue);
152 std::vector<int32_t> labels(pixels.size(), 0);
153 std::vector<size_t> stack;
154 int32_t bestLabel = 0, nextLabel = 0;
155 size_t bestCount = 0;
157 for (
size_t start = 0; start < pixels.size(); start++)
159 if (labels[start] != 0 || pixels[start] <= threshold)
162 const int32_t label = ++nextLabel;
164 labels[start] = label;
165 stack.push_back(start);
166 while (!stack.empty())
168 const size_t index = stack.back();
172 const size_t x = index % workWidth, y = index / workWidth;
173 const size_t neighbors[4] =
175 x > 0 ? index - 1 : index,
176 x + 1 < workWidth ? index + 1 : index,
177 y > 0 ? index - workWidth : index,
178 y + 1 < workHeight ? index + workWidth : index
180 for (
size_t neighbor : neighbors)
182 if (labels[neighbor] == 0 && pixels[neighbor] > threshold)
184 labels[neighbor] = label;
185 stack.push_back(neighbor);
190 if (count > bestCount)
197 if (bestCount < MIN_BLOB_PIXELS)
201 double sumWeight = 0, sumX = 0, sumY = 0, sumBlob = 0;
202 double sumSky = 0, sumSky2 = 0;
204 bool touchesEdge =
false;
206 for (
size_t y = 0; y < workHeight; y++)
208 for (
size_t x = 0; x < workWidth; x++)
210 const size_t index = y * workWidth + x;
211 const double value = pixels[index];
212 if (labels[index] == bestLabel)
214 const double weight = value - threshold;
219 if (x == 0 || y == 0 || x + 1 == workWidth || y + 1 == workHeight)
222 else if (labels[index] == 0)
225 sumSky2 += value * value;
235 result.x = sumX / sumWeight * bin + (bin - 1) / 2.0;
236 result.y = sumY / sumWeight * bin + (bin - 1) / 2.0;
237 result.area =
static_cast<double>(bestCount * bin * bin);
240 const double blobMean = sumBlob / bestCount;
241 const double skyMean = skyCount > 0 ? sumSky / skyCount : minValue;
242 const double skyStd = skyCount > 1 ? std::sqrt(std::max(0.0, sumSky2 / skyCount - skyMean * skyMean)) : 0;
243 const double signal = blobMean - skyMean;
246 double quality = skyStd > 0 ? clamp01((signal / skyStd - MIN_SNR) / (GOOD_SNR - MIN_SNR)) : 1.0;
247 quality *= clamp01(signal / (GOOD_SIGNAL * fullScale));
249 quality *= EDGE_FACTOR;
250 if (referenceArea > 0)
251 quality *= clamp01(1.0 - std::fabs(std::log2(result.area / referenceArea)));
253 result.quality = quality;
254 result.valid = quality > 0;