{"record":{"id":"55b9c5630c46e580","repo":"Yalantis/uCrop","slug":"cimg2cvmat-invalid-number-of-channels-should","errorCode":null,"errorMessage":"_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').","messagePattern":"_cimg2cvmat\\(\\) : Invalid number of channels \\(should be '1' or '3\\+'\\)\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":60033,"sourceCode":"      if (src.channels()==1) return CImg<ucharT>(src.ptr(),src.cols,src.rows,1,1);\n      else if (src.channels()==3) { // BGR\n        CImg<ucharT> res(src.cols,src.rows,1,src.channels());\n        const unsigned char *ptrs = src.ptr();\n        unsigned char *pR = res.data(), *pG = res.data(0,0,0,1), *pB = res.data(0,0,0,2);\n        cimg_forXY(res,x,y) { *(pB++) = *(ptrs++); *(pG++) = *(ptrs++); *(pR++) = *(ptrs++); }\n        return res;\n      }\n      return CImg<ucharT>(src.ptr(),src.channels(),src.cols,src.rows,1,true).get_permute_axes(\"yzcx\");\n    }\n\n    // Convert a CImg<T> to a cv::Mat.\n    cv::Mat _cimg2cvmat() const {\n      if (is_empty())\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cimg2cvmat() : Instance image is empty.\",\n                                    cimg_instance);\n      if (_spectrum==2)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').\",\n                                    cimg_instance);\n      if (_depth!=1)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cimg2cvmat() : Invalid number of slices (should be '1').\",\n                                    cimg_instance);\n      int mat_type = -1;\n      if (pixel_type()==cimg::type<unsigned char>::string()) mat_type = CV_8UC1;\n      if (pixel_type()==cimg::type<char>::string()) mat_type = CV_8SC1;\n      if (pixel_type()==cimg::type<unsigned short>::string()) mat_type = CV_16UC1;\n      if (pixel_type()==cimg::type<short>::string()) mat_type = CV_16SC1;\n      if (pixel_type()==cimg::type<int>::string()) mat_type = CV_32SC1;\n      if (pixel_type()==cimg::type<float>::string()) mat_type = CV_32FC1;\n      if (pixel_type()==cimg::type<double>::string()) mat_type = CV_64FC1;\n      if (mat_type<0)\n        throw CImgInstanceException(_cimg_instance\n                                    \"_cvmat2cimg() : pixel type '%s' is not supported.\",\n                                    cimg_instance,pixel_type());","sourceCodeStart":60015,"sourceCodeEnd":60051,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L60015-L60051","documentation":"_cimg2cvmat() cannot represent a CImg with exactly 2 channels (spectrum == 2), since cv::Mat has no 2-channel mapping in this bridge; it accepts 1 (gray) or 3+ (color/multi-channel) channel images. The image is otherwise valid, so this is a shape-incompatibility error between CImg and OpenCV.","triggerScenarios":"Converting a CImg whose _spectrum was set to 2 — e.g. an image constructed with spectrum=2, a two-band multispectral image, or after slicing channel ranges that yield exactly two channels.","commonSituations":"Working with two-channel data (e.g. L+a, depth+mask pairs, dual-band scientific imagery) and calling get_cvmat() directly; accidentally computing channels=2 in a constructor call.","solutions":["Pad the image to 3 channels (e.g. duplicate or add a zero channel) before conversion.","Split the two channels and convert each as a single-channel cv::Mat.","Fix upstream construction so the image has 1, 3, or 4 channels."],"exampleFix":"// before: 2-channel image\ncv::Mat m = img2ch.get_cvmat(); // throws\n// after\ncimg_forXY(img2ch,x,y) img2ch(x,y,0,2) = 0; // pad to 3 channels\nCImg<unsigned char> img3(img2ch, \"xyzc\", 3); // or append_channel\ncv::Mat m = img3.get_cvmat();","handlingStrategy":"validation","validationCode":"if (img.spectrum() == 2) throw std::runtime_error(\"2-channel image not convertible to cv::Mat; pad or split channels\");","typeGuard":null,"tryCatchPattern":"try { cv::Mat m = img.get_cvmat(); }\ncatch (CImgInstanceException& e) { /* pad/split channels and retry */ }","preventionTips":["Keep OpenCV-interop images at 1, 3, or 4 channels","Audit channel counts when slicing/merging channels","Document that CImg<->Mat bridge rejects spectrum==2"],"tags":["cimg","opencv","channels","shape-mismatch"],"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"}