Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read image grayscale opencv 3.0.0-dev

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.

   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.

Any suggestions?

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

like image 321
elaRosca Avatar asked Apr 28 '14 10:04

elaRosca


People also ask

How do I open an image in grayscale in OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do you read a cv2 image?

The first Command line argument is the image image = cv2. imread(sys. argv[1]) #The function to read from an image into OpenCv is imread() #imshow() is the function that displays the image on the screen. #The first value is the title of the window, the second is the image file we have previously read.

What is cv2 Imread ()?

cv2. imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.


3 Answers

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. imread flags start with IMREAD_, cvtColor flags start with COLOR_, etc.)

like image 180
Aurelius Avatar answered Oct 17 '22 13:10

Aurelius


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)
like image 35
Jibin Mathew Avatar answered Oct 17 '22 12:10

Jibin Mathew


Try this, it works for me everytime

import cv2
gray_img = cv2.imread('img.png', 0)
cv2.imshow(gray_img)
like image 1
Sarthak Vajpayee Avatar answered Oct 17 '22 14:10

Sarthak Vajpayee