我有兩個 RGBA 顏色型別的影像,我想根據透明通道組合它們我使用獨占 for 回圈撰寫代碼,但我想使用 NumPy 以實作速度
crop = img[y:y height,x:x width,:].copy()
for i in range(0,height):
for j in range(0,width):
if(crop[i,j,3] < resized_box[i,j,3]):
crop[i,j,:] = resized_box[i,j,:]
img[y:y height,x:x width,:] = crop
uj5u.com熱心網友回復:
你可以這樣做:
# NOTE: I'm assuming x, y, width and height are defined somewhere and width, height match resized_box's shape
where_to_overwrite = img[y:y height,x:x width,3] < resized_box[:,:,3]
img[y:y height,x:x width,:][where_to_overwrite] = resized_box[where_to_overwrite]
基本上,首先您確定需要覆寫的像素的索引(根據您的代碼,這似乎是作物的 alpha 通道低于resized_box's 的那些),然后您實際設定值。
我相信第二個中的雙索引是必要的,因為首先您查看作物,然后使用計算的布爾索引來識別那里的特定像素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424704.html
