{"record":{"id":"f5488c3ddef07b53","repo":"Yalantis/uCrop","slug":"svd-instance-has-invalid-dimensions-depth-or-c","errorCode":null,"errorMessage":"SVD(): Instance has invalid dimensions (depth or channels different from 1).","messagePattern":"SVD\\(\\): Instance has invalid dimensions \\(depth or channels different from 1\\)\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":33446,"sourceCode":"       \\param[out] V Third matrix of the SVD product.\n       \\param sorting Tells if the diagonal coefficients are sorted (in decreasing order).\n       \\param max_iteration Maximum number of iterations considered for the algorithm convergence.\n       \\param lambda Epsilon used for the algorithm convergence.\n       \\note The instance matrix can be computed from \\c U,\\c S and \\c V by\n       \\code\n       const CImg<> A; // Input matrix (assumed to contain some values)\n       CImg<> U,S,V;\n       A.SVD(U,S,V)\n       \\endcode\n    **/\n    template<typename t>\n    const CImg<T>& SVD(CImg<t>& U, CImg<t>& S, CImg<t>& V, const bool sorting=true,\n                       const unsigned int max_iteration=40, const float lambda=0) const {\n      typedef _cimg_Ttfloat Ttfloat;\n      const Ttfloat eps = (Ttfloat)1e-8f;\n      if (is_empty()) { U.assign(); S.assign(); V.assign(); }\n      else if (_depth!=1 || _spectrum!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"SVD(): Instance has invalid dimensions (depth or channels different from 1).\",\n                                    cimg_instance);\n      else {\n        U = *this;\n        if (lambda!=0) {\n          const unsigned int delta = std::min(U._width,U._height);\n          for (unsigned int i = 0; i<delta; ++i) U(i,i) = (t)(U(i,i) + lambda);\n        }\n        if (S.size()<_width) S.assign(1,_width);\n        if (V._width<_width || V._height<_height) V.assign(_width,_width);\n        CImg<t> rv1(_width);\n        Ttfloat anorm = 0, c, f, g = 0, h, s, scale = 0;\n        int l = 0;\n\n        cimg_forX(U,i) {\n          l = i + 1;\n          rv1[i] = scale*g;\n          g = s = scale = 0;","sourceCodeStart":33428,"sourceCodeEnd":33464,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L33428-L33464","documentation":"SVD() decomposes a 2D single-channel matrix (width x height) and rejects instances whose _depth != 1 or _spectrum != 1, since singular value decomposition is only defined for the 2D matrix stored in the x-y plane.","triggerScenarios":"Calling SVD(U,S,V) on a volumetric image (_depth>1) or a multi-channel/color image (_spectrum>1).","commonSituations":"Forgetting to extract one slice/channel of an RGB or 3D image before SVD; reusing code that operated on image stacks; passing a whole tensor when a single matrix was intended.","solutions":["Extract a single 2D matrix first: img.get_slice(z).get_channel(c) before calling SVD","Verify _depth==1 && _spectrum==1 (via img.depth()/img.spectrum()) as a precondition","If you need SVD of many slices, loop over slices calling SVD per 2D matrix"],"exampleFix":"// before\nvolume.SVD(U, S, V); // depth=10\n// after\nCImg<float> m = volume.get_slice(0).get_channel(0);\nm.SVD(U, S, V);","handlingStrategy":"validation","validationCode":"if (img.depth() == 1 && img.spectrum() == 1) { img.SVD(U, S, V); } else { /* extract slice/channel first */ }","typeGuard":"bool is2DMatrix(const CImg<T>& m) { return m.depth() == 1 && m.spectrum() == 1; }","tryCatchPattern":"try { img.SVD(U, S, V); } catch (CImgInstanceException& e) { /* degrade to per-slice SVD or error out */ }","preventionTips":["Extract slices/channels before matrix operations","Add depth/spectrum assertions at pipeline boundaries","Keep SVD inputs produced by dedicated 2D matrix builders"],"tags":["cimg","svd","linear-algebra","matrix-shape"],"backgroundTag":"tensor-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"}