【问题标题】:Iterating through all pixels, if within range, change pixel to black, else to white遍历所有像素,如果在范围内,将像素更改为黑色,否则为白色
【发布时间】:2016-07-13 05:12:33
【问题描述】:

所以基本上,我想遍历我的所有像素,如果它们在范围内,则将它们的 RGB 值更改为白色,否则更改为黑色。

我见过几个使用蒙版的例子,但我对如何使用蒙版来比较 RGB 值感到有点困惑。 我的范围是这样的

min_YCrCb = np.array([0,133,77],np.uint8) 
max_YCrCb = np.array([255,173,127],np.uint8)

所以首先我在 YCrCb 中有我的图像 img。我如何创建一个蒙版以查看 RGB 是否在范围内,一旦完成,我如何将它们设置为黑白?

【问题讨论】:

  • 黑色是 0,0,0。白色是 255,255,255。您无需检查 R,因为它的范围就是全部。用你想要的两个极端检查 G 和 B
  • 但是我如何检查 G 和 B?我一般不知道如何访问像素的 RGB
  • 不确定 'mask' 但 132
  • 很遗憾,我需要使用口罩。使用 for 循环比较像素的 rgb 值需要太长时间

标签: python opencv pixel mask


【解决方案1】:

我认为inRange 方法是您所需要的。

因此,在您的示例中,您可以使用:

# Keep in mind that OpenCV stores things in BGR order, not RGB
lowerBound = cv.Scalar(0, 133, 770)
upperBound = cv.Scalar(255, 173, 127)

# this gives you the mask for those in the ranges you specified
cv.InRange(cv_input, lowerBound, upperBound, cv_output);

对于你的 cv_input 中的每个像素,如果它的值在给定的范围内,它将被设置为 255(全为 1),否则为 0。如果你想要反转,你可以使用Not 方法。

# This will set all bits in cv_input 
cv.Not(cv_output, cv_inverse)

【讨论】:

    猜你喜欢
    • 2023-01-13
    • 2021-01-27
    • 2015-02-04
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2019-12-13
    • 2015-05-05
    • 1970-01-01
    相关资源
    最近更新 更多