MATLAB Answers

How do I understand the following code?

2 views (last 30 days)
Vinay Chawla
Vinay Chawla on 19 Oct 2020
Commented: Image Analyst on 20 Oct 2020
Hello,
I found this code online and I am trying to understand it for my study. I am unable to understand the following lines of code and I was wondering if someone will assist me in understanding. Specifically, I don't understand the part "%set the image size to suitable value" where it is giving scale and resizing the image and "%% image Resize"
%% Uploading input image
[filename,pathname] = uigetfile('*.*','Choose the input image');
im = imread([pathname,filename]);
% set the image size to suitable value
scale = 600/(max(size(im(:,:,1))));
im = imresize(im,scale*size(im(:,:,1)));
% % Image resize
[m,n,~] = size(im);
I would appreciate the assistance.
Thank you in advance.

  0 Comments

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 19 Oct 2020
Edited: Adam Danz on 19 Oct 2020
The best way to understand it is by using the help or doc commands to look up each function name. At the top of each page you'll see a brief description of the function, inputs and outputs.
Here's a very general description of each line
% CHOOSE A FILE
[filename,pathname] = uigetfile('*.*','Choose the input image');
% READ THE IMAGE FILE IN AS IMAGE DATA
im = imread([pathname,filename]);
% DETERMINE THE SCALE OF THE IMAGE DATA
% (max(size(...)) returns the images maximum dimension
% THE IMAGE DATA (im) IS AN nXmX3 ARRAY OF R-G-B- COLOR VALUES
% BUT THIS LINE ONLY REFERENCES THE "R" VALUES
scale = 600/(max(size(im(:,:,1))));
% SCALE THE IMAGE
im = imresize(im,scale*size(im(:,:,1)));
% GET THE NEW IMAGE SIZE
[m,n,~] = size(im);
You can use imshow(im) to see the image.

  3 Comments

Vinay Chawla
Vinay Chawla on 19 Oct 2020
Thank you. I tried using the help or doc commands for functions and got some of them but not the othres.
The line " scale = 600/(max(size(im(:,:,1)))); " is still very unclear to me.
What is it doing?
Is it creating a scale for the image? or setting it to the actual scale of the image.
as for the " im = imresize(im,scale*size(im(:,:,1)));" Is this resizing the image based on actual scale (Ground Distance).
These lines are part of the code for determining crack dimensions. So I was wondering if the scale and resizing is given based on the actual field dimensions.
I am very new to MATLAB and Image Processing and I would really appreciate if you can provide an easier and detailed explanation on this.
Thank you for your help.
Adam Danz
Adam Danz on 19 Oct 2020
"Is it creating a scale for the image?"
Yes.
This section gets the size of the first two dimensions of the image (width & height)
size(im(:,:,1))
This returns the max (width or height)
(max(size(im(:,:,1)))
This computes the scaling factor. So, if you're working with a tall image (height>width), then this will divide 600 by the height of the image. If height is smaller than 600, the scaling factor will be larger than 1 so the size of the image will increase. I don't know where 600 comes from.
600/(max(size(im(:,:,1))))
Image Analyst
Image Analyst on 20 Oct 2020
It's possible that they want the algorithm to work for any size image but some of the parameters, for example filter window sizes, are built assuming a certain size of the image in pixels. If the image is not that size (say they're 10 times bigger), then the filters might not work as intended - as they did for the smaller images.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!