-215 opencv type_error ai_generated true

cv::error: (-215:Assertion failed) src1.type() == CV_8UC1 in function 'calcOpticalFlowFarneback'

ID: opencv/opticalflow-calcopticalflowfarneback-input-type-mismatch

Also available as: JSON · Markdown · 中文
90%Fix Rate
88%Confidence
1Evidence
2023-03-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4.5.5 active
4.6.0 active
4.7.0 active
4.8.0 active
4.9.0 active

Root Cause

calcOpticalFlowFarneback requires the input image to be single-channel 8-bit (grayscale), but a multi-channel or different depth image was provided.

generic

中文

calcOpticalFlowFarneback 要求输入图像为单通道 8 位(灰度图),但提供了多通道或不同深度的图像。

Official Documentation

https://docs.opencv.org/4.x/dc/d6b/group__video__track.html#ga5d10ebbd59fe09c1f0f8e3c9d0d2c5b9

Workarounds

  1. 95% success Convert the input image to grayscale before calling calcOpticalFlowFarneback: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY); ensure gray is a single-channel 8-bit image.
    Convert the input image to grayscale before calling calcOpticalFlowFarneback: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY); ensure gray is a single-channel 8-bit image.
  2. 85% success If the input is already grayscale but has a different depth (e.g., 16-bit), use cv2.convertScaleAbs to convert to CV_8U: gray = cv2.convertScaleAbs(gray, alpha=(255.0/65535.0)).
    If the input is already grayscale but has a different depth (e.g., 16-bit), use cv2.convertScaleAbs to convert to CV_8U: gray = cv2.convertScaleAbs(gray, alpha=(255.0/65535.0)).

中文步骤

  1. Convert the input image to grayscale before calling calcOpticalFlowFarneback: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY); ensure gray is a single-channel 8-bit image.
  2. If the input is already grayscale but has a different depth (e.g., 16-bit), use cv2.convertScaleAbs to convert to CV_8U: gray = cv2.convertScaleAbs(gray, alpha=(255.0/65535.0)).

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The function signature expects a single channel; a 3-channel input will still trigger the assertion.

  2. 80% fail

    The original multi-channel image remains unchanged, causing the assertion to fail again.