Image processing: when to use script, when to use function

2 views (last 30 days)
Hello, I am fairly new to matlab. I've just completed my first code. My instructors have told me to convert my script.m file into a function file. I hope this isn't a noob question but...Firstly, I'm confused why it matters. I've read the difference between scripts and functions, but I'm having trouble understanding the difference practically. Secondly, I'm not sure how to convert this into a function. For this code, I provide an image, I select a few points, and the image is rotated. I have several other scripts (to free hand crop, to paste images together, and to plot fluorescence). I'm not sure if these should be converted to functions as well...any help here would be very helpful thanks!
  1 Comment
Jane
Jane on 18 Dec 2013
% %%ROTATE LEG SEGMENT
clc; % Clear command window.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
filename = input('Enter image file name: ', 's');
legsegment = input('Enter leg segment (Coxa, Femur, Tibia, Tarsus): ', 's');
%
% Convert TIFF to PNG
a = imread(filename);
newfilename=strrep(filename, 'tif', 'png');
imwrite(a , newfilename);
%
% Show cropped image
I1=imread(newfilename);
imshow(I1);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Select points to register
message = sprintf('Click Data Cursor and select points while holding alt.\nRight click and Export data to workspace. Click OKAY and Hit ENTER when finished');
uiwait(msgbox(message));
%
% Hit ENTER to continue.
pause
p1=cursor_info(1,2).Position;
p2=cursor_info(1,1).Position;
p11=[1000,4608];
p22=[6000,4608];
[imR imC] = size(I1);
%
% The image points are in "image coordinates, with (1,1) at top left.
% Flip these into cartesion form, with (0,0) in the bottom left.
paddy=@(p)[p(1),imR-p(2)];
p1 = paddy(p1);
p2 = paddy(p2);
p11 = paddy(p11);
p22 = paddy(p22);
%
% Create our objects, from real and imaginary axis points. p1(1,1) is x,
% p1(1,2) is y of the cartesian. This forms complex points, c1 which is x+yi.
% These are the original points
c1 = complex(p1(1,1),p1(1,2));
c2 = complex(p2(1,1),p2(1,2));
% These are the rotated points
c11 = complex(p11(1,1),p11(1,2));
c22 = complex(p22(1,1),p22(1,2));
%
% Create the "A" matrix
A = zeros(4,4);
A(1,1) = real(c1);
A(1,2) = -imag(c1);
A(2,1) = real(c2);
A(2,2) = -imag(c2);
A(1,3) = 1;
A(2,3) = 1;
A(3,1) = imag(c1);
A(3,2) = real(c1);
A(4,1) = imag(c2);
A(4,2) = real(c2);
A(3,4) = 1;
A(4,4) = 1;
%
% Set up the b matrix
b = zeros(4,1);
b(1,1) = real(c11);
b(2,1) = real(c22);
b(3,1) = imag(c11);
b(4,1) = imag(c22);
%
% Solve for X =
x = A\b;
% the x vector now has r, and t...
r = complex(x(1,1),x(2,1));
% Convert to degrees
R=angle(r);
angleDegrees=R*180/pi;
%
% Rotate our leg segment
I3 = imrotate(I1,angleDegrees,'bilinear','crop');
figure;
% Display
subplot(1,2,1);
imshow(I1);
subplot(1,2,2);
imshow(I3);
%
% change to correct file name
rotnewfilename = strcat('rot', newfilename);
imwrite(I3, rotnewfilename);
end

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 18 Dec 2013
To underline what Image Analyst wrote: You will want to call parts of your scripts over and over again. In your script I can see at least a couple of small building-blocks that you ought to functionalize: Converting tiff-image to png, cropping the image, and transforming the image.
Another reason to write functions is that with scripts you have access to all the variables in the workspace, it is very easy to build scripts that depend on existing variables (produced by other scripts) - this might be less-than-ideally-reproducible, if you write functions and call them the input to them become more explicitly obvious and dependencies easier to catch.
  3 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 19 Dec 2013
Edited: Bjorn Gustavsson on 19 Dec 2013
Firstly you can't (as far as the matlab versions I've been through) have functions in a script-file - so you need to create separate files for each function. So from your example script I'd cut out the section where you convert a image-file from one format to another and put them into a file my_convert_image.m with this content:
function OK = my_convert_image(filename)
OK = 0;
a = imread(filename);
newfilename=strrep(filename, 'tif', 'png');
imwrite(a , newfilename);
OK = 1;
That will do it for a first version of an image conversion function. Later you'll expand this with a few checks on the input parameter, add something for making different output formats, using a better way of changing the filename extension with the fileparts function, possibly image compression parameter, then you might fix my nonsensical assignment of OK with a try-catch block and whatnot.
HTH
Image Analyst
Image Analyst on 19 Dec 2013
Edited: Image Analyst on 19 Dec 2013
You can have multiple functions in an m-file as long as the first chunk of code in the file is a function (starts with the "function" keyword) and not a script. You can't start off with a script and follow it with functions in the same file, so just make the first lines of code a function. It doesn't need to take any arguments, you can just have
function test()
% code for test
function output1 = sub1()
% code for sub1
function output2 = sub2(input1, input2, input3)
% code for sub2()
all in the same test.m m-file if you want.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 18 Dec 2013
Convert scripts to a function when you want to call them over and over again with different parameters, such as filename, window width, or whatever. You can even make a chunk of your code into a function if you plan on calling it only once but you just want to simplify your code into a few main concepts. So you can call a just a few functions in your main routine, and the functions are essentially black boxes where a lot of the heavy lifting occurs, but whose code does not clutter up your main routine.

Walter Roberson
Walter Roberson on 18 Dec 2013
In addition to what Image Analyst wrote:
When you convert to a function you should likely get rid of the "clc", "close all", "imtool close all", and "workspace" commands, as whatever routine that is calling your function might have need of something you are turning off with those.
In your code you assign two values to variables using input(). Likely those two values should become parameters to the function, so that the calling routine can determine appropriate values in any way it sees fit (such as through a real GUI.)
One reason to use a function that Image Analyst did not happen to mention, is that MATLAB is able to execute functions more efficiently than it can execute scripts. The difference in execution time between the two was reduced a fair bit just a couple of releases ago, but functions are still expected to be more efficient.
Also, when you are in the editor, MATLAB can do better error analysis for functions than for scripts.

Categories

Find more on Scripts in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!