如何用Python和Matplotlib连接并打开相机?

2026-05-28 13:011阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计149个文字,预计阅读时间需要1分钟。

如何用Python和Matplotlib连接并打开相机?

如何通过按键保存图片呢?pythonimport cv2import matplotlib.pyplot as pltcapture=cv2.VideoCapture(0)fig, ax=plt.subplots()plt.ion()if capture.isOpened(): while True:


如何用Python和Matplotlib连接并打开相机?

如何通过按键保存图片呢???

import cv2
import matplotlib.pyplot as plt

capture = cv2.VideoCapture(0)
fig = plt.figure()
plt.ion()

if capture is not None:
while (True):
# 获取一帧
ret, frame = capture.read()
if frame is not None:
# opencv
# cv2.imshow("camera", frame)
# cv2.waitKey(1)

# matplotlib
plt.imshow(frame)
plt.pause(0.1)
fig.clf()
else:
print("Fail to grab")
continue
else:
print("Fail to open camera")
plt.ioff()


本文共计149个文字,预计阅读时间需要1分钟。

如何用Python和Matplotlib连接并打开相机?

如何通过按键保存图片呢?pythonimport cv2import matplotlib.pyplot as pltcapture=cv2.VideoCapture(0)fig, ax=plt.subplots()plt.ion()if capture.isOpened(): while True:


如何用Python和Matplotlib连接并打开相机?

如何通过按键保存图片呢???

import cv2
import matplotlib.pyplot as plt

capture = cv2.VideoCapture(0)
fig = plt.figure()
plt.ion()

if capture is not None:
while (True):
# 获取一帧
ret, frame = capture.read()
if frame is not None:
# opencv
# cv2.imshow("camera", frame)
# cv2.waitKey(1)

# matplotlib
plt.imshow(frame)
plt.pause(0.1)
fig.clf()
else:
print("Fail to grab")
continue
else:
print("Fail to open camera")
plt.ioff()