【问题标题】:Detect if an object has a specific colour out of 3 colours using opencv (cv2 in python3)使用opencv(python3中的cv2)检测对象是否具有3种颜色中的特定颜色
【发布时间】:2018-07-13 16:46:49
【问题描述】:

我目前正在开发一个程序,该程序可以检测我的房间里是否有红色物体或是否有蓝色物体。我周围的其他地方不是白色就是黑色。我已尽量减少房间内光线的变化。

在给定特定色调范围的情况下,我已经成功地在对象周围创建了一个蒙版。我希望我的程序为我打印:

1) “红色”- 如果有红色物体

2) “蓝色”- 如果有蓝色物体

我不知道该怎么做。以下是我在蓝色对象周围创建蒙版的程序。我也给出了其他一些颜色的色调范围。这样你就可以试试了。

程序:

import cv2
import numpy as np

cam = cv2.VideoCapture(1)

while True:
    _, frame = cam.read()

    denoised = cv2.GaussianBlur(frame, (31, 31), 35)
    hsv = cv2.cvtColor(denoised, cv2.COLOR_BGR2HSV)


    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([160, 255, 255])

    mask = cv2.inRange(hsv, lower_red, upper_red)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('frame', frame)
    #cv2.imshow('mask', mask)
    cv2.imshow('res', res)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cam.release()
cv2.destroyAllWindows()

不同颜色的色调(我不确定红色的色调,因为它不适用于某些颜色 - 我尝试了 Stackoverflow 的一些解决方案):

lower_red = np.array([0, 100, 100])
upper_red = np.array([0, 255, 255])

lower_yellow = np.array([15, 210, 20])
upper_yellow = np.array([35, 255, 255])

lower_green = np.array([29, 86, 6])
upper_green = np.array([64, 255, 2555])

lower_orange = np.array([10, 100, 20])
upper_orange = np.array([20,255,255])

以下是一些您可以尝试的示例图片:

【问题讨论】:

    标签: python-3.x opencv cv2 color-detection


    【解决方案1】:

    你的方法在某种程度上是正确的。但是要确定图像特定区域的颜色,您需要计算已知颜色数据集与该区域的 L*a*b 平均值之间的欧几里得距离。

    1. 检测您需要颜色的特定感兴趣区域。
    2. 请参考以下代码来确定感兴趣区域内的颜色。

      class ColorLabeler:
          def __init__(self):
              # initialize the colors dictionary, containing the color
              # name as the key and the RGB tuple as the value
              colors = OrderedDict({
                  "red": (255, 0, 0),
                  "green": (0, 255, 0),
                  "blue": (0, 0, 255)})
      
              # allocate memory for the L*a*b* image, then initialize
              # the color names list
              self.lab = np.zeros((len(colors), 1, 3), dtype="uint8")
              self.colorNames = []
      
              # loop over the colors dictionary
              for (i, (name, rgb)) in enumerate(colors.items()):
                  # update the L*a*b* array and the color names list
                  self.lab[i] = rgb
                  self.colorNames.append(name)
      
              # convert the L*a*b* array from the RGB color space
              # to L*a*b*
              self.lab = cv2.cvtColor(self.lab, cv2.COLOR_RGB2LAB)
      
      def label(self, image, c):
          # construct a mask for the contour, then compute the
          # average L*a*b* value for the masked region
          mask = np.zeros(image.shape[:2], dtype="uint8")
          cv2.drawContours(mask, [c], -1, 255, -1)
          mask = cv2.erode(mask, None, iterations=2)
          mean = cv2.mean(image, mask=mask)[:3]
      
          # initialize the minimum distance found thus far
          minDist = (np.inf, None)
      
          # loop over the known L*a*b* color values
          for (i, row) in enumerate(self.lab):
              # compute the distance between the current L*a*b*
              # color value and the mean of the image
              d = dist.euclidean(row[0], mean)
      
              # if the distance is smaller than the current distance,
              # then update the bookkeeping variable
              if d < minDist[0]:
                  minDist = (d, i)
      
          # return the name of the color with the smallest distance
          return self.colorNames[minDist[1]]
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 2016-09-06
      • 2014-11-14
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      相关资源
      最近更新 更多