arraysimagematlabmontage

Saving output of 3 channel image after using a filter array with montage collage in Matlab


We want to run our filter bank on an image by convolving each filter in the bank with the image and concatenating all the responses into a vector for each pixel. Using the imfilter command in a loop to do this. Since color images have 3 channels, we are going to have a total of 3F filter responses per pixel if the filter bank is of size F . N Then output the result as a 3F channel image

final = [];
for i = 1: length(filterBank)
    x = imfilter(img, cell2mat(filterBank(i)));
    imshow(x);
    final(i)= [x]
end

where :

filterBank is a 20X1 cell ; filterBank(i) is 5X5 double

img is a 230X307X3 uint8

final is just []

this gives an error Subscripted assignment dimension mismatch.

Then I tried :

final(1:1:1) = x

this gives an error : In an assignment A(:) = B, the number of elements in A and B must be the same.

I'm a matlab noob but basically want to save all the 3d matrices result on applying each filter to a single array then apply a montage command


Solution

  • You're trying to set a single element (final(i)) to the value of a matrix. Use cell arrays instead.

    % pre-allocate for memory friendliness
    final = cell(size(filterBank));
    % Loop using ii not i, as i=sqrt(-1) by default in MATLAB
    % Also using numel, as length is only the size in the largest direction
    for ii = 1:numel(filterBank)
        % You say filterBank is a cell, so use {}, then shouldn't need cell2mat()
        x = imfilter(img, filterBank{ii});
        imshow(x);
        % Curly braces because we're indexing a cell now
        final{ii} = x;
    end
    

    Alternatively you could use a 4D matrix (3D for x, 1D for loop variable),

    final = zeros([size(img), numel(filterBank)]); % pre-allocation instead of cell
    for ii = 1:numel(filterBank)
        x = imfilter(img, filterBank{ii});
        imshow(x);
        % assign to ii-th layer of 3D matrix
        final(:,:,:,ii) = x;
    end