Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "variance_noise(): Empty instance."

Error message

_cimg_instance "variance_noise(): Empty instance."

What it means

variance_noise() throws CImgInstanceException when the instance is empty. It estimates noise variance (via a scaled Laplacian for methods > 1) which requires pixel data; note that a non-empty image with null data returns 0 instead of throwing.

Solutions

  1. Guard with is_empty() before calling variance_noise()
  2. Verify the image data was loaded and size() > 0
  3. Fix the failed load/transform upstream
  4. Catch CImgInstanceException around the call

Example fix

// before
double n = img.variance_noise();
// after
if (!img.is_empty()) {
  double n = img.variance_noise();
}
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_empty()) throw std::runtime_error("image empty before variance_noise()");

Type guard

bool usable = !img.is_empty() && img.size() > 0;

Try / catch

try { double n = img.variance_noise(); } catch (const CImgInstanceException& e) { /* handle empty */ }

Prevention

When it happens

Trigger: Calling CImg<T>::variance_noise(variance_method) on an instance where is_empty() is true — no dimensions or no allocated data.

Common situations: Noise estimation on unloaded images; quality checks in pipelines where a previous step failed silently and returned an empty CImg.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/c2ef26fa98a05678. Report an issue: GitHub.

Appendix: source

Thrown at ucrop/src/main/jni/CImg.h:32130

        variance = sig*sig;
      }
      }
      mean = (t)(average/siz);
      return variance>0?variance:0;
    }

    //! Return estimated variance of the noise.
    /**
       \param variance_method Method used to compute the variance (see variance(const unsigned int) const).
       \note Because of structures such as edges in images it is
       recommended to use a robust variance estimation. The variance of the
       noise is estimated by computing the variance of the Laplacian \f$(\Delta
       I)^2 \f$ scaled by a factor \f$c\f$ insuring \f$ c E[(\Delta I)^2]=
       \sigma^2\f$ where \f$\sigma\f$ is the noise variance.
    **/
    double variance_noise(const unsigned int variance_method=2) const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "variance_noise(): Empty instance.",
                                    cimg_instance);

      const ulongT siz = size();
      if (!siz || !_data) return 0;
      if (variance_method>1) { // Compute a scaled version of the Laplacian
        CImg<Tdouble> tmp(*this,false);
        if (_depth==1) {
          const double cste = 1./std::sqrt(20.); // Depends on how the Laplacian is computed
          cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*262144 &&
                                                         _spectrum>=2))
          cimg_forC(*this,c) {
            CImg_3x3(I,T);
            cimg_for3x3(*this,x,y,0,c,I,T) {
              tmp(x,y,c) = cste*((double)Inc + (double)Ipc + (double)Icn +
                                 (double)Icp - 4*(double)Icc);
            }
          }

View on GitHub (pinned to f788b534b4)