blkproc
函数来实现这一点--但是具体怎么做呢?我应该像这样调用
blkproc
吗?B=blkproc(I,[4 4],?)
我要在问号处放什么?如何处理创建的4个块?
blkproc
函数来实现这一点--但是具体怎么做呢?blkproc
吗?B=blkproc(I,[4 4],?)
我要在问号处放什么?如何处理创建的4个块?
由于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的单元数组,其中包含对每个子矩阵执行操作的结果。blockproc
是 blkproc
的新名称(已废弃)。它可以用于对图像中的每个块应用函数。例如,如果您想将矩阵 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.
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);
?
[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]; `
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
B = blkproc(I,[8 8],@(x) mean(x(:)));
- gnovice