【发布时间】: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。他转换为灰度的答案同样好。