{"record":{"id":"6a601e1956379e0e","repo":"Yalantis/uCrop","slug":"matchpatch-instance-image-and-specified-patch-i","errorCode":null,"errorMessage":"matchpatch(): Instance image and specified patch image (%u,%u,%u,%u,%p) have different spectrums.","messagePattern":"matchpatch\\(\\): Instance image and specified patch image \\(%u,%u,%u,%u,%p\\) have different spectrums\\.","errorType":"validation","errorClass":"CImgArgumentException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":46272,"sourceCode":"      CImg<T> matching_score;\n      return _matchpatch(patch_image,patch_width,patch_height,patch_depth,\n                         nb_iterations,nb_randoms,patch_penalization,guide,false,matching_score);\n    }\n\n    template<typename t1, typename t2>\n    CImg<intT> _matchpatch(const CImg<T>& patch_image,\n                           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","sourceCodeStart":46254,"sourceCodeEnd":46290,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L46254-L46290","documentation":"This is an internal overload of matchpatch() (patch matching) that scores patches from patch_image against the instance image. Both images must have the same number of channels (spectrum) because patch descriptors are compared channel-by-channel; a mismatch throws this CImgArgumentException, reporting the patch image's dimensions and data pointer.","triggerScenarios":"Calling matchpatch(patch_image, patch_width, patch_height, patch_depth, ...) where patch_image._spectrum != instance._spectrum - e.g. RGB instance with grayscale patch image, or a patch image loaded with a different channel count.","commonSituations":"Mixing color and grayscale images in template/patch matching, images converted differently upstream (one JPEG loaded as RGB, one as grayscale), or reusing a patch atlas built from differently-encoded sources.","solutions":["Match channel counts before calling: patch_image.resize(patch_image.width(),patch_image.height(),patch_image.depth(),_spectrum) or convert both to the same colorspace","Convert both images to grayscale with get_norm() when color is irrelevant","Guard with if (patch_image.spectrum() != img.spectrum()) and convert/abort explicitly","Verify the loading pipeline so both images decode with the same channel count"],"exampleFix":"// before\nCImg<unsigned char> matches = img.matchpatch(patches_gray, 7, 7, 1); // img is RGB\n// after\nCImg<unsigned char> matches =\n  img.get_norm().matchpatch(patches_gray.get_norm(), 7, 7, 1); // both grayscale","handlingStrategy":"validation","validationCode":"if (patch_image._spectrum != img._spectrum) {\n  patch_image.resize(patch_image.width(), patch_image.height(),\n                     patch_image.depth(), img.spectrum());\n}\nCImg<intT> matches = img.matchpatch(patch_image, pw, ph, pd);","typeGuard":"template<typename T, typename t>\nbool sameSpectrum(const CImg<T>& a, const CImg<t>& b) {\n  return a._spectrum == b._spectrum;\n}","tryCatchPattern":"try {\n  CImg<intT> matches = img.matchpatch(patch_image, pw, ph, pd);\n} catch (CImgArgumentException& e) {\n  std::fprintf(stderr, \"spectrum mismatch: %s\\n\", e.what());\n  CImg<intT> matches = img.get_norm().matchpatch(patch_image.get_norm(), pw, ph, pd);\n}","preventionTips":["Convert all images to a common colorspace/channel count at load time","Use get_norm() (grayscale) on both images when color is irrelevant","Assert spectrum equality in a wrapper around matchpatch","Beware JPEG loaders that return 1 or 3 channels depending on file content"],"tags":["cimg","image-dimensions","patch-matching"],"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"}