微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在每个图像像素中为 2D 图像创建居中的补丁

如何解决在每个图像像素中为 2D 图像创建居中的补丁

大家好,我正在尝试从 2D 图像创建补丁。我需要这些补丁必须在每个像素图像中居中。我正在使用此代码

#patches for each point in 2D slice
patch_size = 27
pacth_m_size = patch_size//2
for x in range(0,sld_arr_norm.shape[0]):
    for y in range(0,sld_arr_norm.shape[1]):
        if x-pacth_m_size>0: # if all is ok the get the patch
            if y-pacth_m_size>0:
                if x+pacth_m_size<sld_arr_norm.shape[1]:
                    if y+pacth_m_size<sld_arr_norm.shape[1]:
                        x_i = x-pacth_m_size
                        x_s = x+pacth_m_size+1
                        y_i = y-pacth_m_size
                        y_s = y+pacth_m_size+1
                        curr_patch= sld_arr_norm[x_i:x_s,y_i:y_s]
                        assert curr_patch.shape == (patch_size,patch_size)
                        print(curr_patch.shape)
        else:
            x_i = x-pacth_m_size
            x_s = x+pacth_m_size+1
            y_i = y-pacth_m_size
            y_s = y+pacth_m_size+1
            if x-pacth_m_size<0:
                issue_patch = sld_arr_norm[0:x_s,y_i:y_s]
                curr_patch  = np.zeros((patch_size,patch_size))
                star_index  = abs(x-pacth_m_size)
                curr_pacth[star_index:,:]=issue_patch.copy()
            if y-pacth_m_size<0:
                issue_patch = sld_arr_norm[x_i:x_s,0:y_s]
                curr_patch  = np.zeros((patch_size,patch_size))
                star_index  = abs(y_i)
                curr_pacth[:,star_index:]=issue_patch.copy()
            if y+pacth_m_size>sld_arr_norm.shape[1]:
                issue_patch = sld_arr_norm[x_i:x_s,patch_size))
                end_index   = abs(y_s-issue_patch.shape[1])
                curr_patch[0:,0:curr_patch.shape[1]-end_index]=issue_patch.copy() #issue_patch[x_i:x_s,y_i:issue_patch.shape[1]]
            if x+pacth_m_size>sld_arr_norm.shape[0]:
               issue_patch = sld_arr_norm[x_i:x_s,y_i:y_s]
               curr_patch  = np.zeros((patch_size,patch_size)) 
               end_index   = abs(x_s-issue_patch.shape[0])
               curr_patch[0:arr_zeros.shape[0]-end_index,:]=issue_patch.copy()
            assert curr_patch.shape == (patch_size,patch_size)
            print(curr_patch.shape)

问题出在图像的边界上 我面临着一些问题,例如补丁不依赖于定义的补丁大小。您知道任何允许以这种方式创建补丁的库吗?

解决方法

最好的方法是首先填充整个图像。然后,我们可以继续提取补丁而不必担心边缘。代码如下所示:

#Define patch size 
patch_size   = 13
pacth_m_size = patch_size//2
#Patch the whole image 
sld_arr_norm_pad = np.pad(sld_arr_norm,pacth_m_size,'wrap')
for x in range(0,sld_arr_norm.shape[0]):
    for y in range(0,sld_arr_norm.shape[1]):
        x_real = x + pacth_m_size
        y_real = y + pacth_m_size

        x_i = x_real - pacth_m_size
        x_s = x_real + pacth_m_size + 1
        y_i = y_real - pacth_m_size
        y_s = y_real + pacth_m_size + 1

        curr_patch = sld_arr_norm_pad[x_i:x_s,y_i:y_s]
        print(curr_patch.shape)
        print((x_i,x_s,y_i,y_s))
        assert curr_patch.shape == (patch_size,patch_size)

希望这对其他人有帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?