给定为矩形图像img
和面片s
。现在,我想用边长为s
的正方形面片覆盖整个图像,这样img
中的每个像素至少在一个面片中,使用最少数量的面片。此外,我希望相邻的面片有尽可能少的重叠
到目前为止:我已经在下面包含了我的代码,并给出了一个示例。然而,它还不能完美地工作。希望有人能找到错误
示例:给定的是img
的形状:(4616, 3016)
和s = 224
这意味着我将在较长的一侧添加21个补丁,在宽度较小的一侧添加14个补丁,总共添加21*14=294个补丁
现在,我试图找出补丁如何分布补丁之间的重叠。
我的补丁可以覆盖大小为:(4704, 3136)
的图像,因此我的补丁在高度上必须覆盖88个重叠像素missing_h = ht * s - h
,宽度类似
现在我想弄清楚,如何在21个补丁上分配88个像素。88 = 4* 21 + 4
因此,我将有hso = 17
个重叠的面片shso = 4
和hbo = 4
个重叠5的面片,宽度类似
现在我只需在整个图像上循环,并跟踪我当前的位置(cur_h, cur_w)
。在每个循环之后,我调整,cur_h, cur_w
。我有s
,我当前的补丁编号i, j
,它指示补丁是有小重叠还是有大重叠
import numpy as np
def part2(img, s):
h = len(img)
w = len(img[0])
ht = int(np.ceil(h / s))
wt = int(np.ceil(w / s))
missing_h = ht * s - h
missing_w = wt * s - w
hbo = missing_h % ht
wbo = missing_w % wt
hso = ht - hbo
wso = wt - wbo
shso = int(missing_h / ht)
swso = int(missing_w / wt)
patches = list()
cur_h = 0
for i in range(ht):
cur_w = 0
for j in range(wt):
patches.append(img[cur_h:cur_h + s, cur_w: cur_w + s])
cur_w = cur_w + s
if j < wbo:
cur_w = cur_w - swso - 1
else:
cur_w = cur_w - swso
cur_h = cur_h + s
if i < hbo:
cur_h = cur_h - shso - 1
else:
cur_h = cur_h - shso
if cur_h != h or cur_w != w:
print("expected (height, width)" + str((h, w)) + ", but got: " + str((cur_h, cur_w)))
if wt*ht != len(patches):
print("Expected number patches: " + str(wt*ht) + "but got: " + str(len(patches)) )
for patch in patches:
if patch.shape[0] != patch.shape[1] or patch.shape[0] != s:
print("expected shape " + str((s, s)) + ", but got: " + str(patch.shape))
return patches
def test1():
img = np.arange(0, 34 * 7).reshape((34, 7))
p = part2(img, 3)
print("Test1 successful")
def test2():
img = np.arange(0, 4616 * 3016).reshape((4616, 3016))
p = part2(img, 224)
print("Test2 successful")
test1()
test2()
通过进行以下编辑,可以修复上述问题:
相关问题 更多 >
编程相关推荐