Yalantis/uCrop · error · CImgInstanceException
_cimg_instance "variance_mean(): Empty instance."
Error message
_cimg_instance "variance_mean(): Empty instance."
What it means
Instance guard in CImg<T>::variance_mean(): both variance estimators need at least one sample; the image is empty so neither variance nor mean can be computed, and the call throws before any summation loop runs.
Solutions
- Check img.is_empty() (or size() > 0) before the call
- Confirm the image load/population step succeeded
- Fix upstream code creating zero-size images
- Catch CImgInstanceException if empty input is expected
Example fix
// before
double var = img.variance_mean(2, mean);
// after
if (!img.is_empty()) {
double var = img.variance_mean(2, mean);
} Defensive patterns
Strategy: validation
Validate before calling
if (img.is_empty()) throw std::runtime_error("image empty before variance_mean()"); Type guard
bool usable = !img.is_empty() && img.size() > 0;
Try / catch
try { double v = img.variance_mean(2, mean); } catch (const CImgInstanceException& e) { /* handle empty */ } Prevention
- Guard noise/quality metrics with is_empty()
- Validate image dimensions after load
- Keep statistics helpers that centralize the empty check
When it happens
Trigger: Calling CImg<T>::variance_mean(variance_method, t& mean) on an empty instance (is_empty() true).
Common situations: Noise estimation or quality metrics computed on images that failed to load; statistics on images whose dimensions were never set.
Related errors
- _cimg_instance "kth_smallest(): Empty instance."
- _cimg_instance "max_min(): Empty instance."
- _cimg_instance "maxabs(): Empty instance."
- _cimg_instance "median(): Empty instance."
- _cimg_instance "min_max(): Empty instance."
AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08).
Data as JSON: /api/errors/2e5261841a4481f7.
Report an issue: GitHub.
Appendix: source
Thrown at ucrop/src/main/jni/CImg.h:32070
with \f$ \bar x = 1/N \sum\limits_{k=1}^N x_k \f$.
- \c 1: Best unbiased estimator, computed as \f$\frac{1}{N - 1} \sum\limits_{k=1}^{N} (x_k - \bar x)^2 \f$.
- \c 2: Least median of squares.
- \c 3: Least trimmed of squares.
**/
double variance(const unsigned int variance_method=1) const {
double foo;
return variance_mean(variance_method,foo);
}
//! Return the variance as well as the average of the pixel values.
/**
\param variance_method Method used to estimate the variance (see variance(const unsigned int) const).
\param[out] mean Average pixel value.
**/
template<typename t>
double variance_mean(const unsigned int variance_method, t& mean) const {
if (is_empty())
throw CImgInstanceException(_cimg_instance
"variance_mean(): Empty instance.",
cimg_instance);
double variance = 0, average = 0;
const ulongT siz = size();
switch (variance_method) {
case 0 : { // Least mean square (standard definition)
double S = 0, S2 = 0;
cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
variance = (S2 - S*S/siz)/siz;
average = S;
} break;
case 1 : { // Least mean square (robust definition)
double S = 0, S2 = 0;
cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }
variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
average = S;
} break;View on GitHub (pinned to f788b534b4)