-1

I have been working on ocr, so I need to load images and convert it to gray scale I tried to load the image as below

filename = "sample.jpg"
image = cv2.imread(filename)
plt.imshow(image)

it gave image which is what I need.

when I tried to load as grayscale image as

filename = "sample.jpg"
image = cv2.imread(filename,0)
plt.imshow(image)

the loaded image wasn't grayscale. Any sort of help is appreciated, thank you. link to image - https://i.sstatic.net/L1i44.png

0

2 Answers 2

1

it was enough just to look for what matplotlib display a grayscale image in color ..., there are plenty of forums on that. See this explanation

Here is the correct code:

import cv2
import matplotlib.pyplot as plt

filename = "image.jpg"
image = cv2.imread(filename, 0)
plt.imshow(image, cmap='gray')
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

-1

just use opencv its faster:

import cv2
image = cv2.imread('yourImg.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Original image',image)
cv2.imshow('Gray image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.