简体   繁体   中英

OpenCV not showing image in python

This is the code that I am using for OpenCV to display image. It only shows me a blank screen instead of showing a picture.

import cv2

# location and name of file is completely correct
img = cv2.imread("./Resources/img-2.jpg")

# Doesn't give a null so its okay
print(img.shape)

# suspecting that problem is here
cv2.imshow("preview", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

The image is stored in the right location and when I'm using a similar approach for a video and a webcam, it works perfectly. The following is what the out is -

输出图像

Try using matplotlib instead:

import matplotlib.pyplot as plt
import cv2

img = cv2.imread("./Resources/img-2.jpg")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert img pixels to RGB format, so that matplotlib displays the image properly

plt.imshow(img)
plt.show()

If it still gives you a blank image, then the problem might come from your file or filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM