{"record":{"id":"a8d29ef2b4e12435","repo":"Yalantis/uCrop","slug":"distance-dijkstra-image-instance-and-metric-map","errorCode":null,"errorMessage":"distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) have incompatible dimensions.","messagePattern":"distance_dijkstra\\(\\): image instance and metric map \\(%u,%u,%u,%u\\) have incompatible dimensions\\.","errorType":"exception","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":46968,"sourceCode":"       \\param value Reference value.\n       \\param metric Field of distance potentials.\n       \\param is_high_connectivity Tells if the algorithm uses low or high connectivity.\n       \\param[out] return_path An image containing the nodes of the minimal path.\n     **/\n    template<typename t, typename to>\n    CImg<T>& distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,\n                               CImg<to>& return_path) {\n      return get_distance_dijkstra(value,metric,is_high_connectivity,return_path).move_to(*this);\n    }\n\n    //! Compute distance map to a specified value, according to a custom metric (use dijkstra algorithm) \\newinstance.\n    template<typename t, typename to>\n    CImg<typename cimg::superset<t,long>::type>\n    get_distance_dijkstra(const T& value, const CImg<t>& metric, const bool is_high_connectivity,\n                          CImg<to>& return_path) const {\n      if (is_empty()) return return_path.assign();\n      if (!is_sameXYZ(metric))\n        throw CImgArgumentException(_cimg_instance\n                                    \"distance_dijkstra(): image instance and metric map (%u,%u,%u,%u) \"\n                                    \"have incompatible dimensions.\",\n                                    cimg_instance,\n                                    metric._width,metric._height,metric._depth,metric._spectrum);\n      typedef typename cimg::superset<t,long>::type td; // Type used for computing cumulative distances\n      CImg<td> result(_width,_height,_depth,_spectrum), Q;\n      CImg<boolT> is_queued(_width,_height,_depth,1);\n      if (return_path) return_path.assign(_width,_height,_depth,_spectrum);\n\n      cimg_forC(*this,c) {\n        const CImg<T> img = get_shared_channel(c);\n        const CImg<t> met = metric.get_shared_channel(c%metric._spectrum);\n        CImg<td> res = result.get_shared_channel(c);\n        CImg<to> path = return_path?return_path.get_shared_channel(c):CImg<to>();\n        unsigned int sizeQ = 0;\n\n        // Detect initial seeds.\n        is_queued.fill(0);","sourceCodeStart":46950,"sourceCodeEnd":46986,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L46950-L46986","documentation":"CImg::get_distance_dijkstra() computes a Dijkstra distance map from pixels equal to `value`, using `metric` as the per-pixel traversal cost map. The library requires the metric map to match the source image along X, Y and Z (spectrum/channel count may differ). If is_sameXYZ(metric) fails, it throws CImgArgumentException so the algorithm never runs on mismatched geometry.","triggerScenarios":"Calling get_distance_dijkstra(value, metric, ...) where metric._width/_height/_depth differ from the image's width/height/depth — e.g. passing a metric computed from a cropped, scaled, or padded image, or a metric with different depth (2D vs 3D).","commonSituations":"Resizing the image after computing the metric map; using a metric derived from a different frame in a video pipeline; mixing 2D and 3D volumes; passing a channel-expanded (spectrum>1) metric while spatial dims were accidentally altered by a crop (e.g. in an image-cropping native module like ucrop).","solutions":["Before calling, verify metric.width()==img.width() && metric.height()==img.height() && metric.depth()==img.depth() (CImg: img.is_sameXYZ(metric)).","Recompute the metric map from the exact image instance you pass, after all crops/resizes are applied.","If dimensions must differ, crop or resize the metric to match (metric.crop(0,0,0,0,w-1,h-1,d-1)) or pad the image.","Ensure both images share the same 2D/3D interpretation (a 2D image has depth 1)."],"exampleFix":"// before\nCImg<> metric = img.get_resize(64,64); // metric smaller than img\nCImg<> dist = img.get_distance_dijkstra(0, metric);\n// after\nCImg<> metric(img.width(), img.height(), img.depth(), 1, 1); // same XYZ dims\nCImg<> dist = img.get_distance_dijkstra(0, metric);","handlingStrategy":"validation","validationCode":"if (!img.is_sameXYZ(metric)) { /* resize metric or abort */ }","typeGuard":"bool metricMatches(const CImg<T>& img, const CImg<t>& metric) {\n  return img.is_sameXYZ(metric);\n}","tryCatchPattern":"try {\n  dist = img.get_distance_dijkstra(value, metric, connectivity, path);\n} catch (const CImgArgumentException& e) {\n  // log dims, recompute metric at img's geometry\n}","preventionTips":["Always derive the metric map from the same image instance passed to the call.","Assert is_sameXYZ(metric) in debug builds before distance computations.","Watch for crops/resizes between metric creation and use."],"tags":["cimg","image-processing","dimension-mismatch","argument-validation"],"backgroundTag":"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"}