当前位置:   article > 正文

opencv颜色处理与追踪_opencv.js 调色

opencv.js 调色

一、opencv RGB颜色处理

图像是颜色的矩阵

1、拉流

  1. import cv2 as cv
  2. import numpy as np
  3. cam = cv.VideoCapture(0)
  4. cam.set(3,300)
  5. #视频帧率
  6. cam.set(5, 30)
  7. cam.set(cv.CAP_PROP_BRIGHTNESS, 50) #设置亮度 -64 - 64 0.0
  8. cam.set(cv.CAP_PROP_CONTRAST, 0) #设置对比度 -64 - 64 2.0
  9. cam.set(cv.CAP_PROP_EXPOSURE, 1000) #设置曝光值 1.0 - 5000 156.0

2、读取与处理视频流

  1. while True:
  2. # 读取视频流
  3. ret, frame = cam.read()
  4. #print(frame.shape)#查看图像大小
  5. #处理视频流
  6. b,g,r = cv.split(frame)
  7. #创建不同颜色的通道
  8. blue = cv.merge((b,blank,blank))
  9. green = cv.merge((blank,g,blank))
  10. red = cv.merge((blank,blank,r))
  11. # 显示视频流
  12. cv.imshow('Color-Channel', frame)
  13. cv.imshow('blue', blue)
  14. cv.imshow('green', green)
  15. cv.imshow('red', red)

3、关闭视频流

  1. # ESC键退出
  2. keyCode = cv.waitKey(30) & 0xFF
  3. if keyCode == 27:
  4. break
  5. cam.release()
  6. cv.destroyAllWindows()

4、使用numpy创建空白图像

  1. #用numpy绘制一个空白画面
  2. blank = np.zeros([240,320,1],np.uint8)
  3. cv.imshow('blank',blank)

5、显示效果

所有代码

  1. # -*- coding: utf-8 -*-
  2. import cv2 as cv
  3. import numpy as np
  4. cam = cv.VideoCapture(0)
  5. cam.set(3,300)
  6. #视频帧率
  7. cam.set(5, 30)
  8. cam.set(cv.CAP_PROP_BRIGHTNESS, 50) #设置亮度 -64 - 64 0.0
  9. cam.set(cv.CAP_PROP_CONTRAST, 0) #设置对比度 -64 - 64 2.0
  10. cam.set(cv.CAP_PROP_EXPOSURE, 1000) #设置曝光值 1.0 - 5000 156.0
  11. #用numpy绘制一个空白画面
  12. blank = np.zeros([240,320,1],np.uint8)
  13. while True:
  14. # 读取视频流
  15. ret, frame = cam.read()
  16. #print(frame.shape)#查看图像大小
  17. #处理视频流
  18. b,g,r = cv.split(frame)
  19. #创建不同颜色的通道
  20. blue = cv.merge((b,blank,blank))
  21. green = cv.merge((blank,g,blank))
  22. red = cv.merge((blank,blank,r))
  23. merge=cv.merge((b,g,r))
  24. # 显示视频流
  25. cv.imshow('Color-Channel', frame)
  26. cv.imshow('blue', blue)
  27. cv.imshow('green', green)
  28. cv.imshow('red', red)
  29. cv.imshow('blank',blank)
  30. cv.imshow('merge',merge)
  31. # ESC键退出
  32. keyCode = cv.waitKey(30) & 0xFF
  33. if keyCode == 27:
  34. break
  35. cam.release()
  36. cv.destroyAllWindows()

二、RGB转成HSV筛选出要追踪的颜色

 HSV颜色模型

 

代码

  1. # -*- coding: utf-8 -*-
  2. import cv2 as cv
  3. import numpy as np
  4. def nothing(x):
  5. pass
  6. cv.namedWindow('trackbars')
  7. cv.resizeWindow('trackbars', 400,500)
  8. cv.createTrackbar('huelower','trackbars',50,179,nothing)
  9. cv.createTrackbar('huehigher','trackbars',100,179,nothing)
  10. #追踪两种颜色
  11. cv.createTrackbar('hue2lower','trackbars',50,179,nothing)
  12. cv.createTrackbar('hue2higher','trackbars',100,179,nothing)
  13. cv.createTrackbar('satlow','trackbars',100,255,nothing)
  14. cv.createTrackbar('sathigh','trackbars',255,255,nothing)
  15. cv.createTrackbar('vallow','trackbars',100,255,nothing)
  16. cv.createTrackbar('valhigh','trackbars',255,255,nothing)
  17. cam = cv.VideoCapture(0)
  18. cam.set(3,300)
  19. #用numpy绘制一个空白画面
  20. blank = np.zeros([240,320,1],np.uint8)
  21. while True:
  22. # 读取视频流
  23. ret, frame = cam.read()
  24. #print(frame.shape)#查看图像大小
  25. #处理视频流
  26. hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
  27. hl = cv.getTrackbarPos('huelower','trackbars')
  28. hu = cv.getTrackbarPos('huehigher','trackbars')
  29. hl2 = cv.getTrackbarPos('hue2lower', 'trackbars')
  30. hu2 = cv.getTrackbarPos('hue2higher', 'trackbars')
  31. sl = cv.getTrackbarPos('satlow','trackbars')
  32. sh = cv.getTrackbarPos('sathigh','trackbars')
  33. vl = cv.getTrackbarPos('vallow','trackbars')
  34. vh = cv.getTrackbarPos('valhigh','trackbars')
  35. # 选取颜色范围
  36. lower = np.array([hl, sl,vl]) # 转换为一维数组
  37. upper = np.array([hu, sh, vh])
  38. lower2 = np.array([hl2, sl,vl]) # 转换为一维数组
  39. upper2 = np.array([hu2, sh, vh])
  40. FGmask = cv.inRange(hsv,lower,upper) # 设置HSV的阈值
  41. FGmask2 = cv.inRange(hsv,lower2,upper2) # 设置HSV的阈值
  42. FGmaskComp=cv.add(FGmask, FGmask2)
  43. cv.imshow('FGmaskComp', FGmask)
  44. FG = cv.bitwise_and(frame,frame, mask=FGmaskComp) # 将掩膜和图像逐像素相加
  45. cv.imshow('FG',FG)
  46. BGmask = cv.bitwise_not(FGmaskComp)
  47. cv.imshow('BGmask',BGmask)
  48. BG = cv.cvtColor(BGmask,cv.COLOR_GRAY2BGR)
  49. cv.imshow('BG', BG)
  50. final = cv.add(FG,BG)
  51. cv.imshow('final',final)
  52. # 显示视频流
  53. cv.imshow('Color-Channel', frame)
  54. # ESC键退出
  55. keyCode = cv.waitKey(30) & 0xFF
  56. if keyCode == 27:
  57. break
  58. cam.release()
  59. cv.destroyAllWindows()

 追踪2种颜色

常用颜色阈值

 三、根据物体颜色追踪物体

追踪效果

代码,可直接复制使用,根据上文常用颜色阈值调整要追踪的颜色

  1. # -*- coding: utf-8 -*-
  2. import cv2 as cv
  3. import numpy as np
  4. def nothing(x):
  5. pass
  6. cv.namedWindow('trackbars')
  7. cv.resizeWindow('trackbars', 400,500)
  8. cv.createTrackbar('huelower','trackbars',50,179,nothing)
  9. cv.createTrackbar('huehigher','trackbars',100,179,nothing)
  10. #追踪两种颜色
  11. cv.createTrackbar('hue2lower','trackbars',50,179,nothing)
  12. cv.createTrackbar('hue2higher','trackbars',100,179,nothing)
  13. cv.createTrackbar('satlow','trackbars',100,255,nothing)
  14. cv.createTrackbar('sathigh','trackbars',255,255,nothing)
  15. cv.createTrackbar('vallow','trackbars',100,255,nothing)
  16. cv.createTrackbar('valhigh','trackbars',255,255,nothing)
  17. cam = cv.VideoCapture(0)
  18. cam.set(3,400)
  19. #用numpy绘制一个空白画面
  20. blank = np.zeros([480,640,1],np.uint8)
  21. while True:
  22. # 读取视频流
  23. ret, frame = cam.read()
  24. #print(frame.shape)#查看图像大小
  25. #处理视频流
  26. hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
  27. hl = cv.getTrackbarPos('huelower','trackbars')
  28. hu = cv.getTrackbarPos('huehigher','trackbars')
  29. hl2 = cv.getTrackbarPos('hue2lower', 'trackbars')
  30. hu2 = cv.getTrackbarPos('hue2higher', 'trackbars')
  31. sl = cv.getTrackbarPos('satlow','trackbars')
  32. sh = cv.getTrackbarPos('sathigh','trackbars')
  33. vl = cv.getTrackbarPos('vallow','trackbars')
  34. vh = cv.getTrackbarPos('valhigh','trackbars')
  35. # 选取颜色范围
  36. lower = np.array([hl, sl,vl]) # 转换为一维数组
  37. upper = np.array([hu, sh, vh])
  38. lower2 = np.array([hl2, sl,vl]) # 转换为一维数组
  39. upper2 = np.array([hu2, sh, vh])
  40. FGmask = cv.inRange(hsv,lower,upper) # 设置HSV的阈值
  41. FGmask2 = cv.inRange(hsv,lower2,upper2) #设置HSV的阈值
  42. FGmaskComp=cv.add(FGmask, FGmask2)
  43. cv.imshow('FGmaskComp', FGmaskComp)
  44. #找到颜色外轮廓
  45. contours, hierarchy = cv.findContours(FGmaskComp, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
  46. contours = sorted(contours, key=lambda x: cv.contourArea(x), reverse=True)
  47. for cnt in contours:
  48. area = cv.contourArea(cnt) # 计算轮廓面积
  49. (x,y,w,h)= cv.boundingRect(cnt)
  50. if area >= 50:
  51. #cv.drawContours(frame, [cnt], -1, (255, 0, 0),3) # 画出图像轮廓
  52. cv.rectangle(frame,(x,y),(x+w,y+h),(255, 0, 0),3) #用矩形框画出轮廓
  53. # 显示视频流
  54. cv.imshow('nanocam', frame)
  55. # ESC键退出
  56. keyCode = cv.waitKey(30) & 0xFF
  57. if keyCode == 27:
  58. break
  59. cam.release()
  60. cv.destroyAllWindows()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/566888
推荐阅读
相关标签
  

闽ICP备14008679号