打开摄像头.拍摄两张图片进行对比内容代码
时间: 2023-06-17 20:08:58 浏览: 114
以下是使用Python和OpenCV库打开摄像头、拍摄两张图片并进行对比的示例代码:
```python
import cv2
# 打开摄像头
cap = cv2.VideoCapture(0)
# 拍摄第一张图片
ret, frame1 = cap.read()
# 拍摄第二张图片
ret, frame2 = cap.read()
# 关闭摄像头
cap.release()
# 对比两张图片
difference = cv2.absdiff(frame1, frame2)
gray = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
_, threshold = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 显示结果
cv2.imshow("Frame 1", frame1)
cv2.imshow("Frame 2", frame2)
cv2.imshow("Difference", difference)
cv2.imshow("Threshold", threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
该代码首先打开摄像头并拍摄第一张图片,然后再拍摄第二张图片。接着,使用OpenCV库中的函数对比两张图片并显示结果。最后,关闭摄像头并释放资源。
阅读全文
相关推荐


















