如何遍历图像的非零值? - Python
How to iterate through non-zeros values of an image ? - Python
我喜欢在线提取和显示图像主色的功能。为了节省时间,我只想迭代非零像素而不是整个图像。但是,我更改函数的方式会引发错误:
if row != [0,0,0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
修改后的代码如下:
def dominantColor(image) :
from matplotlib.pyplot import imshow, show
from scipy.cluster.vq import whiten
from scipy.cluster.vq import kmeans
import pandas as pd
r = []
g = []
b = []
for row in image :
if row != [0,0,0]: #the part I added to the original code
print(row)
for temp_r, temp_g, temp_b in row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)
image_df = pd.DataFrame({'red': r, 'green': g, 'blue': b})
image_df['scaled_color_red'] = whiten(image_df['red'])
image_df['scaled_color_blue'] = whiten(image_df['blue'])
image_df['scaled_color_green'] = whiten(image_df['green'])
cluster_centers, _ = kmeans(image_df[['scaled_color_red','scaled_color_blue','scaled_color_green']], 3)
dominant_colors = []
red_std, green_std, blue_std = image_df[['red','green','blue']].std()
for cluster_center in cluster_centers:
red_scaled, green_scaled, blue_scaled = cluster_center
dominant_colors.append((
red_scaled * red_std / 255,
green_scaled * green_std / 255,
blue_scaled * blue_std / 255
))
imshow([dominant_colors])
show()
return dominant_colors
我应该如何更正我的迭代循环以消除错误并使我的图像只有非零值? (注意:图像实际上是 mask * original_image
)
如果你想共同比较数组元素,你需要在比较之后添加 .all()
方法。所以 if (row == [0,0,0]).all()
.
import numpy as np
image = np.array([
[0, 0, 0],
[1, 0, 0],
[0, 0, 1],
])
for row in image:
if not (row == [0, 0, 0]).all():
print(row)
结果:
[1 0 0]
[0 0 1]
如果我正确理解你的代码,答案就在你发布的错误日志中:
if row != [0,0,0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
因此,any
函数检查行中的任何元素是否被评估为真:
for row in image :
if any(row): #enter the if block if any element is not 0
print(row)
for temp_r, temp_g, temp_b in row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)
我喜欢在线提取和显示图像主色的功能。为了节省时间,我只想迭代非零像素而不是整个图像。但是,我更改函数的方式会引发错误:
if row != [0,0,0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
修改后的代码如下:
def dominantColor(image) :
from matplotlib.pyplot import imshow, show
from scipy.cluster.vq import whiten
from scipy.cluster.vq import kmeans
import pandas as pd
r = []
g = []
b = []
for row in image :
if row != [0,0,0]: #the part I added to the original code
print(row)
for temp_r, temp_g, temp_b in row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)
image_df = pd.DataFrame({'red': r, 'green': g, 'blue': b})
image_df['scaled_color_red'] = whiten(image_df['red'])
image_df['scaled_color_blue'] = whiten(image_df['blue'])
image_df['scaled_color_green'] = whiten(image_df['green'])
cluster_centers, _ = kmeans(image_df[['scaled_color_red','scaled_color_blue','scaled_color_green']], 3)
dominant_colors = []
red_std, green_std, blue_std = image_df[['red','green','blue']].std()
for cluster_center in cluster_centers:
red_scaled, green_scaled, blue_scaled = cluster_center
dominant_colors.append((
red_scaled * red_std / 255,
green_scaled * green_std / 255,
blue_scaled * blue_std / 255
))
imshow([dominant_colors])
show()
return dominant_colors
我应该如何更正我的迭代循环以消除错误并使我的图像只有非零值? (注意:图像实际上是 mask * original_image
)
如果你想共同比较数组元素,你需要在比较之后添加 .all()
方法。所以 if (row == [0,0,0]).all()
.
import numpy as np
image = np.array([
[0, 0, 0],
[1, 0, 0],
[0, 0, 1],
])
for row in image:
if not (row == [0, 0, 0]).all():
print(row)
结果:
[1 0 0]
[0 0 1]
如果我正确理解你的代码,答案就在你发布的错误日志中:
if row != [0,0,0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
因此,any
函数检查行中的任何元素是否被评估为真:
for row in image :
if any(row): #enter the if block if any element is not 0
print(row)
for temp_r, temp_g, temp_b in row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)