{"record":{"id":"fc35b86e3a06db8d","repo":"Yalantis/uCrop","slug":"invert-instance-is-not-a-matrix","errorCode":null,"errorMessage":"invert(): Instance is not a matrix.","messagePattern":"invert\\(\\): Instance is not a matrix\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":32833,"sourceCode":"    }\n\n    //! Compute the cross product between two \\c 1x3 images, viewed as 3D vectors \\newinstance.\n    template<typename t>\n    CImg<_cimg_Tt> get_cross(const CImg<t>& img) const {\n      return CImg<_cimg_Tt>(*this).cross(img);\n    }\n\n    //! Invert the instance image, viewed as a matrix.\n    /**\n       If the instance matrix is not square, the Moore-Penrose pseudo-inverse is computed instead.\n       \\param use_LU Choose the inverting algorithm. Can be:\n       - \\c true: LU solver (faster but sometimes less precise).\n       - \\c false: SVD solver (more precise but slower).\n       \\param lambda is used only in the Moore-Penrose pseudoinverse for estimating A^t.(A^t.A + lambda.Id)^-1.\n    **/\n    CImg<T>& invert(const bool use_LU=false, const float lambda=0) {\n      if (_depth!=1 || _spectrum!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"invert(): Instance is not a matrix.\",\n                                    cimg_instance);\n      if (lambda<0)\n        throw CImgArgumentException(_cimg_instance\n                                    \"invert(): Specified lambda (%g) should be >=0.\",\n                                    cimg_instance);\n\n      if (_width!=_height) return get_invert(use_LU,lambda).move_to(*this); // Non-square matrix: Pseudoinverse\n\n      // Square matrix.\n      const double dete = _width>3?-1.:det();\n      if (dete!=0. && _width==2) {\n        const double\n          a = _data[0], c = _data[1],\n          b = _data[2], d = _data[3];\n        _data[0] = (T)(d/dete); _data[1] = (T)(-c/dete);\n        _data[2] = (T)(-b/dete); _data[3] = (T)(a/dete);\n      } else if (dete!=0. && _width==3) {","sourceCodeStart":32815,"sourceCodeEnd":32851,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L32815-L32851","documentation":"The in-place CImg::invert() inverts the image viewed as a matrix; a matrix requires _depth==1 and _spectrum==1. It throws CImgInstanceException when the instance is volumetric (depth>1) or multi-channel (spectrum>1), since matrix inversion is defined only for 2D single-channel data.","triggerScenarios":"Calling img.invert() on a color image (spectrum=3) or a 3D volume (depth>1), regardless of squareness.","commonSituations":"Loading an RGB image and trying to invert it as a matrix; calling invert() on a sequence of matrices stacked along depth or spectrum instead of one 2D matrix; passing an animated/image-sequence container.","solutions":["Extract a single 2D plane first, e.g. img.get_slice(0) or img.get_channel(0), then invert().","Loop over channels/planes and invert each separately.","Ensure the pipeline that produced the image yields depth=1, spectrum=1."],"exampleFix":"// before\nCImg<float> rgb = img.get_resize(...,3); // spectrum=3\nrgb.invert();\n// after\nCImg<float> m = img.get_channel(0); // single-channel 2D matrix\nm.invert();","handlingStrategy":"validation","validationCode":"if (img.depth()!=1 || img.spectrum()!=1) img = img.get_channel(0).get_slice(0); img.invert();","typeGuard":"bool is2DMatrix(const CImg<T>& m){ return m.depth()==1 && m.spectrum()==1; }","tryCatchPattern":"try { img.invert(); } catch (CImgInstanceException& e) { std::cerr << e.what() << '\\n'; }","preventionTips":["Flatten color/volumetric images to one 2D plane before inversion","Prefer get_channel()/get_slice() when input dimensionality is unknown","Keep a fixed (w,h,1,1) shape convention for matrix pipelines"],"tags":["cimg","matrix","inverse","dimensions"],"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"}