当我尝试将两个图像与蒙版混合时,我使用了np.where():
a,b 和 mask 是ndarray的列表,而dtype是float64。 tempImg 是具有相同dtype的ndarray。
```
a = [[179.52181224 196.11236657 199.25513578 200.81821174 198.7369237
188.63429662 192.30241342 207.88677914 222.15657045 229.51498031
231.98006343 231.87414046 230.46600773 218.49685369 162.22483198
100.36016141 121.1080895 135.74811817 130.99244378 110.68895168
118.30273168 126.86982489 129.77672984 124.48464581 113.252353 ]
[196.82448164 214.76750918 217.8666702 220.43924199 218.29167364
206.96139307 210.62221148 226.93273178 242.81860388 250.77400119
252.75942764 252.58055613 251.7060296 244.7889392 205.30175274
140.06253874 130.66796303 134.60075016 144.20452322 134.04340699
134.67761061 134.7536771 134.06737521 132.02019221 125.54434286]
[199.35308577 217.40897714 220.85336669 223.59548903 221.36787333
210.40169753 213.24258599 228.92592981 244.90159636 252.59280128
252.9813501 248.71485061 237.89496352 225.45499552 211.15977205
167.02392375 125.05120764 110.35189406 137.84965955 135.69712767
133.58192482 132.36280398 132.31858306 134.45862906 132.1907518 ]]
//b is quite similar to a, same size but different value.
//mask is also the same size but has float values between 0 and 1:
[[0, 0, 0...1, 1, 0.56, 0.94, 1]...]
```
for i in range (0, len(a)):
tempImg = tempImg[np.where(mask[i] == 1, a[i], b[i])]
tempImg = tempImg[np.where(mask[i] > 0 and mask[i] < 1,
a[i] * mask[i] + b[i] * (1 - mask[i]), tempImg)]
img.append(tempImg)
预期结果是一个新的ndarray列表(img),但出现以下错误:
in blend
tempImg = tempImg(np.where(mask[i] == 1, a[i], b[i]))
IndexError: arrays used as indices must be of integer (or boolean) type
有人可以帮助我解决问题吗?非常感谢!
答案 0 :(得分:0)
使用二维数组来代替通常使用的2D数组,而不是使用假定数组的列表来表示图像的行。如果您的图像已被定义为数组列表,则可以执行以下操作使其成为2D数组:
import numpy as np
list_of_arrays = [np.asarray([0.1, 0.2]), np.asarray([0.1, 0.2])]
my_image = np.asarray(list_of_arrays)
如果已经定义了掩码,则可以使用OpenCV的bitwise_and
:
看一下documentation,了解使用细节,但
this answer似乎对您很有用。
答案 1 :(得分:0)
感谢您更新问题。根据您的示例数据,这是我的设置:
a = np.array([[179.52181224, 196.11236657, 199.25513578, 200.81821174, 198.7369237,
188.63429662, 192.30241342, 207.88677914, 222.15657045, 229.51498031,
231.98006343, 231.87414046, 230.46600773, 218.49685369, 162.22483198,
100.36016141, 121.1080895 , 135.74811817, 130.99244378, 110.68895168,
118.30273168, 126.86982489, 129.77672984, 124.48464581, 113.252353 ],
[196.82448164, 214.76750918, 217.8666702 , 220.43924199, 218.29167364,
206.96139307, 210.62221148, 226.93273178, 242.81860388, 250.77400119,
252.75942764, 252.58055613, 251.7060296 , 244.7889392 , 205.30175274,
140.06253874, 130.66796303, 134.60075016, 144.20452322, 134.04340699,
134.67761061, 134.7536771 , 134.06737521, 132.02019221, 125.54434286],
[199.35308577, 217.40897714, 220.85336669, 223.59548903, 221.36787333,
210.40169753, 213.24258599, 228.92592981, 244.90159636, 252.59280128,
252.9813501 , 248.71485061, 237.89496352, 225.45499552, 211.15977205,
167.02392375, 125.05120764, 110.35189406, 137.84965955, 135.69712767,
133.58192482, 132.36280398, 132.31858306, 134.45862906, 132.1907518]])
b =a**2
mask=np.array([[0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56],
[0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56],
[0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 0, 1, 1, 0.56, 0.94, 1,0.7,1,0, 0, 1, 1, 0.56]])
tempImg=np.zeros_like(a)
您遇到的问题有两个。首先,您尝试使用来自其他数组(tempImg
)的浮点索引索引到tempImg=tempImg[]
。
解决方案:
for i in range (0, len(a)):
tempImg[i] = np.where(mask[i] == 1, a[i], b[i])
第二,也是最重要的,您根本不需要遍历整个循环!
利用numpy的矢量化解决方案:
tempImg = np.where(mask == 1, a, b)