Best practise for quickly changing parameters of a function?
23 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
BC
el 26 de Feb. de 2021
Comentada: BC
el 26 de Feb. de 2021
Let's say I have the below function (just as an example), which segments objects in an image. I'm testing it on different images. Each time I run it I might want to adjust parameters, e.g. in this case, the numbers 0.38, 800, and 32, to see if it now works better with my images.
function burnedAnt = createAntSegmentation(ant)
greyant = rgb2gray(ant);
adaptedAnt = adaptthresh(greyant, 0.38,"ForegroundPolarity","dark"); % threshold image.
BW = imbinarize(greyant,adaptedAnt); % binarize ant image
BWopen = bwareaopen(~BW,800); % exclude pixels smaller than X
se = strel("disk",32); % create shape used for dilating image
BWdilate = imdilate(BWopen,se); % dilate image
BWfilled = imfill(BWdilate,"holes"); % fill in any holes
burnedAnt = imoverlay(ant,~BWfilled,"k"); %produce final image
end
Instead of scrolling through the code and manually changing each value, what is the best way / reccomended way to quickly do this? I was thinking of having several variables at the top of my script, and then calling these variables in the function. This intuitively seems good to me as it's visually easy to see what they are.
% adjust these values before running function
adaptedThreshValue = 0.38
BWopenValue = 800
seDiskSize = 32
Or is it better to have the values all in one matrix, like this, and index them in the function instead?
functionValues = [0.38, 800, 32]
% 1 = adapted threshold value
% 2 = BWopen value
% 3 = se disk size value
In this way, when I've got a couple of functions in one script, I figure it'll make things much quicker to change, and I can copy + paste the parameters that I ran before, and save them as text in several lines to keep track of what I've been trying out. But, I've only been working on Matlab for a few weeks so thought I would see what everyone reccomends. Thanks!
2 comentarios
Mathieu NOE
el 26 de Feb. de 2021
hello
IMO, the best way is to create a structure of parameters and pass this structure as an argument in your function
so the main script starts with the structure initialization :
param. threshold_value = 0.38
param. BWopen_value = 800
param. disk_size_value = 32
when you call your function, this is how to pass the arguments :
function burnedAnt = createAntSegmentation(ant,param)
and inside the function you can retrieve the individual parameters :
threshold_value = param.threshold_value;
BWopen_value = param.BWopen_value;
disk_size_value = param.disk_size_value;
and use those parameters in your code
the beauty with structures is that you can add as many parameters you want without having to modify how to pass the param structure to the functions
Respuesta aceptada
Image Analyst
el 26 de Feb. de 2021
Try this:
functionValues = [0.38, 800, 32;
0.5, 700, 64;
0.4, 750, 16] % Whatever values you want
% Loop through all sets of values passing them to the segmentation function.
for k = 1 : size(functionValues, 1)
threshold = functionValues(k, 1);
minBlobSize = functionValues(k, 2);
radius = functionValues(k, 3);
burnedAnt = createAntSegmentation(ant, threshold, minBlobSize, radius);
% Now do something with burnedAnt, like display it, save it, call regionprops, or whatever...
end
%===============================================================================
function burnedAnt = createAntSegmentation(ant, threshold, minBlobSize, radius)
% Convert to gray scale if necessary. Calling rgb2gray() on a grayscale image will throw an error. Needs to be a color image.
if ndims(ant) == 3
greyant = rgb2gray(ant);
else
greyand = ant;
end
adaptedAnt = adaptthresh(greyant, threshold, "ForegroundPolarity", "dark"); % threshold image.
BW = imbinarize(greyant,adaptedAnt); % binarize ant image
BWopen = bwareaopen(~BW, minBlobSize); % exclude pixels smaller than X
se = strel("disk", radius); % create shape used for dilating image
BWdilate = imdilate(BWopen, se); % dilate image
BWfilled = imfill(BWdilate, "holes"); % fill in any holes
burnedAnt = imoverlay(ant, ~BWfilled, "k"); %produce final image
end
Más respuestas (1)
Simon Allosserie
el 26 de Feb. de 2021
For each number you want to change regularly, make it also an input parameter to your function.
Then, you can write a little script to go through all the options, by listing the different options per parameter in a list or matrix, and then looping through the matrix to execute the function over and over again using all the different input combinations.
2 comentarios
Simon Allosserie
el 26 de Feb. de 2021
You're welcome :) Please vote my answer as the correct one to close this question. Thanks!
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!