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

查看:85
本文介绍了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的数据矩阵

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天全站免登陆