You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV Watershed算法报错:src.type() == CV_8UC3 && dst.type() == CV_32SC1

OpenCV Watershed断言错误原因分析

错误场景

执行cv2.watershed(img, markers)时触发断言错误:

watershed_img = cv2.watershed(img, markers) cv2.error: OpenCV(4.8.0)
/io/opencv/modules/imgproc/src/segmentation.cpp:161: error:
(-215:Assertion failed) src.type() == CV_8UC3 && dst.type() ==
CV_32SC1 in function 'watershed'

相关输出信息:

print(img.shape, markers.shape)
(405, 304) (405, 304)
 
print(type(img), type(markers))
<class 'numpy.ndarray'> <class 'numpy.ndarray'>

错误原因

从错误信息的断言条件可直接定位问题:

  • 输入图像img不满足要求:src.type() == CV_8UC3要求输入必须是8位无符号3通道彩色图像,但你的img.shape为(405,304),说明这是单通道灰度图,通道数不符合要求。
  • 标记图markers不满足要求:dst.type() == CV_32SC1要求标记图必须是32位有符号整数类型的单通道数组,你仅确认了它是numpy数组,但未验证数据类型,大概率是其他整数类型(比如8位整数),不满足32位有符号整数的要求。

解决方向

  1. 将输入图像转为3通道:若为灰度图,可使用cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)进行转换。
  2. 确保标记图类型为np.int32:通过markers = markers.astype(np.int32)强制转换类型,同时保证标记图尺寸与输入图像一致(你的尺寸是符合要求的)。

内容的提问来源于stack exchange,提问作者Tms91

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 09:42:34