英文:
Looping over multiple ranges in a list
问题
目前我正在尝试为二进制分类手动标记一些图像。我已将一幅图像分成了多个补丁,并将它们与从0到255的数字相对应(每幅图像有256个补丁)。现在,在某些补丁中,发生了一些有趣的事情,我想将它们标记为'1'。我想要标记为1的补丁范围示例是:121:124和137:140,153:156。
是否有一种方式可以在列表中循环这些范围,以便我不必手动输入像在变量image1中一样的内容?[121, 122, 123, 124, 137, 138, 139, 140, 153, 154, 155, 156]?我已经有的代码如下。
谢谢你提前帮助!
def label_patch(image, patch_ranges):
num_labels = np.arange((255))
labels = []
for patch, label in zip(image, num_labels):
if label not in patch_ranges:
labels.append(0)
else:
labels.append(1)
return labels
image1 = label_patch(patched_imgs[1], [121, 122, 123, 124, 137, 138, 139, 140, 153, 154, 155, 156])
英文:
Currently I am trying to manually label some images for binary classification. I have divided up an image into patches, and connected them to a number from 0 to 255 (I have 256 patches per image). Now in certain patches, something interesting is happening and I would like to label those '1'. An example of patch ranges that I would like to label to 1 are; 121:124 and 137:140, 153:156.
Is there a way to loop over these ranges within a list so that I do not have to manually type such as in var image1? [121, 122, 123, 124, 137, 138, 139, 140, 153, 154, 155,156] ? The code I already have is below.
Thank you in advance!
def label_patch(image, patch_ranges):
num_labels = np.arange((255))
labels = []
for patch, label in zip(image, num_labels):
if label not in patch_ranges:
labels.append(0)
else:
labels.append(1)
return labels
image1 = label_patch(patched_imgs[1], [121, 122, 123, 124, 137, 138, 139, 140, 153, 154, 155,156])
</details>
# 答案1
**得分**: 4
我们可以使用[`np.r_`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.r_.html)来简化我们的工作,如下所示 -
```python
patch_ranges = np.r_[121:125, 137:141, 153:157]
请注意,这些范围不包括结束值,因此结束值比最终预期值多一个,即125、141
等。
此外,我们可以优化代码,将值分配为1
,如下所示 -
image[np.isin(image, patch_ranges)] = 1
如果最终输出需要分类为0
和1
,似乎是这种情况,我们可以简单地使用np.isin
的掩码来获取最终输出 -
image1 = np.isin(image, patch_ranges).astype(int)
英文:
We could use np.r_
to ease our efforts there, like so -
patch_ranges = np.r_[121:125, 137:141, 153:157]
Note that the ranges exclude the end value, hence the ending values are one more than the final intended values, i.e. 125, 141,
etc.
Furthermore, we could optimize on the code, to assign 1s
, like so -
image[np.isin(image, patch_ranges)] = 1
If the final output is to be categorized into 0s
and 1s
as seems to be the case, we can simply use the mask off np.isin
to get the final output -
image1 = np.isin(image, patch_ranges).astype(int)
答案2
得分: 1
这是否符合您的意思?
英文:
Would this be what you had in mind?
min_max_values = {121:124, 137:140, 153:156}
numbers = []
[numbers.extend(list(range(x,y))) for x,y in min_max_values.items()]
image1 = label_patch(patched_imgs[1], numbers)
答案3
得分: 0
你可以这样做:
def label_patch_gen(*patch_ranges):
"""生成器,如果数字在给定范围之一则产生True,否则产生False,数字范围为0到255"""
for n in range(256):
yield any(start <= n <= stop for start, stop in patch_ranges)
def label_patch(*patch_ranges):
"""基于`label_patch_gen`创建一个由1和0组成的numpy数组"""
return np.array(list(label_patch_gen(*patch_ranges)), dtype=int)
然后你可以像这样调用它:
image1 = label_patch((121, 124), (137, 140), (153, 156))
请注意,无需将实际图像传递给函数,因为你无论如何都会返回一个标签列表。
英文:
You could do something like this:
def label_patch_gen(*patch_ranges):
""" Generator that yields True if a number is in one of the given ranges
or False if not, for numbers from 0 to 255 """
for n in range(256):
yield any(start <= n <= stop for start, stop in patch_ranges)
def label_patch(*patch_ranges):
""" Create a numpy array of 1s and 0s based on `label_patch_gen` """
return np.array(list(label_patch_gen(*patch_ranges)), dtype=int)
You would then call it like this:
image1 = label_patch((121, 124), (137, 140), (153, 156))
Note there is no need to pass the actual image to the function, as you are returning a list of labels anyway.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论