我必须执行逐块乘法并将逐块结果存储回输入图像的完整(相同)维度的图像。我已经开发了一些代码,但它无法存储单个块。 输入图片的尺寸为 256 * 256 。块大小 5 * 5 。输出图像应为256 * 256。 (即Mul
的大小(256 * 256))。
clear all;
close all;
I = im2double(imread('cameraman.tif'));
[row,col] = size(I);
block = 5;
Var = zeros(1:block,1:block);
for i = 1:block:row-block
for j= 1:block:col-block
Var = I(i:i+block-1,j:j+block-1);
Dem = reshape(1:25,[5 5])';
Mul = Var.*Dem;
end;
end;
答案 0 :(得分:2)
您可以使用mat2cell将图片转换为包含5 x 5块的单元格。由于256不会除以5,因此您必须进行一些调整。您可以使用已经进行调整的mat2tiles。如果您不想使用外部文件,只需检查它们如何调整。无论如何,你应该能够这样做:
% convert image to tiles
C = mat2tiles(I, [5 5]);
% apply some function to each block. In example just zero all elements in blocks
C2 = cellfun(@(block) block.* 0, C, 'UniformOutput', 0);
% concatenate the results into new image
I2 = cell2mat(C2);
答案 1 :(得分:0)
clear all;
close all;
I = im2double(imread('cameraman.tif'));
[row,col] = size(I);
block = 5;
Var = zeros(1:block,1:block);
% Initialize the output image
I2 = zeros(row,col);
Dem = reshape(1:25,[5 5])'; %'
for i = 1:block:row-block
for j= 1:block:col-block
Var = I(i:i+block-1,j:j+block-1);
Mul = Var.*Dem;
% Remap the result to original size
I2(i:i+block-1,j:j+block-1) = Mul;
end;
end;