我打開圖像並將其轉換爲RGBA值的數組。這裏是我的代碼:Python - 如果語句涉及numpy數組
img = Image.open('17112013.png')
imgarray = array(img)
l = imgarray[:,1,1]
m = imgarray[1,:,1]
n = imgarray[1,1,:]
blueCount = 0
redCount = 0
flag = 0
for i in range(0,len(l)):
for j in range(0,len(m)):
if (imgarray[i,j] == [255,255,255,255]):
pass
if (imgarray[i,j] == [0,0,0,255] and flag == 0):
pass
if (imgarray[i,j] == [255,0,0,255] and flag == 0):
pass
if (imgarray[i,j] == [0,0,255,255]):
flag = 1
blueCount = blueCount + 1
if (imgarray[i,j] == [255,0,0,255] and flag == 1):
redCount = redCount + 1
if (imgarray[i,j] == [0,0,0,255] and flag == 1):
flag = 0
if (imgarray[i,j] == [255,255,255,255] and flag == 1):
flag = 0
如果我運行它是這樣的,我收到以下內容:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我明白,我需要使用這些爲「如果」包含numpy的陣列報表;然而,我不確定實現這個的正確語法。可能只是愚蠢的,但任何幫助將不勝感激。
針對''[numpy] ambiguous'的搜索SO對這個錯誤消息提出了許多問題。 – hpaulj