【问题标题】:python OpenCV detect all pixels with pre defined valuepython OpenCV检测所有具有预定义值的像素
【发布时间】:2021-05-13 19:53:47
【问题描述】:

我正在做一个项目,需要用特定颜色识别屏幕中的所有像素,并生成带有输出的二进制数组。

我想知道最有效的方法,谢谢。

我曾尝试使用cv2.inRange,但失败了。这是代码:

import cv2
import numpy
from mss.windows import MSS as mss

sct = mss()

monitor_1 = sct.monitors[1]

color=numpy.array((0, 206, 224),dtype=numpy.uint8)

while True :
    screen = numpy.array(sct.grab(monitor_1))

    mask = cv2.inRange(screen,color, color) # modify your thresholds
    inv_mask = cv2.bitwise_not(mask)
    arrow = cv2.bitwise_and(screen, inv_mask)

    cv2.imshow('arrow', arrow)

【问题讨论】:

  • 我试过了,但一直出错,不知道如何修复它 (The lower boundary is neither an array of the same size and same type as src, nor a scalar in function 'cv::inRange')
  • 请发布您正在测试的代码和您的输入图像。
  • 这是我的图片i.imgur.com/GkWfZdA.png
  • 尝试将颜色阈值定义为数组:color = numpy.array([0, 206, 224])。你的图像也应该是一个8-bit uint 矩阵。

标签: python numpy opencv


【解决方案1】:

按照 cmets 中的建议,您可以使用 inRange,并且您可能希望对 BGR 值使用一些容差。

import cv2

im = cv2.imread("GkWfZdA.png")
yellow = im[130,130,:].astype(int)
mask = cv2.inRange(im, yellow-50, yellow+50)

(需要astype(int) 以确保0 - 10 == -10 而不是246inRange 自动将值限制在有效范围内)

【讨论】:

  • 我正在尝试使用不同颜色的解决方案,但不知道如何更改它检测到的值。你能用更多解释更新答案吗?
  • 颜色 (yellow) 取自我的回答 (im[130,130,:]) 中的中心像素,但您也可以直接将其指定为 BGR 值(例如 (0,0,255) 表示红色)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多