在 Python 中使用 OpenCV 显示图像
Manav Narula
2024年2月15日

在 Python 中,我们使用 OpenCV 库执行各种计算机视觉任务。这个库实现了各种技术,可以非常有效地处理图像。
处理图像的基本任务之一是显示它们。本教程将讨论使用 Python 中的 OpenCV 库显示图像。
使用 imshow()
函数在 Python 中使用 OpenCV 库显示图像
OpenCV 库中的 imshow()
函数显示图像。此函数不直接返回任何内容,而是创建一个显示图像的新窗口。
图像在 Python 中被读取为 numpy
数组。我们可以将此对象传递给 imshow()
函数,该函数将显示它。
我们还可以使用 window_name
参数在此函数中为窗口名称添加标题。请参阅下面的代码。
import cv2
img = cv2.imread("obj.png")
cv2.imshow("Show image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
在上面的代码中,我们使用 imread()
函数读取图像以创建存储该图像的对象。
该对象被传递给 imshow()
函数,图像显示在新窗口中。我们还为这个窗口添加了一个标题。
我们在 imshow()
方法之后使用了两个函数 waitKey()
和 destroyAllWindows()
,它可以防止新创建的窗口自动关闭。它等待用户在关闭它之前按下某个键。
作者: Manav Narula
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
LinkedIn