{"record":{"id":"decc6b4622471238","repo":"Yalantis/uCrop","slug":"displacement-instance-and-reference-image-u","errorCode":null,"errorMessage":"displacement(): Instance and reference image (%u,%u,%u,%u,%p) have different dimensions.","messagePattern":"displacement\\(\\): Instance and reference image \\(%u,%u,%u,%u,%p\\) have different dimensions\\.","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":45959,"sourceCode":"       tells for each pixel if its correspondence vector is constrained to its initial value (constraint mask).\n    **/\n    CImg<T>& displacement(const CImg<T>& reference, const float smoothness=0.1f, const float precision=5.f,\n                          const unsigned int nb_scales=0, const unsigned int iteration_max=10000,\n                          const bool is_forward=false,\n                          const CImg<floatT>& guide=CImg<floatT>::const_empty()) {\n      return get_displacement(reference,smoothness,precision,nb_scales,iteration_max,is_forward,guide).\n        move_to(*this);\n    }\n\n    //! Estimate displacement field between two images \\newinstance.\n    CImg<floatT> get_displacement(const CImg<T>& reference,\n                                  const float smoothness=0.1f, const float precision=5.f,\n                                  const unsigned int nb_scales=0, const unsigned int iteration_max=1000,\n                                  const bool is_forward=false,\n                                  const CImg<floatT>& guide=CImg<floatT>::const_empty()) const {\n      if (is_empty() || !reference) return +*this;\n      if (!is_sameXYZC(reference))\n        throw CImgArgumentException(_cimg_instance\n                                    \"displacement(): Instance and reference image (%u,%u,%u,%u,%p) have \"\n                                    \"different dimensions.\",\n                                    cimg_instance,\n                                    reference._width,reference._height,reference._depth,reference._spectrum,\n                                    reference._data);\n      if (precision<0)\n        throw CImgArgumentException(_cimg_instance\n                                    \"displacement(): Invalid specified precision %g \"\n                                    \"(should be >=0)\",\n                                    cimg_instance,\n                                    precision);\n\n      const bool is_3d = reference._depth>1;\n      const unsigned int spectrum_U = is_3d?3:2;\n\n      if (guide &&\n          (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<spectrum_U))\n        throw CImgArgumentException(_cimg_instance","sourceCodeStart":45941,"sourceCodeEnd":45977,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L45941-L45977","documentation":"CImg's displacement() performs optical-flow estimation between the image instance and a reference image. It requires both images to have identical width, height, depth and spectrum (is_sameXYZC); otherwise it throws this CImgArgumentException, printing the reference image's dimensions and data pointer to help identify which image mismatched.","triggerScenarios":"Calling displacement(reference, ...) where reference has different dimensions than the instance - e.g. a reference frame from a resized video, a grayscale reference for an RGB instance (spectrum 1 vs 3), or a 3D volume paired with a 2D image (depth 1 vs >1).","commonSituations":"Video pipelines where frames vary in size (letterboxing), color/grayscale mismatch between frames, comparing images from different sources with different resolutions, or forgetting that a previously loaded image was cropped/rescaled.","solutions":["Resize the reference to match the instance with reference.resize(_width,_height,_depth,_spectrum) before calling displacement()","Match channel counts, e.g. convert both images to grayscale with get_norm().normalize() or get_channel(0)","Verify dimensions with if (!img.is_sameXYZC(reference)) before calling and handle the mismatch explicitly","Check upstream loading/cropping code so both images come from identically-sized sources"],"exampleFix":"// before\nCImg<float> flow = img.displacement(reference);\n// after\nif (!img.is_sameXYZC(reference))\n  reference.resize(img.width(), img.height(), img.depth(), img.spectrum());\nCImg<float> flow = img.displacement(reference);","handlingStrategy":"validation","validationCode":"if (!img.is_sameXYZC(reference)) {\n  reference.resize(img.width(), img.height(), img.depth(), img.spectrum());\n}\nCImg<float> flow = img.displacement(reference);","typeGuard":"template<typename T>\nbool isCompatibleReference(const CImg<T>& inst, const CImg<T>& ref) {\n  return !inst.is_empty() && ref && inst.is_sameXYZC(ref);\n}","tryCatchPattern":"try {\n  CImg<float> flow = img.displacement(reference);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"dimension mismatch: %s\\n\", e.what());\n  reference.resize(img.width(), img.height(), img.depth(), img.spectrum());\n  CImg<float> flow = img.displacement(reference);\n}","preventionTips":["Always check is_sameXYZC(reference) before optical flow","Keep both frames from the same resolution pipeline (avoid mixed letterboxing)","Match channel counts (grayscale vs RGB) before displacement","For video, assert frame dimensions are constant across the sequence"],"tags":["cimg","image-dimensions","optical-flow"],"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"}