【问题标题】:Reshaping long vertical RGB image array into layered horizontal sections in Octave在 Octave 中将长垂直 RGB 图像阵列重塑为分层的水平部分
【发布时间】:2021-09-30 00:32:09
【问题描述】:

我正在尝试将一个长的垂直图像数组(多维 RGB 数组)重塑为 Octave 中的分层水平部分。

我有一个很长的垂直图像阵列 (32734x1x3)。 如何重塑图像数组,以便我 长垂直图像转换为 52x640x3 的分层水平图像数组,而数组的其余部分用 0 填充以使其“方方正正”

我在查看reshape,但不知道如何在多个维度上重塑数组,同时创建分层的水平部分。

简单示例:数组中剩余的空间用零填充。

我的逻辑/测试是:

a=[1:32734]';

num_cols_wanted=640
num_of_rows_calc=ceil(size(a,1)/num_cols_wanted) %use ceil to get whole number rounded up
num_cells_to_add=mod(size(a,1),num_cols_wanted) %extra cells needed to even array out

b = zeros(num_of_rows_calc, num_cols_wanted); %preallocate 

b=reshape(a,[num_of_rows_calc,num_cols_wanted]); %place reshaped a array into preallocated b array

在测试脚本结束时,我在将原始 array a 重塑为更大的预分配 array b 时遇到问题。我收到一个错误无法重塑 32734x1

PS:我使用的是 Octave 5.2

【问题讨论】:

    标签: arrays image multidimensional-array octave rgb


    【解决方案1】:

    使用reshape 时,输入元素的数量必须等于输出(重构)元素的数量。

    在您的情况下:length(a) 必须等于 num_of_rows_calc*num_cols_wanted


    执行b = zeros(...) 然后b = reshape(...) 只会覆盖b 的值(用零填充b 没有帮助)。


    • 您可以用零填充b(创建一个“长”向量),并将a 元素复制到a 的开头:

       b = zeros(num_of_rows_calc*num_cols_wanted, 1);
       b(1:length(a)) = a;
      
    • b的元素数量正确后,我们可以重塑b

        b = reshape(b, [num_cols_wanted, num_of_rows_calc])';
      

      注意: 在 OCTAVE 中重塑向量首先按列(从上到下)对元素进行排序。
      对于按行排序,我们可以重塑为 cols x 行并转置结果。


    完整的代码示例:

    %a=[1:32734]';
    a = (1:10)';
    
    num_cols_wanted=3; %640;
    num_of_rows_calc=ceil(size(a,1)/num_cols_wanted); %use ceil to get whole number rounded up
    num_cells_to_add=mod(size(a,1),num_cols_wanted); %extra cells needed to even array out
    
    b = zeros(num_of_rows_calc*num_cols_wanted, 1); %Create a vector of zeros with desired number of elements.
    
    b(1:length(a)) = a; %Copy a into the b - keeping the zeros at the end of b (we could also add zero padding at the end of a).
    
    %Reshape to num_cols_wanted x num_of_rows_calc and transpose, because OCTAVE ordering is "column major".
    b = reshape(b, [num_cols_wanted, num_of_rows_calc])'; %reshape b array into preallocated b array
    

    结果:

    b =
    
        1    2    3
        4    5    6
        7    8    9
       10    0    0
    

    3D 输出示例:

    a = cat(3, (1:10)', (21:30)', (31:40)');
    
    a = squeeze(a); % Remove redunded dimentsion
    
    num_cols_wanted=4;%640;
    num_of_rows_calc=ceil(size(a,1)/num_cols_wanted); %use ceil to get whole number rounded up
    num_cells_to_add=mod(size(a,1),num_cols_wanted); %extra cells needed to even array out
    
    b = zeros(num_of_rows_calc*num_cols_wanted, 3); %Create 3 columns matrix of zeros with desired number of elements.
    
    b(1:length(a), :) = a; %Copy a into the b - keeping the zeros at the end of b (we could also add zero padding at the end of a).
    
    %Reshape to 3 x num_cols_wanted x num_of_rows_calc and permute, because OCTAVE ordering is "column major".
    b = reshape(b, [num_cols_wanted, num_of_rows_calc, 3]); %reshape b array into preallocated b array
    b = permute(b, [2, 1, 3]);
    

    【讨论】:

    • 我添加了一个 3D 输出示例。我认为这是你的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    相关资源
    最近更新 更多