如何在MATLAB中将图像分割成块?

13
我有一张256x256的图片,我想将其分成4个大小为128x128的块,并将它们分别称为A1到A4。现在我想单独调用它们并对它们进行一些操作。我知道可以使用blkproc函数来实现这一点--但是具体怎么做呢?
我应该像这样调用blkproc吗?
B=blkproc(I,[4 4],?)

我要在问号处放什么?如何处理创建的4个块?

5个回答

12

由于blockproc(和已弃用的blkproc)都是图像处理工具箱中的函数,因此我想添加一个基本的MATLAB解决方案,不需要额外的工具箱...

如果您想将矩阵分成子矩阵,一种方法是使用mat2cell将矩阵分解并将每个子矩阵存储在单元格数组的单元格中。对于您的情况,语法如下:

C = mat2cell(I, [128 128], [128 128]);

C现在是一个2x2的单元格数组,每个单元格存储一个128x128的I子矩阵。如果您想对每个单元格执行操作,可以使用函数cellfun。例如,如果您想要计算每个子矩阵中值的平均值,则应执行以下操作:

meanValues = cellfun(@(x) mean(x(:)), C);

第一个参数是指向匿名函数函数句柄, 它首先将每个子矩阵重塑为列向量,然后取平均值。输出是一个2x2矩阵,其中包含每个子矩阵的平均值。如果您传递给cellfun的函数对于每个单元格创建不同大小或类型的输出,则cellfun将无法连接它们并会抛出错误:

??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

如果在调用cellfun时添加..., 'UniformOutput', false);,则上述情况的输出将变为一个2x2的单元数组,其中包含对每个子矩阵执行操作的结果。

7

blockprocblkproc 的新名称(已废弃)。它可以用于对图像中的每个块应用函数。例如,如果您想将矩阵 I 划分为 8x8 块并计算每个块的平均值,则可以执行以下操作:

B=blockproc(I, [8 8], @(x) mean(x.data(:)));
B是包含块平均值的矩阵。
这里需要注意两点:
  • The specifier [8 8] specifies the size of the blocks, not the number of blocks.

  • You don't get access to the blocks themselves outside of the function you pass to blockproc. If you need the blocks themselves, you have to do as Adrien suggested:

    A1=I(1:128, 1:128);
    A2=I(129:256, 1:128);
    A3=I(1:128, 129:256);
    A4=I(129:256, 129:256);
    

    Of course, in a real program, you should probably do this using a loop.


2
如果您仍在使用带有BLKPROC函数的旧版本图像处理工具箱,则上述示例的语法将更改为:B = blkproc(I,[8 8],@(x) mean(x(:))); - gnovice

3
如果myImage是你的256x256像素的图片,那么它会是这样:
image_top_left = myImage(1:128,1:128);
image_top_right = myImage(1:128,129:256);
image_bottom_left = myImage(129:256,1:128);
image_bottom_right = myImage(129:256,129:256);

?


1
最好让您的程序适用于所有大小的图像,而不仅限于256 * 256。

[row, col]=size(your_image);
mr = round(row/2); % median of rows
mc = round(col/2); % median of columns
% Now divide your image and call each of them separately and do what ever you   want
top_left  = your_image(1:mr  , 1:mc);
top_right = your_image(1:mr  , (mc+1):col); 
bot_left  = your_image((mr+1):row , 1:mc);
bot_right = your_image((mr+1):row , (mc+1):col);
% final stage is to combining these parts again to return to its original shape 
Back_to_original = [top_left,top_right ; bot_left,bot_right]; `   


希望这对你有用。

0
如果您想将图像分成大小为gs(例如2、3、4、5、6、7、8、9、10等)的正方形,则可以使用以下代码。此函数从输入图像中提取gs x gs补丁并将它们存储在“patch”变量中。请注意,如果输入图像的宽度或高度不是gs的倍数,则除法余数相等的偏移量将不会被包含在补丁提取中。
function [ patchim , npatchim ] = divideimage (im , gs)
 imheight=size(im,1);
 imwidth=size(im,2);
maxgsrow =  floor( imheight / gs);
maxgscol =  floor( imwidth / gs );
npatch = 1;
for i = 1 : maxgsrow
  for j = 1 : maxgscol
    rmin = ( i - 1 ) * gs + 1;
    rmax = i * gs;
    cmin = ( j - 1) * gs + 1;
    cmax = j * gs;
%%%%%%    do processes
    patch ( : , : , : , npatch) = im( rmin : rmax , cmin : cmax , : );
    npatch = npatch + 1;
  endfor
endfor   
 npatchim = npatch - 1;
patchim = patch;
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接