0
votes
import numpy as np
import  cv2
import matplotlib.pyplot as plt
import matplotlib

img = cv2.imread('flood.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

Above is my code and when I run this program Ii get

"/home/badal/Python-3.7.1/image_process/im2.py:9: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
 plt.show()

After using tkagg I get

"Traceback (most recent call last):
File "/home/badal/Python-3.7.1/image_process/im2.py", line 5, in import tkinter as tk File "/usr/local/lib/python3.7/tkinter/init.py", line 36, in import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named '_tkinter'"

import numpy as np
import  cv2
import matplotlib.pyplot as plt
import matplotlib
import tkinter as tk
matplotlib.use('tkagg')

img = cv2.imread('flood.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

I have already installed tkinter, so I don't know what to do.

1
To read in an image using OpenCV you can do image = cv2.imread('image.png'). To display it, you can do cv2.imshow('image', image) with cv2.waitKey() - nathancy

1 Answers

0
votes

I think that the problem is on cv2 module that I don't know what is.

img = cv2.imread('flood.jpg',0)

However I made some changes.

First of all I don't import cv2 module.

Second I import Image from PIL.

And to open the pic I made

img = Image.open('flood.jpg')

So your code becomes:

import numpy as np
#import  cv2
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image

img = Image.open('flood.jpg')
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()

enter image description here