## How to store an image into cell arrays in a loop?

### Ilgaz Eyidogan (view profile)

on 27 Oct 2019
Latest activity Commented on by Rik

### Rik (view profile)

on 27 Oct 2019
Hello, I want to create a Gaussian Pyramid which basically can be explained as first subsampling an image (a.k.a. reducing the size of an image) with less distortion in resolution.
So, I want to store each image in pyramid levels into a cell in a cell array.(If there's something wrong with the terminology, pls inform me)
I have written a code, it doesn't give any error but it doesn't work the way I want it to work either. I mean, I want it to display each image in a loop after the operations, but when I run the code nothing appears. Here I will include the code. If you help me to fix it, I would highly appreciate it. I think there might be something wrong with either the function itself(due to syntax errors) or the use of cell arrays. Please help me!!
%% Part 3
%Write function
%Iterate Gaussian filtering and then subsampling 5 times for Gaussian Pyramid
image_path = 'C:\Users\User\Desktop\ILGAZ\2019 Guz\cmpe 565\SecondHomework\woman.jpg';
function gaussPyramid(im)
sigma = 1.5;
numofSubsmp = 5;
imageCell = cell(1,numofSubsmp+1);
imageCell{1} = im;
for i = 1:numofSubsmp + 1
image = imageCell{i};
figure(i), imshow(image)
title('Image')
filtered_image = imgaussfilt(imageCell{i},sigma);
imageCell{i+1} = imresize(filtered_image, 0.5);
end
end

Rik

on 27 Oct 2019

on 27 Oct 2019
Edited by Rik

### Rik (view profile)

on 27 Oct 2019

You forgot to call your function:
%% Part 3
%Write function
%Iterate Gaussian filtering and then subsampling 5 times for Gaussian Pyramid
%image_path = 'C:\Users\User\Desktop\ILGAZ\2019 Guz\cmpe 565\SecondHomework\woman.jpg';
image_path ='pears.png';%RGB demo image
gaussPyramid(im)
% ^ this call is important, as mlint warned you
function gaussPyramid(im)
sigma = 1.5;
numofSubsmp = 5;
imageCell = cell(1,numofSubsmp+1);
imageCell{1} = im;
for i = 1:numofSubsmp + 1
image = imageCell{i};
figure(i), imshow(image)
title('Image')
filtered_image = imgaussfilt(imageCell{i},sigma);
imageCell{i+1} = imresize(filtered_image, 0.5);
end
end
Personally I dislike using multiple figures, instead preferring subplots. I also avoid using i and j as loop variables to avoid any possible confusion with the imaginary unit. One much more important thing is that you are using image as a variable name, which shadows the low-level function that imshow uses.
So my suggestion would be something like this:
%% Part 3
%Write function
%Iterate Gaussian filtering and then subsampling 5 times for Gaussian Pyramid
%image_path = 'C:\Users\User\Desktop\ILGAZ\2019 Guz\cmpe 565\SecondHomework\woman.jpg';
image_path ='pears.png';%RGB demo image
pyramid=gaussPyramid(im);
function imageCell=gaussPyramid(im)
sigma = 1.5;
numofSubsmp = 5;
imageCell = cell(1,numofSubsmp+1);
imageCell{1} = im;
%determine number of rows and cols for subplot (you can make this as
%complicated or simple as you want, as long as the product of the two is
%larger than the number of images that will be plotted)
sub.r= max(1,round( (3/4)*sqrt(numofSubsmp+1) ));
sub.c= max(1,ceil( (numofSubsmp+1)/sub.r ));
figure(1),clf(1)
for subsample = 1: (numofSubsmp+1)
im = imageCell{subsample};
subplot(sub.r,sub.c,subsample)
imshow(im)
title(sprintf('Image %d',subsample))
filtered_image = imgaussfilt(imageCell{subsample},sigma);
imageCell{subsample+1} = imresize(filtered_image, 0.5);
end
end

Rik

### Rik (view profile)

on 27 Oct 2019
Each function has its own workspace. Inside the laplPyramid function the variable im does not exist. If you want it to exist, the easiest solution is to pass it as an input argument.
Ilgaz Eyidogan

### Ilgaz Eyidogan (view profile)

on 27 Oct 2019
Yes but what I want to do is to call gaussPyramid into laplPyramid because laplPyramid is constructed by using gaussPyramid. Additionally, when I put im to laplPyramid as an input argument, it gives an error which says not enough input argument.
%% Part 3
%Write function
%Iterate the loop six times for Gaussian Pyramid
%Gaussian pyramid is built by first gaussian filtering and then subsampling
%the image for each iteration of the loop
image_path = 'C:\Users\User\Desktop\ILGAZ\2019 Guz\cmpe 565\SecondHomework\woman.jpg';
numofSubsmp = 5;
gaussPyramid(im,numofSubsmp);
laplPyramid(numofSubsmp);
function imageCell = gaussPyramid(im,numofSubsmp)
imageCell = cell(1,numofSubsmp);
imageCell{1} = im;
sigma = 1.5;
for ind = 1:numofSubsmp
figure(ind), imshow(imageCell{ind})
title('Gaussian Pyramid')
filtered_image = imgaussfilt(imageCell{ind},sigma);
imageCell{ind+1} = imresize(filtered_image, 0.5);
end
end
function pyramid = laplPyramid(im,numofSubsmp)
laplCell = cell(1,numofSubsmp-1);
imageCell = gaussPyramid(im,numofSubsmp);
it = 6;
for num = 1:(numofSubsmp-1)
laplCell{num} = imageCell{num} - imresize(imageCell{num+1},2);
figure(it), imshow(laplCell{num})
title('Laplacian Pyramid')
it = it + 1;
end
end
And the error is as follows:
>> Part3i
Not enough input arguments.
Error in Part3i>laplPyramid (line 26)
laplCell = cell(1,numofSubsmp-1);
Error in Part3i (line 11)
laplPyramid(numofSubsmp);
Rik

### Rik (view profile)

on 27 Oct 2019
That's because at the top when you are actually calling this function, you still have the old syntax. When in doubt, use breakpoints and go through your code step by step to find out where the error is.