How to store an image into cell arrays in a loop?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!!
Thanks in advance!
%% 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';
im = im2double(imread(image_path));
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
1 comentario
Rik
el 27 de Oct. de 2019
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Respuestas (1)
Rik
el 27 de Oct. de 2019
Editada: Rik
el 27 de Oct. de 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
im = im2double(imread(image_path));
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
im = im2double(imread(image_path));
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
6 comentarios
Rik
el 27 de Oct. de 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.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!