{"record":{"id":"9ee9a9330070acbb","repo":"Yalantis/uCrop","slug":"matchpatch-specified-patch-size-ux-ux-u-is-big","errorCode":null,"errorMessage":"matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions of the instance image.","messagePattern":"matchpatch\\(\\): Specified patch size %ux%ux%u is bigger than the dimensions of the instance image\\.","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":46279,"sourceCode":"                           const unsigned int patch_width,\n                           const unsigned int patch_height,\n                           const unsigned int patch_depth,\n                           const unsigned int nb_iterations,\n                           const unsigned int nb_randoms,\n                           const float patch_penalization,\n                           const CImg<t1> &guide,\n                           const bool is_matching_score,\n                           CImg<t2> &matching_score) const {\n      if (is_empty()) return CImg<intT>::const_empty();\n      if (patch_image._spectrum!=_spectrum)\n        throw CImgArgumentException(_cimg_instance\n                                    \"matchpatch(): Instance image and specified patch image (%u,%u,%u,%u,%p) \"\n                                    \"have different spectrums.\",\n                                    cimg_instance,\n                                    patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,\n                                    patch_image._data);\n      if (patch_width>_width || patch_height>_height || patch_depth>_depth)\n        throw CImgArgumentException(_cimg_instance\n                                    \"matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions \"\n                                    \"of the instance image.\",\n                                    cimg_instance,patch_width,patch_height,patch_depth);\n      if (patch_width>patch_image._width || patch_height>patch_image._height || patch_depth>patch_image._depth)\n        throw CImgArgumentException(_cimg_instance\n                                    \"matchpatch(): Specified patch size %ux%ux%u is bigger than the dimensions \"\n                                    \"of the patch image image (%u,%u,%u,%u,%p).\",\n                                    cimg_instance,patch_width,patch_height,patch_depth,\n                                    patch_image._width,patch_image._height,patch_image._depth,patch_image._spectrum,\n                                    patch_image._data);\n      const unsigned int\n        _constraint = patch_image._depth>1?3:2,\n        constraint = guide._spectrum>_constraint?_constraint:0;\n\n      if (guide &&\n          (guide._width!=_width || guide._height!=_height || guide._depth!=_depth || guide._spectrum<_constraint))\n        throw CImgArgumentException(_cimg_instance\n                                    \"matchpatch(): Specified guide (%u,%u,%u,%u,%p) has invalid dimensions \"","sourceCodeStart":46261,"sourceCodeEnd":46297,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L46261-L46297","documentation":"In matchpatch(), the patch window (patch_width x patch_height x patch_depth) must fit inside the instance image. If any patch dimension exceeds the corresponding instance dimension, the library throws this CImgArgumentException because no valid match window could be extracted.","triggerScenarios":"Calling matchpatch() with patch_width > _width, patch_height > _height, or patch_depth > _depth - e.g. a 16x16 patch on a 10x10 image, or a 3D patch size on a 2D (depth=1) image.","commonSituations":"Hardcoded patch sizes larger than small thumbnails/tiles, applying 3D-volume patch parameters to 2D images, or images downscaled by preprocessing after the patch size was chosen.","solutions":["Reduce patch_width/patch_height/patch_depth so each is <= the instance image's dimensions","Compute the patch size dynamically, e.g. std::min(16u, (unsigned)img.width())","Skip or resize small images before patch matching","Use patch_depth=1 for 2D images instead of a volumetric patch size"],"exampleFix":"// before\nimg.matchpatch(patches, 16, 16, 1); // img is 10x10\n// after\nconst unsigned pw = std::min(16u, (unsigned)img.width());\nconst unsigned ph = std::min(16u, (unsigned)img.height());\nimg.matchpatch(patches, pw, ph, 1);","handlingStrategy":"validation","validationCode":"if (patch_width > img.width() || patch_height > img.height() || patch_depth > img.depth()) {\n  patch_width  = std::min(patch_width,  (unsigned)img.width());\n  patch_height = std::min(patch_height, (unsigned)img.height());\n  patch_depth  = std::min(patch_depth,  (unsigned)img.depth());\n}\nCImg<intT> matches = img.matchpatch(patch_image, patch_width, patch_height, patch_depth);","typeGuard":"template<typename T>\nbool patchFitsImage(unsigned pw, unsigned ph, unsigned pd, const CImg<T>& img) {\n  return pw <= (unsigned)img.width() && ph <= (unsigned)img.height() &&\n         pd <= (unsigned)img.depth();\n}","tryCatchPattern":"try {\n  CImg<intT> matches = img.matchpatch(patch_image, pw, ph, pd);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"patch too large for instance: %s\\n\", e.what());\n  CImg<intT> matches = img.matchpatch(patch_image,\n      std::min(pw,(unsigned)img.width()), std::min(ph,(unsigned)img.height()), 1);\n}","preventionTips":["Derive patch size from the smallest image in the batch, not a constant","Use patch_depth=1 for 2D images","Clamp patch dimensions with std::min against image dims","Re-check patch sizes after any downscaling preprocessing"],"tags":["cimg","image-dimensions","patch-matching"],"backgroundTag":"value-out-of-range","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"}