MATLAB - 合并子矩阵 [英] MATLAB - Merge submatrices

查看:33
本文介绍了MATLAB - 合并子矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

处理完每个块后,我想再次重新构图.问题是行数和列数是动态的,所以我不能用(或者不知道这里怎么用)cat命令或者[firstsubmatrix secondsubmatrix] 语法.

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?

推荐答案

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

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

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

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.

如果您可以访问图像处理工具箱,我还会查看blkproc"功能.

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

这篇关于MATLAB - 合并子矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆