Opencv 颜色过滤 Python mask 选取颜色 颜色直方图

布满荆棘的人生 2022-09-16 04:59 263阅读 0赞

截图目标,分析目标的颜色直方图:

  1. import cv2
  2. from matplotlib import pyplot as plt
  3. def show_img(path):
  4. img = cv2.imread(path)
  5. b, g, r = img[:, :, 0], img[:, :, 1], img[:, :, 2]
  6. hist_b = cv2.calcHist([b], [0], None, [256], [0, 256])
  7. hist_g = cv2.calcHist([g], [0], None, [256], [0, 256])
  8. hist_r = cv2.calcHist([r], [0], None, [256], [0, 256])
  9. plt.figure(figsize=(10, 5))
  10. plt.subplot(121)
  11. plt.plot(hist_r, color='r', label="r")
  12. plt.plot(hist_g, color='g', label="g")
  13. plt.plot(hist_b, color='b', label="b")
  14. plt.legend()
  15. img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  16. h, s, v = img2[:, :, 0], img2[:, :, 1], img2[:, :, 2]
  17. hist_h = cv2.calcHist([h], [0], None, [256], [0, 256])
  18. hist_s = cv2.calcHist([s], [0], None, [256], [0, 256])
  19. hist_v = cv2.calcHist([v], [0], None, [256], [0, 256])
  20. plt.subplot(122)
  21. plt.plot(hist_h, color='r', label="h")
  22. plt.plot(hist_s, color='g', label="s")
  23. plt.plot(hist_v, color='b', label="v")
  24. plt.legend()
  25. plt.show()
  26. return hist_r, hist_g, hist_b, hist_h, hist_s, hist_v
  27. r, g, b, h, s, v = show_img("lanse.png")

在这里插入图片描述

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread("1111.jpg")
  5. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  6. lower_red = np.array([115, 0, 160])
  7. upper_red = np.array([140, 30, 190])
  8. mask = cv2.inRange(hsv, lower_red, upper_red)
  9. res = cv2.bitwise_and(img, img, mask=mask)
  10. # cv2.imshow('img', img)
  11. cv2.imshow('mask', mask)
  12. cv2.imshow('res', res)
  13. cv2.waitKey(0)
  14. cv2.destroyAllWindows()

发表评论

表情:
评论列表 (有 0 条评论,263人围观)

还没有评论,来说两句吧...

相关阅读

    相关 颜色直方图图像检索

    颜色直方图图像检索是一种基于图像颜色分布的图像检索方法。它的基本思想是通过计算图像的颜色直方图,然后使用这些直方图来比较和匹配图像。这种方法通常用于在大规模图像数据库中查找相似

    相关 颜色直方图匹配(一)

    最近在研究颜色直方图匹配算法,提取一张图的色彩特征有很多种方法,最常用的一种特征是提取它的颜色直方图分布特征。网上搜了很多文章,颜色直方图匹配算法主要有两类,一类基于RGB色彩

    相关 OpenCV颜色识别

    彩色模型 数字图像处理中常用的采用模型是RGB(红,绿,蓝)模型和HSV(色调,饱和度,亮度),RGB广泛应用于彩色监视器和彩色视频摄像机,我们平时的图片一般都是RGB模