【问题标题】:Python and PiCamera Color Detection Boolean IssuesPython 和 PiCamera 颜色检测布尔问题
【发布时间】:2020-03-19 04:03:37
【问题描述】:

我对 Python 还很陌生,并试图编写一种 PiCamera 的代码来获取不同的颜色并据此做出反应。我目前对每种颜色的尝试是拍照并检测所涉及的颜色。下面是一些示例代码。

def red_detect():
    # initialize the camera and grab a reference to the raw camera capture
    rawCapture = PiRGBArray(camera, size=(640, 480))

    # allow the camera to warmup
    time.sleep(0.1)

    # grab an image from the camera
    camera.capture(rawCapture, format="bgr")
    image = rawCapture.array

    # Convert BGR to HSV
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    lower_red = np.array([161, 155, 84],np.uint8)
    upper_red = np.array([179, 255, 255],np.uint8)
    red_mask = cv2.inRange(hsv, lower_red, upper_red)
    red = cv2.bitwise_and(image, image, mask=red_mask)

    return 255 in cv2.inRange(hsv, lower_red, upper_red)  

然后该代码将使用 if 语句进行评估,例如 if green == True:

但是,我所有的颜色都恢复为 True。我是不是快拍错了?

【问题讨论】:

    标签: python opencv colors detection picamera


    【解决方案1】:

    如果你只是想检测图像的颜色,有更简单的方法:

    from PIL import Image
    
    image = Image.open('image.jpg')
    
    w, h = image.size
    
    pixel = []
    
    for x in range(w):
        for y in range(h):
            r, g, b = image.getpixel((x, y))
            pixel.append([r, g, b])
    
    avr = [sum(x)//len(x) for x in zip(*pixel)]
    
    colors = dict(zip(['red','green','blue'],[i for i in avr]))
    print(max(colors))
    

    附:我在那里使用枕头模块。比我能帮上的忙,也是 python 新手

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多