基于numpy阵列的图像重建
我有一个函数,它可以将我的初始img分割成100x100个数组基于numpy阵列的图像重建,numpy,opencv,Numpy,Opencv,我有一个函数,它可以将我的初始img分割成100x100个数组 def blockshaped(arr, nrows, ncols): h, w = arr.shape return (arr.reshape(h//nrows, nrows, -1, ncols) .swapaxes(1,2) .reshape(-1, nrows, ncols)) 然后,如果enumerate==偶数,则该循环生成一个新的_img,其
def blockshaped(arr, nrows, ncols):
h, w = arr.shape
return (arr.reshape(h//nrows, nrows, -1, ncols)
.swapaxes(1,2)
.reshape(-1, nrows, ncols))
然后,如果enumerate==偶数,则该循环生成一个新的_img,其中填充了零数组,如果enumerate==偶数,则生成块
image = np.full((1000,2000),5)
blocks = blockshaped(image,100,100)
new_image = []
for i,block in enumerate(blocks):
if i%2==0:
new_image.append(np.zeros((100,100)))
else:
new_image.append(block)
现在我不知道如何构建一个最终图像1000x2000,其中填充了我的新图像阵列
有什么想法吗?你可以颠倒你在
blockshape()
中应用的步骤来实现这一点。注意,我重命名了一些变量以进行描述
更短
将numpy作为np导入
def块状(arr、nrows、ncols):
r、 c=棱形#(r,c)
返回(arr.reformate(r//nrows,nrows,c//ncols,ncols)#->(r/n,n,c/m,m)
.swapaxes(1,2)#->(r/n,c/m,n,m)
.重塑(-1,nrows,ncols))#->(r/n*c/m,n,m)
r、 c=10002000
n、 m=100100
图像=np.满((r,c),5)
块=块形(图像,nrows=n,ncols=m)
#创建新的空图像,只填充偶数块
新图像=np.零((len(块),n,m))
新图像[::2]=块[::2]
#new_image[1::2]=np.full((n,m),13)#如果你想做一些奇怪的事情
#反向块状#(r/n*c/m,n,m)
新建图像=(新建图像。重塑(r//n,c//m,n,m)#->(r/n,c/m,n,m)
.swapaxes(1,2)#->(r/n,n,c/m,m)
.重塑(图像.形状)#->(r,c)
将matplotlib.pyplot作为plt导入
plt.imshow(新图片)
您已使用
image=np.full((10002000),5)
块=块形状(图像,100100)
然后,如果计数器是奇数,则需要用块
值填充新数组
new_image=[]
对于i,枚举中的块(块):
如果i%2==0:
新的_image.append(np.zeros((100100)))
其他:
新建图像。追加(块)
- 不要将
初始化为空列表,而是初始化为numpy zeros数组。问题是新图像的new_image
形状是什么
- 如果希望每行都是块值,则需要声明大小:
-
new_image=np.zero(形状=(200010000))
- 因为每个
都是100x100=10000,并且块的大小是200x100x100。我们放置每一行块
值,例如block
new_image[0,10000]=block
-
将numpy导入为np 从PIL导入图像 def块形状(arr、n行、n列): h、 w=阵列形状 返回(arr.reformate(h//n_行,n_行,-1,n列)。交换(1,2)。reformate(-1,n_行,n列)) 图像=np.满((10002000),5) 块=块形状(图像,100100) new_image=np.zero(shape=(200010000),dtype=np.uint8) 对于i,枚举中的块(块): 如果我是%2!=0: 新的_图像[i,:]=block.flatte() new\u image=image.fromarray(new\u image) 新建_image.show()
- 结果将是一个空白图像,但如果您将
更改为image=np.full((10002000),5)
。那么结果将是:image=np.full((10002000),155)
- 上部变成灰色
-
- 如果您只想为每个奇数i设置块中的部分图像
:(100100)
-
将numpy导入为np 从PIL导入图像 def块形状(arr、n行、n列): h、 w=阵列形状 返回(arr.reformate(h//n_行,n_行,-1,n列)。交换(1,2)。reformate(-1,n_行,n列)) 图像=np.满((10002000),155) 块=块形状(图像,100100) 新图像=np.Zero(形状=(10002000),数据类型=np.uint8) 对于i,枚举中的块(块): 如果我是%2!=0: 新_图像[i:100+i,i:100+i]=块 new\u image=image.fromarray(new\u image) 新建_image.show()
- 结果:
-