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

我已经看到了几个使用遮罩的示例,但是我对如何使用遮罩比较RGB值感到困惑。
我的范围就是这样

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

首先,我将图像Img保存在YCrCb中。我如何创建一个遮罩,使其可以看到RGB是否在范围内,一旦完成,如何将它们设置为黑白?

最佳答案

我认为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)

关于python - 遍历所有像素(如果在范围内),将像素更改为黑色,否则更改为白色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38337872/

10-11 07:45
查看更多