【问题标题】:Matlab reshape back into original imageMatlab重塑回原始图像
【发布时间】:2015-08-24 15:26:18
【问题描述】:

我正在尝试将多维数组重塑为原始图像。我使用我找到的in this question 的出色解决方案将 512x512 像素的图像拆分为 8x8 像素的子矩阵:

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

在这种情况下 n=m=8 并且 sub_images 是一个 8x8x4096 的数组。现在的问题是我想回到原始图像避免 for 循环,但我不明白该怎么做。我知道存在colfiltblockproc的功能,但我不能使用它们。非常感谢任何帮助!

【问题讨论】:

    标签: matlab matrix multidimensional-array vectorization reshape


    【解决方案1】:

    只需与您用来重塑原始数组的相反操作即可。 permute 命令保持不变(切换第一维和第二维),而 reshape 命令返回到 512

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

    【讨论】:

      【解决方案2】:

      你可以用两个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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-31
        • 1970-01-01
        • 2010-11-28
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多