生成矩形补丁的索引 [英] Generate indices of a rectangular patch

查看:72
本文介绍了生成矩形补丁的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理以矩阵表示的图像时,我经常发现自己生成矩形补丁的索引,例如:

In dealing with images represented as a matrix, I frequently find myself generate indices of a rectangular patch like:

si, sj = 0, 0  # upper-left corner
w, h = 2, 2  # patch width & height

indices = ((i, j) for i in range(si, si + h) for j in range(sj, sj + w))
print(list(indices))

# process corresponding pixels

请注意

  1. 生成器代码很长,无法很好地适合于一行,而我找不到一种优雅的方式将其组织成多个代码.
  2. 嵌套的for循环不好,因为我需要迭代处理才能进入下一个处理步骤.

我感觉在Python中存在一种超级优雅的方式来做到这一点.在那儿?还是用任何优雅的方式将生成器代码组织成多行而无需使用行连续\?

I have a feeling that there exists a super elegant way to do this in Python. Is there? Or any elegant way to organize the generator code into multiple lines without using line continuation \?

推荐答案

从此线程查找我想要的解决方案

Find the solution I want myself from this thread.

from itertools import product

si, sj = 0, 0  # upper-left corner
w, h = 2, 2  # patch width & height
indices = product(range(si, si + h), range(sj, sj + w))
print(list(indices))

这篇关于生成矩形补丁的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆