MATLAB - Merge submatrices

后端 未结 2 899
离开以前
离开以前 2020-12-11 05:46

I\'m working on an image processing project in MATLAB. In order to preprocess the image more easily, I\'ve divided it in rows and columns, so from a original image (a 2D uin

2条回答
  •  时光取名叫无心
    2020-12-11 06:06

    With regard to your specific question about how you could convert back and forth between a 2-D matrix and a 3-D matrix according to your diagram, I am first going to assume that originalHeight and originalWidth can be evenly divided by numRows and numCols, respectively. Building on a solution I gave to a similar problem that was previously asked, here is a solution using only reshapes and permutations of the matrices:

    %# Convert from 2-D to 3-D:
    blocks = reshape(permute(reshape(originalImage,blockHeight,blockWidth,[]),...
                             [1 3 2]),blockHeight,blockWidth,[]);
    
    %# Convert from 3-D to 2-D:
    newImage = reshape(permute(reshape(blocks,blockHeight,[],blockWidth),...
                               [1 3 2]),originalHeight,originalWidth);
    

    Note that the blocks in the 3-D matrix are concatenated along the third dimension, so blocks(:,:,i) is the ith block from the 2-D matrix. Note also that these solutions will extract and fill blocks in the 2-D matrix in a row-wise fashion. In other words, if originalImage = [A1 A2; A3 A4];, then blocks(:,:,1) = A1;, blocks(:,:,2) = A2;, etc.

提交回复
热议问题