Yalantis/uCrop · error · CImgArgumentException

_cimg_instance "MSE(): Instance and specified image (%u,%u,%

Error message

_cimg_instance "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions."

What it means

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).

Source

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

            ((double)Incc + (double)Ipcc + (double)Icnc +
             (double)Icpc +
             (double)Iccn + (double)Iccp - 6*(double)Iccc);
          S+=val; S2+=val*val;
        }
      }
      if (variance_method) variance = siz>1?(S2 - S*S/siz)/(siz - 1):0;
      else variance = (S2 - S*S/siz)/siz;
      return variance>0?variance:0;
    }

    //! Compute the MSE (Mean-Squared Error) between two images.
    /**
       \param img Image used as the second argument of the MSE operator.
    **/
    template<typename t>
    double MSE(const CImg<t>& img) const {
      if (img.size()!=size())
        throw CImgArgumentException(_cimg_instance
                                    "MSE(): Instance and specified image (%u,%u,%u,%u,%p) have different dimensions.",
                                    cimg_instance,
                                    img._width,img._height,img._depth,img._spectrum,img._data);
      double vMSE = 0;
      const t* ptr2 = img._data;
      cimg_for(*this,ptr1,T) {
        const double diff = (double)*ptr1 - (double)*(ptr2++);
        vMSE+=diff*diff;
      }
      const ulongT siz = img.size();
      if (siz) vMSE/=siz;
      return vMSE;
    }

    //! Compute the PSNR (Peak Signal-to-Noise Ratio) between two images.
    /**
       \param img Image used as the second argument of the PSNR operator.
       \param max_value Maximum theoretical value of the signal.

View on GitHub (pinned to f788b534b4)

Solutions

  1. Make both images the same size first: resize() or crop() one to match the other's dimensions
  2. Convert to the same number of channels (e.g. channels(0,2) to drop alpha, or RGB conversion) before comparing
  3. Validate img.size() == other.size() before calling MSE()
  4. Assert dimensions match (_width,_height,_depth,_spectrum) in debug builds

Example fix

// before
double err = reference.MSE(candidate); // sizes differ
// after
if (reference.size() == candidate.size()) {
  double err = reference.MSE(candidate);
} else {
  candidate.resize(reference, 3);
  double err = reference.MSE(candidate);
}
Defensive patterns

Strategy: validation

Validate before calling

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()");

Type guard

bool comparable = a.size() == b.size();

Try / catch

try { double err = a.MSE(b); } catch (const CImgArgumentException& e) { /* resize/convert and retry */ }

Prevention

When it happens

Trigger: 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.

Common situations: Comparing reference and reconstructed images saved/loaded with different channel counts or rescaling; comparing crops of different sizes; forgetting to resize before quality evaluation.

Understand the failure class

Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.

Related errors


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