Python错误集锦:OpenCV bilateralFilter双边滤波提示:bilateral_filter.dispatch.cpp:166: error: (-215:Assertion failed) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data in function ‘cv::bilateralFilter_8u’

原文链接:http://www.juzicode.com/opencv-error-bilateralfilter-src-type-not-cv8u

错误提示:

OpenCV bilateralFilter双边滤波提示:bilateral_filter.dispatch.cpp:166: error: (-215:Assertion failed) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data in function ‘cv::bilateralFilter_8u’

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('opencv-logo.png',cv2.IMREAD_UNCHANGED)
print(img.shape)
img_blur=cv2.bilateralFilter(img,7,10,10)

cv2.imshow('img_blur',img_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
(739, 600, 4)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-5-b9e52ea6f0a1> in <module>
      4 img = cv2.imread('opencv-logo.png',cv2.IMREAD_UNCHANGED)
      5 print(img.shape)
----> 6 img_blur=cv2.bilateralFilter(img,7,10,10)
      7 
      8 cv2.imshow('img_blur',img_blur)

error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\imgproc\src\bilateral_filter.dispatch.cpp:166: error: (-215:Assertion failed) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data in function 'cv::bilateralFilter_8u'

错误原因:

1、bilateralFilter的入参src表示的图像使用CV_8U类型时,只能是1通道或3通道的图像类型,上述例子中打印img.shpae是4通道图像,所以报错。

解决方法:

1、读入图像时不使用cv2.IMREAD_UNCHANGED标志,默认转换为3通道类型CV_8UC3。

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('opencv-logo.png' )
print(img.shape)
img_blur=cv2.bilateralFilter(img,7,10,10)

cv2.imshow('img_blur',img_blur)
cv2.waitKey(0)
cv2.destroyAllWindows()

扩展内容:

  1. OpenCV-Python教程
  2. OpenCV-Python教程:高斯平滑、双边平滑


如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注