This code demonstrates how to read an image, display it, and wait for user input before closing the window using OpenCV in Python.

import cv2
# 读入图像
img=cv2.imread('1.jpg')
# 显示图像
cv2.imshow('demo',img)
# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation:

  • cv2.imread('1.jpg'): Reads the image from the specified file path and returns it as a NumPy array.
  • cv2.imshow('demo',img): Displays the image in a window named 'demo'.
  • cv2.waitKey(0): Pauses the execution until a key is pressed. This keeps the window open until the user interacts with it.
  • cv2.destroyAllWindows(): Closes all open windows created by OpenCV.

Note: There was a typo in the original code. The correct method to close all windows is cv2.destroyAllWindows() (not cv2.destoryAllWindows()). This corrected code ensures proper window closure.

Python OpenCV: Reading and Displaying Images

原文地址: https://www.cveoy.top/t/topic/pdTu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录