{"record":{"id":"5004725b1a567869","repo":"Yalantis/uCrop","slug":"cimg2cvmat-instance-image-is-empty","errorCode":null,"errorMessage":"_cimg2cvmat() : Instance image is empty.","messagePattern":"_cimg2cvmat\\(\\) : Instance image is empty\\.","errorType":"exception","errorClass":"CImgInstanceException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":60029,"sourceCode":"#ifdef cimg_use_opencv\n\n    // Convert a continuous cv::Mat<uchar> to a CImg<uchar>.\n    static CImg<ucharT> _cvmat2cimg(const cv::Mat &src) {\n      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;","sourceCodeStart":60011,"sourceCodeEnd":60047,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L60011-L60047","documentation":"The OpenCV bridge function _cimg2cvmat() requires a non-empty CImg image to convert to a cv::Mat. When the instance image has zero dimensions (is_empty() is true, e.g. default-constructed or never loaded), CImgInstanceException is thrown before any conversion is attempted.","triggerScenarios":"Calling .get_cvmat()/_cimg2cvmat() on an image that was default-constructed, on the result of a failed load() (which returns an empty image), or after assign() cleared it.","commonSituations":"Forgetting to check that load() succeeded (CImg returns an empty image instead of throwing when exception_mode is 0); passing a freshly constructed CImg<T> img; a pipeline stage upstream produced no output.","solutions":["Check img.is_empty() before converting and load/assign the image first.","Verify the load call that populated the image succeeded (file existed, format supported).","Restructure code so conversion happens only after a successful load/assignment."],"exampleFix":"// before\nCImg<unsigned char> img;\ncv::Mat m = img.get_cvmat(); // throws: empty\n// after\nCImg<unsigned char> img;\nimg.load(\"photo.png\");\nif (img.is_empty()) throw std::runtime_error(\"image failed to load\");\ncv::Mat m = img.get_cvmat();","handlingStrategy":"validation","validationCode":"if (img.is_empty()) throw std::runtime_error(\"cannot convert: image is empty\");","typeGuard":null,"tryCatchPattern":"try { cv::Mat m = img.get_cvmat(); }\ncatch (CImgInstanceException& e) { /* handle empty-image case */ }","preventionTips":["Always check is_empty() after load()","Remember load() returns an empty image on failure when exception_mode is 0","Assert non-empty at pipeline boundaries"],"tags":["cimg","opencv","empty-image","conversion"],"backgroundTag":"empty-required-field","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"}