{"record":{"id":"2e5261841a4481f7","repo":"Yalantis/uCrop","slug":"cimg-instance-variance-mean-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"variance_mean(): Empty instance.\"","messagePattern":"_cimg_instance \"variance_mean\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32070,"sourceCode":"       with \\f$ \\bar x = 1/N \\sum\\limits_{k=1}^N x_k \\f$.\n       - \\c 1: Best unbiased estimator, computed as \\f$\\frac{1}{N - 1} \\sum\\limits_{k=1}^{N} (x_k - \\bar x)^2 \\f$.\n       - \\c 2: Least median of squares.\n       - \\c 3: Least trimmed of squares.\n    **/\n    double variance(const unsigned int variance_method=1) const {\n      double foo;\n      return variance_mean(variance_method,foo);\n    }\n\n    //! Return the variance as well as the average of the pixel values.\n    /**\n       \\param variance_method Method used to estimate the variance (see variance(const unsigned int) const).\n       \\param[out] mean Average pixel value.\n    **/\n    template<typename t>\n    double variance_mean(const unsigned int variance_method, t& mean) const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"variance_mean(): Empty instance.\",\n                                    cimg_instance);\n\n      double variance = 0, average = 0;\n      const ulongT siz = size();\n      switch (variance_method) {\n      case 0 : { // Least mean square (standard definition)\n        double S = 0, S2 = 0;\n        cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }\n        variance = (S2 - S*S/siz)/siz;\n        average = S;\n      } break;\n      case 1 : { // Least mean square (robust definition)\n        double S = 0, S2 = 0;\n        cimg_for(*this,ptrs,T) { const double val = (double)*ptrs; S+=val; S2+=val*val; }\n        variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;\n        average = S;\n      } break;","sourceCodeStart":32052,"sourceCodeEnd":32088,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32052-L32088","documentation":"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.","triggerScenarios":"Calling CImg<T>::variance_mean(variance_method, t& mean) on an empty instance (is_empty() true).","commonSituations":"Noise estimation or quality metrics computed on images that failed to load; statistics on images whose dimensions were never set.","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"],"exampleFix":"// before\ndouble var = img.variance_mean(2, mean);\n// after\nif (!img.is_empty()) {\n  double var = img.variance_mean(2, mean);\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before variance_mean()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { double v = img.variance_mean(2, mean); } catch (const CImgInstanceException& e) { /* handle empty */ }","preventionTips":["Guard noise/quality metrics with is_empty()","Validate image dimensions after load","Keep statistics helpers that centralize the empty check"],"tags":["cimg","image-processing","empty-image","statistics"],"backgroundTag":"empty-image-instance","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}