OpenCV识别蓝色

使用openCV识别蓝色

# -*- coding: utf-8 -*-
"""
Created on Thu Sep  3 13:51:46 2020
"""
import numpy as np
import cv2

# 读取摄像头
cap=cv2.VideoCapture(0)

while(1):
    
    # 获取摄像头图片
    _,frame=cap.read()
    # 转换图片为hsv
    hsv=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    # 设置蓝色hsv上下限
    lower_blue=np.array([110,43,46])
    upper_blue=np.array([124,255,255])
   # lower_red=np.array([0,0,221])
   # high_red=np.array([180,30,255])
    # 设置掩码图片
    mask=cv2.inRange(hsv,lower_blue,upper_blue)
    
    # mask=cv2.inRange(hsv,lower_red,high_red)
    # 将两张图片合并
    res=cv2.bitwise_and(frame,frame,mask=mask)
    # 原图片
    cv2.imshow('frame',frame)
    # 掩码图片
    cv2.imshow('mask',mask)
    # 结果集
    cv2.imshow('res',res)
    k=cv2.waitKey(5) & 0xff
    if k==27:
        break
    
cv2.destroyAllWindows()
    
    

你可能感兴趣的:(python,opencv,图像识别)