MATLAB - 合并子矩阵

发布于 2024-11-14 06:58:11 字数 844 浏览 0 评论 0原文

我正在 MATLAB 中处理图像处理项目。为了更轻松地预处理图像,我将其分为行和列,因此从原始图像(2D uint8 矩阵),现在我有一个 3D 矩阵,就像堆栈一样。

图像分解

处理完每个块后,我想再次重新组合图像。问题在于行数和列数是动态的,因此我无法使用(或不知道如何在此处使用它)cat 命令或[firstsubmatrix secondarysubmatrix] 语法。

顺便说一句,我这样进行划分:

numRows = 3
numCols = 3
blockHeight = originalHeight / numRows;
blockWidth = originalWidth / numCols;

blocks = uint8(zeros(numCols * numRows, blockHeight, blockWidth));

因此,对于每个块,我使用以下方式填充其内容

y0 = (row - 1) * rowHeight + 1;
y1 = row * rowHeight;
x0 = (col - 1) * rowWidth + 1;
x1 = col * rowWidth;

blocks(numBlock, :, :) = originalImage(y0:y1, x0:x1);

:是否有更好的方法,以及将块连接起来的任何方法?

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 uint8 matrix), now I have a 3D matrix, like a stack.

Image decomposition

After processing each block, I want to recompose the image again. The problem is that the number of rows and columns is dynamic, so I can't use (or don't know how to use it here) the cat command or the [firstsubmatrix secondsubmatrix] syntax.

By the way, I do the division like this:

numRows = 3
numCols = 3
blockHeight = originalHeight / numRows;
blockWidth = originalWidth / numCols;

blocks = uint8(zeros(numCols * numRows, blockHeight, blockWidth));

So for each block, I fill its content using

y0 = (row - 1) * rowHeight + 1;
y1 = row * rowHeight;
x0 = (col - 1) * rowWidth + 1;
x1 = col * rowWidth;

blocks(numBlock, :, :) = originalImage(y0:y1, x0:x1);

Is there a better way of doing it, and any way of having the blocks joined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-11-21 06:58:11

如果我正确理解你的问题,那么我会这样做:
假设我们有一些尺寸为 m × n 的数据矩阵

[m n] = size(data);

rows_wanted = 10;
cols_wanted = 10;
submatrix_rows = rows_wanted*ones(1,m/rows_wanted);
submatrix_cols = cols_wanted*ones(1,n/cols_wanted);
data_cells = mat2cell(data,submatrix_rows,submatrix_cols);
for k1 = 1:submatrix_rows;
    for k2 = 1:submatrix_cols;
        proc_data_cells{k1,k2} = function_for_matrics(data_cells{k,l});
    end
end
proc_data_mtx = cell2mat(proc_data_cells);

,将您的数据转换为一个单元格,其中单元格的每个元素都是一个子矩阵,然后遍历每个元素,执行您的函数,并将其输出到一个新单元格。使用 cell2mat 输出完全连接的处理矩阵。

如果您有权访问图像处理工具箱,我还会查看“blkproc”函数。

If I am understanding your question correctly, then this is how I would do it:
Assume we have some data matrix with dimensions m by n

[m n] = size(data);

rows_wanted = 10;
cols_wanted = 10;
submatrix_rows = rows_wanted*ones(1,m/rows_wanted);
submatrix_cols = cols_wanted*ones(1,n/cols_wanted);
data_cells = mat2cell(data,submatrix_rows,submatrix_cols);
for k1 = 1:submatrix_rows;
    for k2 = 1:submatrix_cols;
        proc_data_cells{k1,k2} = function_for_matrics(data_cells{k,l});
    end
end
proc_data_mtx = cell2mat(proc_data_cells);

convert your data into a cell, where each element of the cell is a submatrix, then go through each element, preform your function, and output it to a new cell. Use cell2mat to output a fully concatenated processed matrix.

If you have access to the Image Processing Toolbox, I would also check out the 'blkproc' function.

把回忆走一遍 2024-11-21 06:58:11

关于如何根据您的图表在 2-D 矩阵和 3-D 矩阵之间来回转换的具体问题,我首先假设 originalHeightOriginalWidth 可以分别被 numRowsnumCols 整除。基于我提供的解决方案之前的类似问题问,这是一个仅使用矩阵重塑和排列的解决方案:

%# 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);

请注意,3D 矩阵中的块沿第三维连接,因此 blocks(:,:,i) 是二维矩阵中的第 i 块。另请注意,这些解决方案将以行方式提取并填充二维矩阵中的块。换句话说,如果originalImage = [A1 A2; A3 A4];,然后 blocks(:,:,1) = A1;blocks(:,:,2) = A2; 等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文