Yalantis/uCrop · error · CImgArgumentException

watershed(): image instance and specified priority (%u,%u,%u

Error message

watershed(): image instance and specified priority (%u,%u,%u,%u,%p) have different dimensions.

What it means

CImg's watershed() requires the priority image to have exactly the same width, height and depth as the image being segmented (the spectrum may differ). If is_sameXYZ(priority) fails, the library throws this error because the flooding algorithm needs one priority value per spatial location.

Source

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

       specified the priority map.
    **/
    template<typename t>
    CImg<T>& watershed(const CImg<t>& priority, const bool is_high_connectivity=false) {
#define _cimg_watershed_init(cond,X,Y,Z) \
      if (cond && !(*this)(X,Y,Z)) Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,nb_seeds)

#define _cimg_watershed_propagate(cond,X,Y,Z) \
      if (cond) { \
        if ((*this)(X,Y,Z)) { \
          ns = labels(X,Y,Z) - 1; xs = seeds(ns,0); ys = seeds(ns,1); zs = seeds(ns,2); \
          d = cimg::sqr((float)x - xs) + cimg::sqr((float)y - ys) + cimg::sqr((float)z - zs); \
          if (d<dmin) { dmin = d; nmin = ns; nlabel = (*this)(xs,ys,zs); } \
        } else Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,n); \
      }

      if (is_empty()) return *this;
      if (!is_sameXYZ(priority))
        throw CImgArgumentException(_cimg_instance
                                    "watershed(): image instance and specified priority (%u,%u,%u,%u,%p) "
                                    "have different dimensions.",
                                    cimg_instance,
                                    priority._width,priority._height,priority._depth,priority._spectrum,priority._data);
      if (_spectrum!=1) {
        cimg_forC(*this,c)
          get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum));
        return *this;
      }

      CImg<uintT> labels(_width,_height,_depth,1,0), seeds(64,3);
      CImg<typename cimg::superset2<T,t,int>::type> Q;
      unsigned int sizeQ = 0;
      int px, nx, py, ny, pz, nz;
      bool is_px, is_nx, is_py, is_ny, is_pz, is_nz;
      const bool is_3d = _depth>1;

      // Find seed points and insert them in priority queue.

View on GitHub (pinned to f788b534b4)

Solutions

  1. Resize or crop the priority image to match img.dimensions() before calling watershed()
  2. Ensure both images derive from the same source geometry
  3. Add an assert(img.is_sameXYZ(priority)) in debug builds to catch mismatches early

Example fix

// before
img.watershed(prio); // prio from a resized image
// after
priority.resize(img.width(), img.height(), img.depth(), -100, 3);
img.watershed(priority);
Defensive patterns

Strategy: validation

Validate before calling

if (!img.is_sameXYZ(priority)) priority.resize(img.width(), img.height(), img.depth(), -100, 3);

Type guard

bool watershedReady(const CImg<T>& img, const CImg<t>& prio) { return img.is_sameXYZ(prio); }

Try / catch

try {
  img.watershed(priority);
} catch (cimg_library::CImgArgumentException& e) {
  priority.resize(img.width(), img.height(), img.depth(), -100, 3);
  img.watershed(priority);
}

Prevention

When it happens

Trigger: Calling img.watershed(prio) where prio was computed from a resized/cropped/upsampled version of img, or where prio came from a different image entirely.

Common situations: Computing a gradient/priority map at a different resolution (e.g. after resize() without interpolation alignment), loading the priority image from a separate file with different dimensions, forgetting that get_gradient() returns a 3-spectrum image whose XYZ must still match.

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/278d09e0fca11c02. Report an issue: GitHub.