VideoCapture

from xgoedu import XGOEDU 
import time 
#实例化edu
XGO_edu = XGOEDU()  
XGO_edu.lcd_text(50,50,'hello',color=(255,0,0),fontsize=50)
time.sleep(2)

import cv2
import numpy as np

# 打开摄像头
cap = cv2.VideoCapture(0)
cap.set(3, 320)
cap.set(4, 240)

while True:
    # 读取摄像头中的图像
    ret, frame = cap.read()

    # 转换颜色空间为HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # 定义蓝色的范围
    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([130, 255, 255])

    # 根据阈值获取蓝色区域
    mask = cv2.inRange(hsv, lower_blue, upper_blue)

    # 对原图像和掩模进行位运算
    res = cv2.bitwise_and(frame, frame, mask=mask)

    # 显示图像
    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)

    # 按下q键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放资源
cap.release()
cv2.destroyAllWindows()

 

posted @ 2023-06-30 18:58  freedragon  阅读(57)  评论(0编辑  收藏  举报