简体   繁体   English

如何在python中使用numpy提取图像(3bands)中的线条?

[英]how to extract line in images(3bands) using numpy in python?

In order to modify the value of a particular vertical line in the image, i want to extract the 3-band vertical line of the image.为了修改图像中特定垂直线的值,我想提取图像的 3 波段垂直线。

When I was a two dimensional array, I extracted the vertical lines with the following code:当我是二维数组的时候,我用下面的代码提取了垂直线:

vertical_line = array[:,[1]]

I tried to use this code in a 3d array image but it failed.我尝试在 3d 数组图像中使用此代码,但失败了。

#image shape(b,g,r) (100,100,3)
# my try
vertical_line_try_1 = image[:,[1]][3]#result => [[255,255,255]]
vertical_line_try_2 = image[:,[1][3]]#result => IndexError: list index out of range
vertical_line_try_3 = image[:,[1],[3]]#result => IndexError: index 3 is out of bounds for axis 2 with size 3

How can I extract the vertical lines of three bands at once without writing a loop?如何在不编写循环的情况下一次提取三个波段的垂直线?

When you index with a slice the corresponding dimension is kept (since you get a possible stridded range of values).当您使用切片进行索引时,会保留相应的维度(因为您会获得可能的跨值范围)。 On the other hand, when you index with a single number that dimension disappears.另一方面,当您使用单个数字进行索引时,该维度就会消失。

Ex:前任:

a = np.random.randn(4,5)

# Let's get the third column in the matrix
print(a[:, 2].shape)   # prints `(4,)` -> we get a vector
print(a[:, 2:3].shape) # prints `(4,1)` -> we keep the result as a column matrix

From your two dimensional example it seems you are using advanced indexing just as a way to keep the dimension.从您的二维示例来看,您似乎正在使用高级索引作为保持维度的一种方式。 If that is true, using a slice containing a single element as I have shown is cleaner (my opinion) and faster (tested with %timeit in IPython).如果这是真的,那么使用我所展示的包含单个元素的切片会更干净(我认为)和更快(在 IPython 中使用%timeit进行测试)。

Now, back to your question.现在,回到你的问题。 It looks like you want to extract all values whose index of the second dimension is equal to 1, similar to the red part in the figure below.看起来你想提取所有第二维索引等于1的值,类似于下图中的红色部分。

索引第二个维度

If that is the case, then just use either image[:,1, :] (or just image[:,1] ) if you to get the values as a matrix, or image[:,1:2, :] if you want a 3D array.如果是这种情况,则只需使用image[:,1, :] (或仅使用image[:,1] )如果您将值作为矩阵获取,或者使用image[:,1:2, :]如果你想要一个 3D 阵列。


It is also useful to understand what your failed attempts were actually doing.了解失败的尝试实际上在做什么也很有用。

  1. image[:,[1]][3] : This is actually two indexing operations in the same line. image[:,[1]][3] :这实际上是同一行中的两个索引操作。 First, image[:,[1]] would return a 100x1x3 array, and then the second indexing with [3] would take the fourth line.首先, image[:,[1]]将返回一个100x1x3数组,然后使用[3]的第二个索引将占用第四行。 Since this second indexing is a regular one (not a fancy and not a slice), then the indexed dimension disappears and you get a 1x3 array in the end由于第二个索引是常规索引(不是花哨的,也不是切片),因此索引维度消失,最后得到一个1x3数组
  2. image[:,[1][3]] : This one I'm not really sure what it is doing image[:,[1][3]] :这个我不太确定它在做什么
  3. image[:,[1],[3]] : This means "take all values from the first dimension, but only the ones from the second index of the first dimension and the fourth index of the third dimension. Since the third dimension only has "size" 3, then trying to get the fourth index results in the out of bound error. image[:,[1],[3]] :这意味着“从第一个维度中获取所有值,但仅从第一个维度的第二个索引和第三个维度的第四个索引中获取。由于只有第三个维度具有“大小”3,然后尝试获取第四个索引会导致越界错误。

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

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