将图像分成3 * 3块 [英] divide the image into 3*3 blocks

查看:243
本文介绍了将图像分成3 * 3块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,其尺寸不是3的倍数,也可能是。
我们如何将整个图像分成3 * 3矩阵的块。
(可以忽略那些不属于3 * 3倍数的最后一个。
另外,3 * 3矩阵可以保存在数组中。

I have a matrix that does not happen to have dimensions that are multiples of 3 or it might. How can we divide the entire image into blocks of 3*3 matrices. (Can ignore the last ones which does not come under the 3*3 multiples. Also, the 3*3 matrices can be be saved in arrays.

a=3; b=3; %window size
x=size(f,1)/a; y=size(f,2)/b; %f is the original image
m=a*ones(1,x); n=b*ones(1,y);
I=mat2cell(f,m,n);


推荐答案

我从未使用过mat2cell来划分矩阵,现在好像在想它这是一个非常好的主意。由于我这台计算机上没有MATLAB,我将描述我的方式,不涉及mat2cell。

I have never used mat2cell to divide matrices, and thinking about it now it seems like a really good idea. As I don't have MATLAB here in this computer, I'll describe the way I do it, which does not involve mat2cell.

忽略最后的列和行很简单:

Ignoring the last columns and rows is easy:

d = 3; % the dimension of the sub matrix
[x,y] = size(f);

% perform integer division by three
m = floor(x/d);
n = floor(y/d);

% find out how many cols and rows have to be left out
m_rest = mod(x,d);
n_rest = mod(y,d);

% remove the rows and columns that won't fit
new_f = f(1:(end-m_rest), 1:(end-n_rest));

%  this steps you won't have to perform if you use mat2cell
% creates the matrix with (m,n) pages 
new_f = reshape( new_f, [ d m d n ] );
new_f = permute( new_f, [ 1 3 2 4 ] );

现在你可以访问这样的子矩阵:

Now you can access the sub-matrices like this:

new_f(:,:,1,1) % returns the 1st one

new_f(:,:,3,2) % returns the one at position [3,2]

如果您想使用mat2cell来做到这一点,那么你可以执行以下操作:

If you'd like to use mat2cell to do that, you could do something like the following:

% after creating new_f, instead of the reshape, permute
cells_f = mat2cell(new_f, d*ones(1,m), d*ones(1,n));

然后你会以不同的方式访问它:

Then you would access it in a different way:

cells_f{1,1}
cells_f{3,2}

我无法测试的单元格方法,因为我在这台PC上没有MATLAB,但是如果我能正确回想起mat2cell的用法,它应该可以正常工作。

The cell approach I cannot test because I don't have MATLAB on this PC, but if I can recall the usage of mat2cell correctly, it should work fine.

希望有所帮助:)

这篇关于将图像分成3 * 3块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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