opencv type_error ai_generated true

cv2.error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'equalizeHist'

ID: opencv/mat-type-assertion

Also available as: JSON · Markdown
90%Fix Rate
92%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4 active

Root Cause

OpenCV function received Mat with wrong type or channels. equalizeHist/Canny require grayscale.

generic

Workarounds

  1. 95% success Convert color space before calling the function
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    Sources: https://docs.opencv.org/4.x/

  2. 88% success Check actual type of your Mat
    print(img.shape, img.dtype)  # (H,W,3) uint8=BGR; (H,W) uint8=gray

Dead Ends

Common approaches that don't work:

  1. Convert to float32 assuming function needs float 75% fail

    Most OpenCV functions expect CV_8U (uint8). Float without scaling produces black images.

  2. Reshape Mat dimensions instead of converting color space 82% fail

    numpy reshape changes memory layout but not pixel format