[英]How to change pixel value based on a condition
图像为 1920 x 1080。当通道值高于另一个时,如何更改像素的值?
这就是我所做的。
y_range = 1080
for y in range(y_range):
x = 0
while x < 1920:
green_value = img[y, x][1]
red_value = img[y, x][2]
diff = int(red_value) - int(green_value)
if diff < 5:
img[y, x] = [0, 0, 0]
x = x + 1
有没有比迭代每个像素更有效的方法?
不要为此使用任何循环,使用ndarray
功能和逻辑索引。
您想要实现的目标是:
d = img[:,:,2] - img[:,:,1] # Difference of color channels
q = d < 5 # Threshold criterion
img[q] = [0,0,0] # Overwrite data based on threshold
如果您的图像是 BGR 格式,并且您想要红色和绿色通道之间的签名差异。 如果您的意思是距离变化:
d = np.abs(img[:,:,2] - img[:,:,1]) # Distance between color channels
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.