Display a Gaussian pyramid stored in a cell array in a single figure

1k Views Asked by At

I'm working on a Gaussian Pyramid code for matlab. Basically it loads an image, creates a cell array and fills it with different levels of the gaussian pyramid.

I want to show the content of my cell array filled with images in one single figure, so you can see the gaussian pyramid effect. Meaning the original image is at full size and the rest are downsampled by 2 each. And all that in one figure.

I'm quite the amateur when it comes to Matlab so I don't really know how to do that. I already tried it somewhat with subplots but failed.

Thanks in advance.

2

There are 2 best solutions below

7
On BEST ANSWER

I used a loop to add zeros at the top of all images then merged them

Sample cell,

im = imread('peppers.png');
for i = 1 : 5
    I{i} = im(1 : 2*i : end, 1 : 2*i : end,:); 
end

The code, I being your cell,

m = size(I{1}, 1);
newI = I{1};
for i = 2 : numel(I)
    [q,p,~] = size(I{i});
    I{i} = cat(1,repmat(zeros(1, p, 3),[m - q , 1]),I{i});
    newI = cat(2,newI,I{i});
end
imshow(newI)

enter image description here

For 2D images use : I{i} = cat(1,repmat(zeros(1 , p),[m - q , 1]),I{i});

enter image description here

3
On

How about:

subplot(numel(YourCell), 1, 1), imshow(YourCell{1});
for k=2:5
    subplot(1,numel(YourCell),k), imshow(YourCell{k})
    xlim([1 size(YourCell{1},1)]);
    ylim([1 size(YourCell{1},2)]);
end

Result (with dummy data):

cascade

Edit:

You can play with the arrangement of your tiles by calculating the position of the next one. Here is a quick and dirty example, you can surely do a better job:

Side by side:

border=5;
MergedImage=ones(size(YourCell{1},1), 2.5*size(YourCell{1},2));
MergedImage(1:size(YourCell{1},1), 1:size(YourCell{1},2))=YourCell{1};
Pos=[1, size(YourCell{1},1)+border];

for k=1:(numel(YourCell)-1)
    MergedImage(Pos(1):Pos(1)+size(YourCell{k+1}, 1)-1, Pos(2):Pos(2)+size(YourCell{k+1}, 2)-1)=YourCell{k+1};
    Pos=[Pos(1), Pos(2)+size(YourCell{k+1}, 2)+border];

end

imshow(MergedImage);

cascade2

Or a tighter arrangement:

border=5;
MergedImage=ones(size(YourCell{1},1), 2*size(YourCell{1},2));
MergedImage(1:size(YourCell{1},1), 1:size(YourCell{1},2))=YourCell{1};
Pos=[1, size(YourCell{1},1)+border];

for k=1:(numel(YourCell)-1)
    MergedImage(Pos(1):Pos(1)+size(YourCell{k+1}, 1)-1, Pos(2):Pos(2)+size(YourCell{k+1}, 2)-1)=YourCell{k+1};
    if mod(k,2) == 0
        Pos=[Pos(1)+size(YourCell{k+1}, 1)+border, Pos(2)];
    else
        Pos=[Pos(1), Pos(2)+size(YourCell{k+1}, 2)+border];
    end
end

imshow(MergedImage);

cascade3