【问题标题】:thresh.cpp:1676: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'thresh.cpp:1676: 错误: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'
【发布时间】:2021-05-12 23:10:30
【问题描述】:

学习这个tutorial at OpenCV of Adaptive-Thresholding,复制了确切的代码

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('sudoku.jpg',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
            cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-m8us58q4\opencv\modules\imgproc\src\thresh.cpp:1676: 错误:(-215:断言失败)src.type()==函数中的CV_8UC1 'cv::adaptiveThreshold'
文件 “C:\Users\me\Documents\test\AdaptiveThresholding.py”,第 8 行,在 th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\

opencv-python 4.5.2.52

Python 3.9.5

【问题讨论】:

  • 使用img = cv.imread('sudoku.jpg',cv.IMREAD_GRAYSCALE) 与阈值兼容。
  • @fmw42 有效。虽然不能接受评论作为答案,但非常感谢您提供的信息。
  • 感谢下面的@Tim Roberts。他转换为灰度的答案同样好。

标签: python opencv


【解决方案1】:

这不是确切的代码。确切的代码读取灰度 PNG。你有一个彩色 JPG。这就是区别。自适应阈值需要灰度图像。所以,添加:

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

【讨论】:

  • 不仅抓住了失踪者,还提供了额外的指导。太棒了!
【解决方案2】:

将您所需的图像转换为灰度 bcz 自适应阈值需要灰度图像 例如: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

【讨论】:

  • 这与the accepted answer 中的解决方案相同。 在回答已有答案的旧问题时,请确保提供新颖的解决方案或比现有答案更好的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 2020-09-17
  • 2020-10-13
  • 2021-04-14
  • 2021-04-19
  • 2022-08-12
  • 2023-04-01
相关资源
最近更新 更多