{"record":{"id":"86afa7f4819b3e55","repo":"Yalantis/uCrop","slug":"distance-eikonal-image-instance-and-metric-map","errorCode":null,"errorMessage":"distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have incompatible dimensions.","messagePattern":"distance_eikonal\\(\\): 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":47156,"sourceCode":"      return get_distance_dijkstra(value,metric,is_high_connectivity,return_path);\n    }\n\n    //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).\n    /**\n       \\param value Reference value.\n       \\param metric Field of distance potentials.\n     **/\n    template<typename t>\n    CImg<T>& distance_eikonal(const T& value, const CImg<t>& metric) {\n      return get_distance_eikonal(value,metric).move_to(*this);\n    }\n\n    //! Compute distance map to one source point, according to a custom metric (use fast marching algorithm).\n    template<typename t>\n    CImg<Tfloat> get_distance_eikonal(const T& value, const CImg<t>& metric) const {\n      if (is_empty()) return *this;\n      if (!is_sameXYZ(metric))\n        throw CImgArgumentException(_cimg_instance\n                                    \"distance_eikonal(): image instance and metric map (%u,%u,%u,%u) have \"\n                                    \"incompatible dimensions.\",\n                                    cimg_instance,\n                                    metric._width,metric._height,metric._depth,metric._spectrum);\n      CImg<Tfloat> result(_width,_height,_depth,_spectrum,cimg::type<Tfloat>::max()), Q;\n      CImg<charT> state(_width,_height,_depth); // -1=far away, 0=narrow, 1=frozen\n\n      cimg_pragma_openmp(parallel for cimg_openmp_if(_spectrum>=2) firstprivate(Q,state))\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<Tfloat> res = result.get_shared_channel(c);\n        unsigned int sizeQ = 0;\n        state.fill(-1);\n\n        // Detect initial seeds.\n        Tfloat *ptr1 = res._data; char *ptr2 = state._data;\n        cimg_for(img,ptr0,T) { if (*ptr0==value) { *ptr1 = 0; *ptr2 = 1; } ++ptr1; ++ptr2; }","sourceCodeStart":47138,"sourceCodeEnd":47174,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L47138-L47174","documentation":"CImg::get_distance_eikonal() computes an eikonal (fast-marching) distance map to pixels equal to `value`, using `metric` as the local speed map. It requires metric to have identical width, height and depth as the image (checked via is_sameXYZ); otherwise it throws CImgArgumentException before running the fast-marching solver.","triggerScenarios":"Calling get_distance_eikonal(value, metric) where the metric map's width, height, or depth differs from the calling image — e.g. a downsampled speed map, a metric for a neighboring tile, or a 3D metric used with a 2D image.","commonSituations":"Building the metric at a different resolution (pyramid/multiscale code), cropping one image but not the metric, video frames of varying sizes, or accidentally passing the metric of a previous iteration's image.","solutions":["Check img.is_sameXYZ(metric) (or compare width/height/depth) before the call.","Regenerate the metric from the same image instance passed as `this`, after all preprocessing.","Resize/crop the metric to the image geometry, or pad the smaller one to match.","Confirm depth matches: a 2D image needs a depth-1 metric."],"exampleFix":"// before\nCImg<> metric = speed.get_crop(0,0,w/2,h/2); // cropped metric\nCImg<Tfloat> d = img.get_distance_eikonal(0, metric);\n// after\nCImg<> metric = speed.get_crop(0,0,img.width()-1,img.height()-1);\nCImg<Tfloat> d = img.get_distance_eikonal(0, metric);","handlingStrategy":"validation","validationCode":"if (!img.is_sameXYZ(metric)) throw std::invalid_argument(\"metric must match image XYZ dims\");","typeGuard":"bool eikonalMetricOk(const CImg<T>& img, const CImg<t>& metric) {\n  return img.is_sameXYZ(metric);\n}","tryCatchPattern":"try {\n  d = img.get_distance_eikonal(seedValue, metric);\n} catch (const CImgArgumentException& e) {\n  metric.resize(img.width(), img.height(), img.depth(), -1, 0);\n  d = img.get_distance_eikonal(seedValue, metric);\n}","preventionTips":["Build the speed/metric map at the exact resolution of the input image.","Re-check dims after any multiscale/pyramid downsampling.","Prefer letting resize/crop helpers keep image and metric in lockstep."],"tags":["cimg","image-processing","dimension-mismatch","fast-marching"],"backgroundTag":"shape-mismatch","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}