{"record":{"id":"c2ef26fa98a05678","repo":"Yalantis/uCrop","slug":"cimg-instance-variance-noise-empty-instance","errorCode":null,"errorMessage":"_cimg_instance \"variance_noise(): Empty instance.\"","messagePattern":"_cimg_instance \"variance_noise\\(\\): Empty instance\\.\"","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32130,"sourceCode":"        variance = sig*sig;\n      }\n      }\n      mean = (t)(average/siz);\n      return variance>0?variance:0;\n    }\n\n    //! Return estimated variance of the noise.\n    /**\n       \\param variance_method Method used to compute the variance (see variance(const unsigned int) const).\n       \\note Because of structures such as edges in images it is\n       recommended to use a robust variance estimation. The variance of the\n       noise is estimated by computing the variance of the Laplacian \\f$(\\Delta\n       I)^2 \\f$ scaled by a factor \\f$c\\f$ insuring \\f$ c E[(\\Delta I)^2]=\n       \\sigma^2\\f$ where \\f$\\sigma\\f$ is the noise variance.\n    **/\n    double variance_noise(const unsigned int variance_method=2) const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"variance_noise(): Empty instance.\",\n                                    cimg_instance);\n\n      const ulongT siz = size();\n      if (!siz || !_data) return 0;\n      if (variance_method>1) { // Compute a scaled version of the Laplacian\n        CImg<Tdouble> tmp(*this,false);\n        if (_depth==1) {\n          const double cste = 1./std::sqrt(20.); // Depends on how the Laplacian is computed\n          cimg_pragma_openmp(parallel for cimg_openmp_if(_width*_height>=(cimg_openmp_sizefactor)*262144 &&\n                                                         _spectrum>=2))\n          cimg_forC(*this,c) {\n            CImg_3x3(I,T);\n            cimg_for3x3(*this,x,y,0,c,I,T) {\n              tmp(x,y,c) = cste*((double)Inc + (double)Ipc + (double)Icn +\n                                 (double)Icp - 4*(double)Icc);\n            }\n          }","sourceCodeStart":32112,"sourceCodeEnd":32148,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32112-L32148","documentation":"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.","triggerScenarios":"Calling CImg<T>::variance_noise(variance_method) on an instance where is_empty() is true — no dimensions or no allocated data.","commonSituations":"Noise estimation on unloaded images; quality checks in pipelines where a previous step failed silently and returned an empty CImg.","solutions":["Guard with is_empty() before calling variance_noise()","Verify the image data was loaded and size() > 0","Fix the failed load/transform upstream","Catch CImgInstanceException around the call"],"exampleFix":"// before\ndouble n = img.variance_noise();\n// after\nif (!img.is_empty()) {\n  double n = img.variance_noise();\n}","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"image empty before variance_noise()\");","typeGuard":"bool usable = !img.is_empty() && img.size() > 0;","tryCatchPattern":"try { double n = img.variance_noise(); } catch (const CImgInstanceException& e) { /* handle empty */ }","preventionTips":["Check is_empty() before noise estimation","Confirm pipeline steps never silently return empty images","Validate inputs at pipeline boundaries"],"tags":["cimg","image-processing","empty-image","noise"],"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"}