opencv – How can one display an image using cv2 in Python

opencv – How can one display an image using cv2 in Python

As far as I can see, you are doing it almost good. There is one thing missing:

cv2.imshow(image,img)
cv2.waitKey(0)

So probably your window appears but is closed very very fast.

you can follow following code

import cv2
# read image 
image = cv2.imread(path to your image)
# show the image, provide window name first
cv2.imshow(image window, image)
# add wait key. window waits until user presses a key
cv2.waitKey(0)
# and finally destroy/close all open windows
cv2.destroyAllWindows()

I think your job is done then

opencv – How can one display an image using cv2 in Python

Since OpenCV reads images with BGR format, youd convert it to RGB format before pass the image to pyplot

import cv2
import matplotlib.pyplot as plt

image = cv2.imread(YOUR_FILEPATH)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()

Leave a Reply

Your email address will not be published. Required fields are marked *