Yalantis/uCrop · error · CImgInstanceException

_cimg_instance "median(): Empty instance."

Error message

_cimg_instance "median(): Empty instance."

What it means

Instance guard in CImg<T>::median(): computing the median requires a non-empty pixel array to sort/select from; the image is empty, so the quickselect-based median is undefined and the call is rejected.

Solutions

  1. Check is_empty() before calling median()
  2. Verify the image was populated (e.g. load succeeded, size() > 0)
  3. Fix zero-size crops/ROIs feeding the median call
  4. Catch CImgInstanceException around the operation

Example fix

// before
const T med = img.median();
// after
if (!img.is_empty()) {
  const T med = img.median();
} else {
  // choose a default or propagate an error
}
Defensive patterns

Strategy: validation

Validate before calling

if (img.is_empty()) throw std::runtime_error("image empty before median()");

Type guard

bool usable = !img.is_empty() && img.size() > 0;

Try / catch

try { const T med = img.median(); } catch (const CImgInstanceException& e) { /* default or error */ }

Prevention

When it happens

Trigger: Calling CImg<T>::median() on an empty instance: default-constructed CImg, zero dimensions, or data lost after a failed load/transform.

Common situations: Median filtering or noise estimation on unloaded images; image statistics on the result of a crop that selected nothing.

Related errors


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

Appendix: source

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

            do ++i; while (arr[i]<pivot);
            do --j; while (arr[j]>pivot);
            if (j<i) break;
            cimg::swap(arr[i],arr[j]);
          }
          arr[l + 1] = arr[j];
          arr[j] = pivot;
          if (j>=k) ir = j - 1;
          if (j<=k) l = i;
        }
      }
    }

    //! Return the median pixel value.
    /**
     **/
    T median() const {
      if (is_empty())
        throw CImgInstanceException(_cimg_instance
                                    "median(): Empty instance.",
                                    cimg_instance);
      const ulongT s = size();
      switch (s) {
      case 1 : return _data[0];
      case 2 : return cimg::median(_data[0],_data[1]);
      case 3 : return cimg::median(_data[0],_data[1],_data[2]);
      case 5 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4]);
      case 7 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6]);
      case 9 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8]);
      case 13 : return cimg::median(_data[0],_data[1],_data[2],_data[3],_data[4],_data[5],_data[6],_data[7],_data[8],
                                    _data[9],_data[10],_data[11],_data[12]);
      }
      const T res = kth_smallest(s>>1);
      return (s%2)?res:(T)((res + kth_smallest((s>>1) - 1))/2);
    }

    //! Return greatest common diviser of all image values.

View on GitHub (pinned to f788b534b4)