[英]Error while trying to display only certain colour pixels of an image that is captured
我正在嘗試將捕獲圖像的所有像素(不是黃色)轉換為白色。
我收到的錯誤消息是:
if jmg[i,j]!=[255,255,0]:
valueError: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
下面是我的代碼:
import cv2
import picamera
import numpy
import time
from matplotlib import pyplot as plt
print("ready")
with picamera.PiCamera() as camera:
camera.resolution=(400,400)
time.sleep(1)
camera.capture("/home/pi/rowdy.jpg")
print("Done")
yellow=[255,255,0]
img=cv2.imread("/home/pi/rowdy.jpg",1)
jmg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
for i in numpy.arange(399):
for j in numpy.arange(399):
if jmg[i,j]!=[255,255,0]:
jmg[i,j]=[255,255,255]
plt.imshow(jmg)
plt.xticks([]),plt.yticks([])
plt.show()
jmg
是一個由三個值組成的numpy數組。 當您將numpy數組與另一個數組(或列表)進行比較時,它將按element-wise
進行比較。
if jmg[i,j]!=[255,255,0]: #np.array(255, 0, 0) != [255,255,0]
# -> np.array(False, True, False)
這意味着您需要執行錯誤消息中所說的操作,並使用any()
或all()
來確定所需的值。 在你的情況,你要檢查,如果any
這些值不匹配,這意味着你的元素智能邏輯將使這三個值中的一個True
if (jmg[i,j]!=[255,255,0]).any():
jmg[i,j]=[255,255,255]
一種更快的方法是使用numpy
庫。 首先創建法師的副本,然后使用numpy.argwhere
在所需位置替換像素值:
image1 = image.copy()
image1[np.argwhere(image != [255,255,0])] = [255,255,255]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.