{"record":{"id":"9625936088151c08","repo":"Yalantis/uCrop","slug":"cimg-instance-mse-instance-and-specified-imag","errorCode":null,"errorMessage":"_cimg_instance \"MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.\"","messagePattern":"_cimg_instance \"MSE\\(\\): Instance and specified image \\(%u,%u,%u,%u,%p\\) have different dimensions\\.\"","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32198,"sourceCode":"            ((double)Incc + (double)Ipcc + (double)Icnc +\n             (double)Icpc +\n             (double)Iccn + (double)Iccp - 6*(double)Iccc);\n          S+=val; S2+=val*val;\n        }\n      }\n      if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;\n      else variance = (S2 - S*S/siz)/siz;\n      return variance>0?variance:0;\n    }\n\n    //! Compute the MSE (Mean-Squared Error) between two images.\n    /**\n       \\param img Image used as the second argument of the MSE operator.\n    **/\n    template<typename t>\n    double MSE(const CImg<t>& img) const {\n      if (img.size()!=size())\n        throw CImgArgumentException(_cimg_instance\n                                    \"MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.\",\n                                    cimg_instance,\n                                    img._width,img._height,img._depth,img._spectrum,img._data);\n      double vMSE = 0;\n      const t* ptr2 = img._data;\n      cimg_for(*this,ptr1,T) {\n        const double diff = (double)*ptr1 - (double)*(ptr2++);\n        vMSE+=diff*diff;\n      }\n      const ulongT siz = img.size();\n      if (siz) vMSE/=siz;\n      return vMSE;\n    }\n\n    //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images.\n    /**\n       \\param img Image used as the second argument of the PSNR operator.\n       \\param max_value Maximum theoretical value of the signal.","sourceCodeStart":32180,"sourceCodeEnd":32216,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32180-L32216","documentation":"MSE(img) throws CImgArgumentException (not an instance exception) when the two images have different total sizes. MSE is a per-pixel difference accumulated over both buffers, so the images must have identical dimensions (same width*height*depth*spectrum).","triggerScenarios":"Calling img.MSE(other) where other.size() != img.size() — e.g. comparing an RGB (3-channel) image against a grayscale one, different resolutions, or one image being empty while the other is not.","commonSituations":"Comparing reference and reconstructed images saved/loaded with different channel counts or rescaling; comparing crops of different sizes; forgetting to resize before quality evaluation.","solutions":["Make both images the same size first: resize() or crop() one to match the other's dimensions","Convert to the same number of channels (e.g. channels(0,2) to drop alpha, or RGB conversion) before comparing","Validate img.size() == other.size() before calling MSE()","Assert dimensions match (_width,_height,_depth,_spectrum) in debug builds"],"exampleFix":"// before\ndouble err = reference.MSE(candidate); // sizes differ\n// after\nif (reference.size() == candidate.size()) {\n  double err = reference.MSE(candidate);\n} else {\n  candidate.resize(reference, 3);\n  double err = reference.MSE(candidate);\n}","handlingStrategy":"validation","validationCode":"if (a.size() != b.size() || a._width != b._width || a._height != b._height || a._depth != b._depth || a._spectrum != b._spectrum) throw std::runtime_error(\"image dimensions differ before MSE()\");","typeGuard":"bool comparable = a.size() == b.size();","tryCatchPattern":"try { double err = a.MSE(b); } catch (const CImgArgumentException& e) { /* resize/convert and retry */ }","preventionTips":["Resize and channel-normalize both images before quality comparison","Assert _width/_height/_depth/_spectrum equality in tests","Never compare crops of different sizes directly"],"tags":["cimg","image-processing","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}