2015-06-09 50 views
3

我正在尝试将多维数组重塑成原始图像。我已拆分使用很好的解决方案,我发现in this question一个在8×8像素的子矩阵512×512像素的图像:在此情况下N = M = 8和sub_imagesMatlab重塑成原始图像

sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), n, []), [2 1 3]), n, m, []), [2 1 3]); 

是8x8x4096阵列。现在问题是我想回到原始图像避免for循环,但我不明白如何去做。我知道存在函数colfiltblockproc,但我无法使用它们。任何帮助是极大的赞赏!

回答

1

只是做你用来重塑原始数组的东西的相反。该置换指令保持不变(切换第一和第二个维度),而重塑命令回去高达512

reshaped_i_image = reshape(permute(reshape(permute(sub_images, [2 1 3]), 8, 512, []), [2 1 3]), 512, 512); 
+0

谢谢!你解决了我的问题。 – BugsFree

1

您可以用两个reshape的和一个permute解决这个问题 -

out = reshape(permute(reshape(sub_images,n,m,512/n,512/m),[1 3 2 4]),512,[]); 

这里需要注意的是,permute昂贵的,只要有可能就必须避免。


下一个上市是在解决了上述问题,大小的运行时的测试方法上市至今 -

i_image = rand(512,512); 
n = 8; m = 8; 
sub_images = permute(reshape(permute(reshape(i_image, size(i_image, 1), ... 
            n, []), [2 1 3]), n, m, []), [2 1 3]); 

func1 = @() reshape(permute(reshape(sub_images,n,m,512/n,512/m),... 
                 [1 3 2 4]),512,[]); 
func2 = @() reshape(permute(reshape(permute(sub_images, [2 1 3]), ... 
             8, 512, []), [2 1 3]), 512, 512); 


>> timeit(func1) 
ans = 
    0.0022201 
>> timeit(func2) 
ans = 
    0.0046847