Yalantis/uCrop · error · CImgInstanceException

_cvmat2cimg() : pixel type '%s' is not supported.

Error message

_cvmat2cimg() : pixel type '%s' is not supported.

What it means

During conversion, _cvmat2cimg (via the cv::Mat bridge in _cimg2cvmat path) builds a CV_* mat_type from the CImg pixel type; if pixel_type() matches none of uchar/char/ushort/short/int/float/double, mat_type stays -1 and CImgInstanceException is thrown. The CImg<T> instantiation uses a pixel type OpenCV cannot represent.

Source

Thrown at ucrop/src/main/jni/CImg.h:60049

                                    cimg_instance);
      if (_spectrum==2)
        throw CImgInstanceException(_cimg_instance
                                    "_cimg2cvmat() : Invalid number of channels (should be '1' or '3+').",
                                    cimg_instance);
      if (_depth!=1)
        throw CImgInstanceException(_cimg_instance
                                    "_cimg2cvmat() : Invalid number of slices (should be '1').",
                                    cimg_instance);
      int mat_type = -1;
      if (pixel_type()==cimg::type<unsigned char>::string()) mat_type = CV_8UC1;
      if (pixel_type()==cimg::type<char>::string()) mat_type = CV_8SC1;
      if (pixel_type()==cimg::type<unsigned short>::string()) mat_type = CV_16UC1;
      if (pixel_type()==cimg::type<short>::string()) mat_type = CV_16SC1;
      if (pixel_type()==cimg::type<int>::string()) mat_type = CV_32SC1;
      if (pixel_type()==cimg::type<float>::string()) mat_type = CV_32FC1;
      if (pixel_type()==cimg::type<double>::string()) mat_type = CV_64FC1;
      if (mat_type<0)
        throw CImgInstanceException(_cimg_instance
                                    "_cvmat2cimg() : pixel type '%s' is not supported.",
                                    cimg_instance,pixel_type());
      cv::Mat res;
      if (_spectrum>1) {
        cv::Mat *const channels = new cv::Mat[_spectrum];
        cimg_forC(*this,c)
          channels[c] = cv::Mat(_height,_width,mat_type,_data + _width*_height*(_spectrum - 1 - c));
        cv::merge(channels,_spectrum,res);
        delete[] channels;
      } else res = cv::Mat(_height,_width,mat_type,_data).clone();

      return res;
    }

#endif

    //! Load image from a camera stream, using OpenCV.
    /**

View on GitHub (pinned to f788b534b4)

Solutions

  1. Convert/cast the image to a supported type first, e.g. img.get_cast<unsigned char>() or float.
  2. Use standard CImg<unsigned char>, <unsigned short>, <int>, <float>, or <double> for OpenCV interop.
  3. Store binary results as uchar instead of bool.

Example fix

// before
CImg<bool> mask = img.get_threshold(128);
cv::Mat m = mask.get_cvmat(); // unsupported type
// after
cv::Mat m = mask.get_cast<unsigned char>().get_cvmat();
Defensive patterns

Strategy: type-guard

Validate before calling

// compile-time: only instantiate interop with supported types
static_assert(std::is_same_v<T,unsigned char> || std::is_same_v<T,unsigned short> ||
              std::is_same_v<T,short> || std::is_same_v<T,int> ||
              std::is_same_v<T,float> || std::is_same_v<T,double>,
              "type not supported by cv::Mat conversion");

Type guard

template<class T> bool is_cvmat_supported() { return std::is_same_v<T,unsigned char> || std::is_same_v<T,char> || std::is_same_v<T,unsigned short> || std::is_same_v<T,short> || std::is_same_v<T,int> || std::is_same_v<T,float> || std::is_same_v<T,double>; }

Try / catch

try { cv::Mat m = img.get_cvmat(); }
catch (CImgInstanceException& e) { img.cast<unsigned char>(); /* retry as uchar */ }

Prevention

When it happens

Trigger: Instantiating CImg with an exotic type (e.g. CImg<bool>, CImg<long>, CImg<float complex-like custom>) and calling cvmat conversion; compile-time template instantiated with a non-standard arithmetic type.

Common situations: Custom fixed-point or extended-precision image types; template-generic code that accidentally instantiates with an unusual T; bool images from threshold operations kept as CImg<bool>.

Related errors


AI-assisted analysis of Yalantis/uCrop@f788b534b4 (2026-09-08). Data as JSON: /api/errors/bbedabd4074b0b0e. Report an issue: GitHub.