【问题标题】:Reshape array of images into a column vector, sorted row by row for each image将图像数组重塑为列向量,为每个图像逐行排序
【发布时间】:2015-05-03 09:04:43
【问题描述】:

尊敬的 Matlab 专家您好,

我有一个图像数组,比如说一个 2 x 3 的图像网格(6 个图像)。每个图像的分辨率为 4 x 4(为简单起见)。可以说图像是灰度的。

我将图像加载到尺寸为 2 x 3 x 4 x 4 的 4D 矩阵中。现在我想创建一个包含条目的列向量

1:图像 1,1 中第一行的第一个像素

2:图像 1,1 中第一行的第二个像素

3: ...

16: 图像 1, 1 中最后一行的最后一个像素

17: 图像 2, 1 中第一行的第一个像素

...

以此类推。我可以用一堆 for 循环成功地创建它:

for imageX = 1 : resolution(2)
   for imageY = 1 : resolution(1)
      for pixelX = 1 : resolution(4)
         for pixelY = 1 : resolution(3)

               % linear index for 4D indices
               row = ((imageY - 1) * resolution(2) + imageX - 1) * resolution(3) * resolution(4) + ...
                   (pixelY - 1) * resolution(4) + pixelX;

               lightFieldVector(row) = lightField(imageY, imageX, pixelY, pixelX);
         end
      end
   end
end

我想知道这堆丑陋的循环是否可以用几个reshapepermute 操作来代替。我想是的,但我很难找到正确的顺序。我使用了这些方法几次,但只使用了 2D 矩阵。从文档中我可以得出结论,重塑将是“列优先”,所以这与我需要的相反。

感谢您的帮助, 阿德里安

【问题讨论】:

    标签: image matlab matrix reshape permute


    【解决方案1】:

    这应该是正确的顺序 -

    lightFieldVector = reshape(permute(lightField,[4 3 2 1]),[],1)
    

    这里的全部内容是关于在 MATLAB 中了解 linear indexingpermute


    逆向过程-

    R = resolution
    lightField = permute(reshape(lightFieldVector,R(4),R(3),R(2),R(1)),[4 3 2 1])
    

    【讨论】:

    • 哇,这个答案很快!
    • 反向过程呢。会不会是这样:lightField = permute(reshape(lightFieldVector, resolution), [4, 3, 2, 1]); ?
    • 你不知道光速是在 Divakar 之后受到启发的吗? (笑)。 +1
    • 唯一让他慢下来的是从他的电脑到 stackoverflow 服务器的电缆:D
    • @aeduG haha​​ 很高兴为您制作color
    猜你喜欢
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 2017-06-24
    • 1970-01-01
    • 2016-05-20
    • 2021-05-26
    相关资源
    最近更新 更多