Python 读取图像灰度 opencv 3.0.0-dev

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23339315/
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 02:47:15  来源:igfitidea点击:

Read image grayscale opencv 3.0.0-dev

pythonopencvgrayscale

提问by elaRosca

I am trying to read images directly as black and white.

我正在尝试将图像直接读取为黑白图像。

I recently updated my OpenCv version to 3.0.0-dev, and the code that I used before does not work anymore.

我最近将我的 OpenCv 版本更新为 3.0.0-dev,我之前使用的代码不再起作用了。

   img = cv2.imread(f, cv2.CV_LOAD_IMAGE_GRAYSCALE)

works fine for 2.4 but does not work for the new version, as there is no field CV_LOAD_IMAGE_GRAYSCALE.

适用于 2.4 但不适用于新版本,因为没有 field CV_LOAD_IMAGE_GRAYSCALE

Any suggestions?

有什么建议?

Note: I know that cv2.imread(f,0)will work, but I do not like having unnamed constants in my code. Thanks!

注意:我知道这cv2.imread(f,0)会起作用,但我不喜欢在我的代码中使用未命名的常量。谢谢!

采纳答案by Aurelius

The flag has been renamed to cv2.IMREAD_GRAYSCALE. Generally speaking, flags now have names prefixed in a manner that relates to the function to which they refer. (e.g. imreadflags start with IMREAD_, cvtColorflags start with COLOR_, etc.)

该标志已重命名为cv2.IMREAD_GRAYSCALE. 一般来说,标志现在的名称前缀与它们所引用的函数有关。(例如imread标志以 开头IMREAD_cvtColor标志以 开头COLOR_,等等)

回答by Jibin Mathew

Try this it works for me

试试这个对我有用

import cv2
im_gray = cv2.imread('gray_image.png', cv2.IMREAD_GRAYSCALE)
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
cv2.imwrite('blackwhite.png', im_bw)

回答by Sarthak Vajpayee

Try this, it works for me everytime

试试这个,它每次都对我有用

import cv2
gray_img = cv2.imread('img.png', 0)
cv2.imshow(gray_img)