MATLAB - 合并子矩阵
我正在 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,那么我会这样做:
假设我们有一些尺寸为 m × n 的数据矩阵
,将您的数据转换为一个单元格,其中单元格的每个元素都是一个子矩阵,然后遍历每个元素,执行您的函数,并将其输出到一个新单元格。使用 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
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.
关于如何根据您的图表在 2-D 矩阵和 3-D 矩阵之间来回转换的具体问题,我首先假设
originalHeight
和OriginalWidth
可以分别被numRows
和numCols
整除。基于我提供的解决方案之前的类似问题问,这是一个仅使用矩阵重塑和排列的解决方案:请注意,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
andoriginalWidth
can be evenly divided bynumRows
andnumCols
, 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: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, iforiginalImage = [A1 A2; A3 A4];
, thenblocks(:,:,1) = A1;
,blocks(:,:,2) = A2;
, etc.