{"record":{"id":"278d09e0fca11c02","repo":"Yalantis/uCrop","slug":"watershed-image-instance-and-specified-priority","errorCode":null,"errorMessage":"watershed(): image instance and specified priority (%u,%u,%u,%u,%p) have different dimensions.","messagePattern":"watershed\\(\\): image instance and specified priority \\(%u,%u,%u,%u,%p\\) have different dimensions\\.","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":43554,"sourceCode":"       specified the priority map.\n    **/\n    template<typename t>\n    CImg<T>& watershed(const CImg<t>& priority, const bool is_high_connectivity=false) {\n#define _cimg_watershed_init(cond,X,Y,Z) \\\n      if (cond && !(*this)(X,Y,Z)) Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,nb_seeds)\n\n#define _cimg_watershed_propagate(cond,X,Y,Z) \\\n      if (cond) { \\\n        if ((*this)(X,Y,Z)) { \\\n          ns = labels(X,Y,Z) - 1; xs = seeds(ns,0); ys = seeds(ns,1); zs = seeds(ns,2); \\\n          d = cimg::sqr((float)x - xs) + cimg::sqr((float)y - ys) + cimg::sqr((float)z - zs); \\\n          if (d<dmin) { dmin = d; nmin = ns; nlabel = (*this)(xs,ys,zs); } \\\n        } else Q._priority_queue_insert(labels,sizeQ,priority(X,Y,Z),X,Y,Z,n); \\\n      }\n\n      if (is_empty()) return *this;\n      if (!is_sameXYZ(priority))\n        throw CImgArgumentException(_cimg_instance\n                                    \"watershed(): image instance and specified priority (%u,%u,%u,%u,%p) \"\n                                    \"have different dimensions.\",\n                                    cimg_instance,\n                                    priority._width,priority._height,priority._depth,priority._spectrum,priority._data);\n      if (_spectrum!=1) {\n        cimg_forC(*this,c)\n          get_shared_channel(c).watershed(priority.get_shared_channel(c%priority._spectrum));\n        return *this;\n      }\n\n      CImg<uintT> labels(_width,_height,_depth,1,0), seeds(64,3);\n      CImg<typename cimg::superset2<T,t,int>::type> Q;\n      unsigned int sizeQ = 0;\n      int px, nx, py, ny, pz, nz;\n      bool is_px, is_nx, is_py, is_ny, is_pz, is_nz;\n      const bool is_3d = _depth>1;\n\n      // Find seed points and insert them in priority queue.","sourceCodeStart":43536,"sourceCodeEnd":43572,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L43536-L43572","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Resize or crop the priority image to match img.dimensions() before calling watershed()","Ensure both images derive from the same source geometry","Add an assert(img.is_sameXYZ(priority)) in debug builds to catch mismatches early"],"exampleFix":"// before\nimg.watershed(prio); // prio from a resized image\n// after\npriority.resize(img.width(), img.height(), img.depth(), -100, 3);\nimg.watershed(priority);","handlingStrategy":"validation","validationCode":"if (!img.is_sameXYZ(priority)) priority.resize(img.width(), img.height(), img.depth(), -100, 3);","typeGuard":"bool watershedReady(const CImg<T>& img, const CImg<t>& prio) { return img.is_sameXYZ(prio); }","tryCatchPattern":"try {\n  img.watershed(priority);\n} catch (cimg_library::CImgArgumentException& e) {\n  priority.resize(img.width(), img.height(), img.depth(), -100, 3);\n  img.watershed(priority);\n}","preventionTips":["Keep priority maps in the same geometry pipeline as the source image","Check dimensions after any resize/crop before segmentation","Unit-test segmentation with same-size synthetic inputs"],"tags":["cimg","image-processing","watershed","dimension-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"}