赞
踩
图像是颜色的矩阵
1、拉流
- import cv2 as cv
- import numpy as np
-
- cam = cv.VideoCapture(0)
- cam.set(3,300)
-
- #视频帧率
- cam.set(5, 30)
- cam.set(cv.CAP_PROP_BRIGHTNESS, 50) #设置亮度 -64 - 64 0.0
- cam.set(cv.CAP_PROP_CONTRAST, 0) #设置对比度 -64 - 64 2.0
- cam.set(cv.CAP_PROP_EXPOSURE, 1000) #设置曝光值 1.0 - 5000 156.0
2、读取与处理视频流
- while True:
- # 读取视频流
- ret, frame = cam.read()
- #print(frame.shape)#查看图像大小
-
- #处理视频流
- b,g,r = cv.split(frame)
- #创建不同颜色的通道
- blue = cv.merge((b,blank,blank))
- green = cv.merge((blank,g,blank))
- red = cv.merge((blank,blank,r))
-
- # 显示视频流
- cv.imshow('Color-Channel', frame)
- cv.imshow('blue', blue)
- cv.imshow('green', green)
- cv.imshow('red', red)
3、关闭视频流
- # ESC键退出
- keyCode = cv.waitKey(30) & 0xFF
- if keyCode == 27:
- break
-
- cam.release()
- cv.destroyAllWindows()
4、使用numpy创建空白图像
- #用numpy绘制一个空白画面
- blank = np.zeros([240,320,1],np.uint8)
- cv.imshow('blank',blank)
5、显示效果
所有代码
- # -*- coding: utf-8 -*-
- import cv2 as cv
- import numpy as np
-
- cam = cv.VideoCapture(0)
- cam.set(3,300)
-
- #视频帧率
- cam.set(5, 30)
- cam.set(cv.CAP_PROP_BRIGHTNESS, 50) #设置亮度 -64 - 64 0.0
- cam.set(cv.CAP_PROP_CONTRAST, 0) #设置对比度 -64 - 64 2.0
- cam.set(cv.CAP_PROP_EXPOSURE, 1000) #设置曝光值 1.0 - 5000 156.0
-
- #用numpy绘制一个空白画面
- blank = np.zeros([240,320,1],np.uint8)
-
- while True:
- # 读取视频流
- ret, frame = cam.read()
- #print(frame.shape)#查看图像大小
-
- #处理视频流
- b,g,r = cv.split(frame)
- #创建不同颜色的通道
- blue = cv.merge((b,blank,blank))
- green = cv.merge((blank,g,blank))
- red = cv.merge((blank,blank,r))
- merge=cv.merge((b,g,r))
-
- # 显示视频流
- cv.imshow('Color-Channel', frame)
- cv.imshow('blue', blue)
- cv.imshow('green', green)
- cv.imshow('red', red)
- cv.imshow('blank',blank)
- cv.imshow('merge',merge)
-
- # ESC键退出
- keyCode = cv.waitKey(30) & 0xFF
- if keyCode == 27:
- break
-
- cam.release()
- cv.destroyAllWindows()
HSV颜色模型
代码
- # -*- coding: utf-8 -*-
- import cv2 as cv
- import numpy as np
-
- def nothing(x):
- pass
-
- cv.namedWindow('trackbars')
- cv.resizeWindow('trackbars', 400,500)
- cv.createTrackbar('huelower','trackbars',50,179,nothing)
- cv.createTrackbar('huehigher','trackbars',100,179,nothing)
- #追踪两种颜色
- cv.createTrackbar('hue2lower','trackbars',50,179,nothing)
- cv.createTrackbar('hue2higher','trackbars',100,179,nothing)
-
- cv.createTrackbar('satlow','trackbars',100,255,nothing)
- cv.createTrackbar('sathigh','trackbars',255,255,nothing)
- cv.createTrackbar('vallow','trackbars',100,255,nothing)
- cv.createTrackbar('valhigh','trackbars',255,255,nothing)
-
- cam = cv.VideoCapture(0)
- cam.set(3,300)
- #用numpy绘制一个空白画面
- blank = np.zeros([240,320,1],np.uint8)
-
- while True:
- # 读取视频流
- ret, frame = cam.read()
- #print(frame.shape)#查看图像大小
- #处理视频流
- hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
- hl = cv.getTrackbarPos('huelower','trackbars')
- hu = cv.getTrackbarPos('huehigher','trackbars')
-
- hl2 = cv.getTrackbarPos('hue2lower', 'trackbars')
- hu2 = cv.getTrackbarPos('hue2higher', 'trackbars')
-
- sl = cv.getTrackbarPos('satlow','trackbars')
- sh = cv.getTrackbarPos('sathigh','trackbars')
- vl = cv.getTrackbarPos('vallow','trackbars')
- vh = cv.getTrackbarPos('valhigh','trackbars')
- # 选取颜色范围
- lower = np.array([hl, sl,vl]) # 转换为一维数组
- upper = np.array([hu, sh, vh])
-
- lower2 = np.array([hl2, sl,vl]) # 转换为一维数组
- upper2 = np.array([hu2, sh, vh])
-
-
- FGmask = cv.inRange(hsv,lower,upper) # 设置HSV的阈值
- FGmask2 = cv.inRange(hsv,lower2,upper2) # 设置HSV的阈值
- FGmaskComp=cv.add(FGmask, FGmask2)
- cv.imshow('FGmaskComp', FGmask)
- FG = cv.bitwise_and(frame,frame, mask=FGmaskComp) # 将掩膜和图像逐像素相加
- cv.imshow('FG',FG)
- BGmask = cv.bitwise_not(FGmaskComp)
- cv.imshow('BGmask',BGmask)
- BG = cv.cvtColor(BGmask,cv.COLOR_GRAY2BGR)
- cv.imshow('BG', BG)
- final = cv.add(FG,BG)
- cv.imshow('final',final)
-
- # 显示视频流
- cv.imshow('Color-Channel', frame)
- # ESC键退出
- keyCode = cv.waitKey(30) & 0xFF
- if keyCode == 27:
- break
-
- cam.release()
- cv.destroyAllWindows()
追踪2种颜色
常用颜色阈值
追踪效果
代码,可直接复制使用,根据上文常用颜色阈值调整要追踪的颜色
- # -*- coding: utf-8 -*-
- import cv2 as cv
- import numpy as np
-
- def nothing(x):
- pass
-
- cv.namedWindow('trackbars')
- cv.resizeWindow('trackbars', 400,500)
- cv.createTrackbar('huelower','trackbars',50,179,nothing)
- cv.createTrackbar('huehigher','trackbars',100,179,nothing)
- #追踪两种颜色
- cv.createTrackbar('hue2lower','trackbars',50,179,nothing)
- cv.createTrackbar('hue2higher','trackbars',100,179,nothing)
-
- cv.createTrackbar('satlow','trackbars',100,255,nothing)
- cv.createTrackbar('sathigh','trackbars',255,255,nothing)
- cv.createTrackbar('vallow','trackbars',100,255,nothing)
- cv.createTrackbar('valhigh','trackbars',255,255,nothing)
-
- cam = cv.VideoCapture(0)
- cam.set(3,400)
- #用numpy绘制一个空白画面
- blank = np.zeros([480,640,1],np.uint8)
-
- while True:
- # 读取视频流
- ret, frame = cam.read()
- #print(frame.shape)#查看图像大小
- #处理视频流
- hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
- hl = cv.getTrackbarPos('huelower','trackbars')
- hu = cv.getTrackbarPos('huehigher','trackbars')
- hl2 = cv.getTrackbarPos('hue2lower', 'trackbars')
- hu2 = cv.getTrackbarPos('hue2higher', 'trackbars')
- sl = cv.getTrackbarPos('satlow','trackbars')
- sh = cv.getTrackbarPos('sathigh','trackbars')
- vl = cv.getTrackbarPos('vallow','trackbars')
- vh = cv.getTrackbarPos('valhigh','trackbars')
- # 选取颜色范围
- lower = np.array([hl, sl,vl]) # 转换为一维数组
- upper = np.array([hu, sh, vh])
- lower2 = np.array([hl2, sl,vl]) # 转换为一维数组
- upper2 = np.array([hu2, sh, vh])
-
- FGmask = cv.inRange(hsv,lower,upper) # 设置HSV的阈值
- FGmask2 = cv.inRange(hsv,lower2,upper2) #设置HSV的阈值
- FGmaskComp=cv.add(FGmask, FGmask2)
- cv.imshow('FGmaskComp', FGmaskComp)
-
- #找到颜色外轮廓
- contours, hierarchy = cv.findContours(FGmaskComp, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
- contours = sorted(contours, key=lambda x: cv.contourArea(x), reverse=True)
- for cnt in contours:
- area = cv.contourArea(cnt) # 计算轮廓面积
- (x,y,w,h)= cv.boundingRect(cnt)
- if area >= 50:
- #cv.drawContours(frame, [cnt], -1, (255, 0, 0),3) # 画出图像轮廓
- cv.rectangle(frame,(x,y),(x+w,y+h),(255, 0, 0),3) #用矩形框画出轮廓
-
- # 显示视频流
- cv.imshow('nanocam', frame)
- # ESC键退出
- keyCode = cv.waitKey(30) & 0xFF
- if keyCode == 27:
- break
-
- cam.release()
- cv.destroyAllWindows()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。