[英]How can I change a color channel in an image using numpy?
I have an image where some color channels in the pixels have a value of zero (ie 255, 146, 0). 我有一个图像,其中像素中的某些颜色通道的值为零(即255,146,0)。 I want to be able to change any value equal to zero in the array to a different value, but I do not know how to access these values. 我希望能够将数组中等于零的任何值更改为不同的值,但我不知道如何访问这些值。 Any help with this? 对此有何帮助?
This is the image array: 这是图像数组:
[[[ 76 163 168]
[109 166 168]
[173 172 167]
...,
[ 83 182 144]
[ 78 172 134]
[ 82 150 131]]
[[ 51 151 168]
[ 99 157 171]
[173 195 159]
...,
[ 56 165 144]
[ 25 198 125]
[ 35 185 121]]
[[ 76 163 121]
[112 147 120]
[175 151 118]
...,
[ 57 162 159]
[ 36 185 132]
[ 32 194 97]]
...,
[[ 78 189 126]
[ 68 173 129]
[ 58 171 150]
...,
[ 41 188 163]
[ 34 176 126]
[ 35 176 102]]
[[131 155 161]
[101 141 161]
[ 42 151 177]
...,
[ 56 178 122]
[ 45 192 114]
[ 46 184 112]]
[[130 157 185]
[ 83 141 185]
[ 42 158 185]
...,
[ 63 187 88]
[ 45 194 102]
[ 45 184 129]]]
img[(img==zero_val).all(-1)] = new_val
, where zero_val
is the zero
color and new_val
is the new color to be assigned at those places where we have zero
colored pixels. ,其中zero_val
是zero
颜色, new_val
是在我们zero
颜色像素的那些地方分配的新颜色。
Sample run - 样品运行 -
# Random image array
In [112]: img = np.random.randint(0,255,(4,5,3))
# Define sample zero valued and new valued arrays
In [113]: zero_val = [255,146,0]
...: new_val = [255,255,255]
...:
# Set two random points/pixels to be zero valued
In [114]: img[0,2] = zero_val
In [115]: img[2,3] = zero_val
# Use proposed approach
In [116]: img[(img==zero_val).all(-1)] = new_val
# Verify that new values have been assigned
In [117]: img[0,2]
Out[117]: array([255, 255, 255])
In [118]: img[2,3]
Out[118]: array([255, 255, 255])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.