Python 无法使用adaptiveThreshold:函数adaptiveThreshold 中的CV_8UC1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27014207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:19:15  来源:igfitidea点击:

Failure to use adaptiveThreshold: CV_8UC1 in function adaptiveThreshold

pythonopencv

提问by PhuongHoang

I have used openCV python and encountered an error.

我使用过 openCV python 并遇到错误。

img_blur = cv2.medianBlur(self.cropped_img,5)
img_thresh_Gaussian = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

plt.subplot(1,1,1),plt.imshow(img_thresh_Gaussian, cmap = 'gray')
plt.title("Image"), plt.xticks([]), plt.yticks([])
plt.show()

but I received:

但我收到了:

cv2.error: /home/phuong/opencv_src/opencv/modules/imgproc/src/thresh.cpp:1280: error: (-215) src.type() == CV_8UC1 in function adaptiveThreshold

Do I have to install something else?

我必须安装其他东西吗?

采纳答案by sam

you should load your file like this

你应该像这样加载你的文件

src.create(rows, cols, CV_8UC1);
src = imread(your-file, CV_8UC1);

and after that

在那之后

adaptiveThreshold(src, dst, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 75, 10);

回答by Salvador Dali

The problem is that you are trying to use adaptive thresholding to an image that is not in greyscale. And the function only works with a greyscale images.

问题是您正在尝试对非灰度图像使用自适应阈值。并且该功能仅适用于灰度图像。

So you have to convert your image to a greyscale format as it is described in documentation.

因此,您必须按照文档中的说明将图像转换为灰度格式。

They read the image in a greyscale format with: img = cv2.imread('dave.jpg',0). You can also convert it to greyscale with: img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

他们与灰度格式读取的图像:img = cv2.imread('dave.jpg',0)。您还可以使用以下方法将其转换为灰度:img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

回答by Vaibhav K

you can change the code to slightly like this :

您可以将代码更改为稍微像这样:

img_blur = cv2.medianBlur(self.cropped_img,5).astype('uint8')
img_thresh_Gaussian = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

just by adding ('uint8') while blurring has resolved my issue.

只需添加 ('uint8') 而模糊就解决了我的问题。

回答by Yazen WAKED

Try to add cv2.IMREAD_GRAYSCALE in the second parameter of imread

尝试在 imread 的第二个参数中添加 cv2.IMREAD_GRAYSCALE

img = cv2.imread(File_path, cv2.IMREAD_GRAYSCALE)

img = cv2.imread(File_path, cv2.IMREAD_GRAYSCALE)

回答by Aram.rt

you have to imreadyour image in grayscale, so use the code below:

您必须以灰度图像读取图像,因此请使用以下代码:

image = imread('address', 0)

0 exchange the channel of image to grayscale

0 将图像通道交换为灰度