[英]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.