简体   繁体   English

如何使用numpy数组切片遮罩图像灰度

[英]How to mask an image gray scale using numpy array slicing

I need to replace 8 bits values (0 to 255) indexed set of an image (final image), following a "map values" from another image (second image) gray scale which related map indexes was chosen from a primary image. 我需要替换另一个图像(第二个图像)灰度的“映射值”,然后替换一个图像(最终图像)的8位值(0到255)索引集,该灰度值是从主图像中选择了相关的映射索引。

In fact this is similar thing that MATLAB does with 实际上,这与MATLAB相似

 indexS =  find(image1 == integer ('could be a integer from 1 to 255')) 
 imagfinal(indexS) = imagsecondary(indexS).

I tried following examples for python/matlab find() on stack, for ex.: MATLAB-style find() function in Python . 我尝试了以下示例,例如在堆栈上使用python / matlab find(): Python中的MATLAB样式find()函数 And the related ones... 还有相关的...

I tried n.nonzero , np.argwhere and np.where, but I am really confuse. 我尝试了n.nonzero,np.argwhere和np.where,但是我真的很困惑。

I have three source images, let's say A, B, C, same shape, eg. 我有3个原始图片,例如A,B,C,形状相同。 (100x100) with diverse 0 to 255 values, I mean they are completely gray scales different each other. (100x100)具有0到255之间的不同值,我的意思是它们是完全互不相同的灰度。

So, 1st step - I need to get all indexes with values = 1 (but could be , 10, 196 , up to 255) from A, so I did: 因此,第一步-我需要从A获取所有值等于1(但可能是,10、196,最大255)的索引,所以我这样做了:

Aboolean = np.equal(A,1)

result is 结果是

       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],...

then I tried use those boolean index array results for getting the values from B: 然后我尝试使用这些布尔索引数组结果从B获取值:

Bnew = B[Aboolean]

But it did not work for furthers steps, because the result is a map of values and the indexes are lost... 但这并不能用于进一步的步骤,因为结果是值的映射并且索引丢失了。

The values from Bnew are supposed to substitute the respective values 8-bits on C image,I mean those 8-bits values into the same position (or same indexes), remembering that B and C (also A) have the same shape/size array (100x100). Bnew的值应该替换为C图像上的各个8位值,我的意思是将这些8位值放入相同的位置(或相同的索引),请记住B和C(也为A)具有相同的形状/大小阵列(100x100)。

So I tried again: 所以我再次尝试:


D = np.where(Aboolean,B,C)

when plotting the image, the final result is just the same image C !! 绘制图像时,最终结果就是相同的图像C! No modifications, at all. 完全没有修改。


fig, ax = plt.subplots(nrows=1, figsize=(16,20))
ax.imshow(D, cmap='gray',interpolation='nearest')

results same image 'C' 结果相同的图像“ C”

My goal is a kind of replacing a set of values from B upon C (ruled by same index positions) , that was sliced following a map of indexes of conditions upon A. 我的目标是从B到C替换一组值(由相同的索引位置决定),该值是根据A上的条件的索引图进行切片的。

You can do this by using the boolean indexing from A to directly copy the values from C into B (if you don't want to modify the original B, first create a copy using B.copy() ). 您可以通过使用A的布尔索引直接将C中的值复制到B中来进行此操作(如果您不想修改原始B,请首先使用B.copy()创建一个副本)。

>>> import numpy as np
>>> A = np.array([0,0,1,0,0])
>>> B = np.array([1,2,3,4,5])
>>> C = np.array([10,9,8,7,6])
>>> B[A==1] = C[A==1]

>>> B
array([1, 2, 8, 4, 5])

EDIT: 编辑:

C[A==1] = B[A==1] C [A == 1] = B [A == 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM