-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.stack.imgur.com/L1i44.png

RashidLadj_Winux
  • 810
  • 1
  • 6
  • 17
swasthik nayak
  • 605
  • 1
  • 5
  • 11

2 Answers2

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()
RashidLadj_Winux
  • 810
  • 1
  • 6
  • 17
-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()
Jasar Orion
  • 626
  • 7
  • 26