Search code examples
matlabimage-processingblockaddition

how to store block after block processing back to full image


I have to perform a block-wise multiplication and store the block-wise result back to an image of full (same) dimension as of input image. I have developed some code but it's unable to store the individual block. The input image is of size 256*256. Block of size 5*5. The output image should be of 256*256. (i.e Mul is of size (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;

Solution

  • You can use mat2cell to convert your image into cell containing 5 x 5 blocks. Since 256 does not divide by 5, you will have to do some adjustment. You can use mat2tiles that already does the adjustment. If you don't want to use external files, just check how they adjust for that. Anyway, you should be able to do it like this:

    % 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);