当前位置:   article > 正文

Python opencv 根据HSV实现简单的颜色分类_cv2色彩分类

cv2色彩分类
  1. # hsv_color_picker
  2. """
  3. import cv2
  4. import numpy as np
  5. def nothing(x):
  6. pass
  7. cv2.namedWindow("frame")
  8. cv2.createTrackbar("H","frame",0,179,nothing)
  9. cv2.createTrackbar("S","frame",255,255,nothing)
  10. cv2.createTrackbar("V","frame",255,255,nothing)
  11. img_hsv=np.zeros((250,500,3),np.uint8)
  12. while True:
  13. h=cv2.getTrackbarPos("H","frame")
  14. s=cv2.getTrackbarPos("S","frame")
  15. v=cv2.getTrackbarPos("V","frame")
  16. img_hsv[:]=(h,s,v)
  17. img_bgr=cv2.cvtColor(img_hsv,cv2.COLOR_HSV2BGR)
  18. cv2.imshow("frame",img_bgr)
  19. key=cv2.waitKey(1)
  20. if key==27:
  21. break
  22. cv2.destroyWindow()
  23. """
  24. import cv2
  25. cap=cv2.VideoCapture(1)
  26. cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
  27. cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
  28. while True:
  29. _,frame=cap.read()
  30. hsv_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
  31. height,width,_=frame.shape
  32. cy,cx=height//2,width//2
  33. # 获取画面中心点像素值
  34. pixel_center=hsv_frame[cy,cx]
  35. hue_value=pixel_center[0]
  36. color="Undefined"
  37. if 0<hue_value<11 or 156<hue_value<180:
  38. color="RED"
  39. elif 11<hue_value<25:
  40. color="ORANGE"
  41. elif 26<hue_value<34:
  42. color="YELLOW"
  43. elif 35<hue_value<77:
  44. color="GREEN"
  45. elif 78<hue_value<99:
  46. color="Qing"
  47. elif 100<hue_value<124:
  48. color="BLUE"
  49. elif 125<hue_value<155:
  50. color="purple"
  51. # elif hue_value<170:
  52. # color="PINK"
  53. else:
  54. pass
  55. # color="RED"
  56. pixel_center_bgr=frame[cx,cy]
  57. b,g,r=[int(i) for i in pixel_center_bgr]
  58. cv2.putText(frame,color,(10,70),0,1,(255,255,255),2)
  59. cv2.circle(frame,(cx,cy),8,(255,255,255),3)
  60. cv2.imshow("Frame",frame)
  61. key=cv2.waitKey(1)
  62. if key==27:
  63. break
  64. cap.release()
  65. cv2.destroyWindow()

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

闽ICP备14008679号