gpt4 book ai didi

python - cv2.adaptiveThreshold()返回空矩阵

转载 作者:行者123 更新时间:2023-12-02 17:41:10 24 4
gpt4 key购买 nike

    # Read image in grayscale mode
img = cv2.imread(inp_pic,0)

# Median Blur and Gaussian Blur to remove Noise
img = cv2.medianBlur(img,3)
img = cv2.GaussianBlur(img, (5, 5), 0)
print(img)

# Adaptive Threshold for handling lightning
im_th = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,5)
print(im_th)

函数自适应阈值在这里返回一个空矩阵,但是正在传递的图像(img)不是一个空矩阵(我使用print语句检查了)为什么会这样?

您可以在这里找到源代码:
https://github.com/tanmay-edgelord/HandwrittenDigitRecognition/blob/master/performRecognition.ipynb

最佳答案

我在https://github.com/tanmay-edgelord/HandwrittenDigitRecognition/blob/master/Digit1.jpg上的图像上运行了您的代码,它按预期工作。我所做的唯一更改是使用cv2.imshow()显示图像。

import numpy as np
import cv2
# Read image in grayscale mode
img = cv2.imread(r"path\to\img",0)

# Median Blur and Gaussian Blur to remove Noise
img = cv2.medianBlur(img,3)
img = cv2.GaussianBlur(img, (5, 5), 0)
#print(img)

# Adaptive Threshold for handling lightning
im_th = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,11,5)
cv2.imshow("te", im_th)
if cv2.waitKey():
cv2.destroyAllWindows()

结果如下:

enter image description here

您为什么确定这是一个空矩阵?如果只是从 [0 0 0 ..., 0 0 0]来看,这是一个很大的数组。 Python只是将其显示为数组的简写形式。只需使用 np.maximum(img_th)即可获取数组中的最大值,您将看到最大值为非零。

关于python - cv2.adaptiveThreshold()返回空矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com