fprintf function for structuring element

I'm working on a GUI that outputs user commands to a script file, and I've run into an issue programming the structuring element for performing morphological operations. As it stands, I construct the structuring element by allowing the user to choose the shape and the values for radius, length, or whatever, so the structuring element looks something like:
se = strel('disk',5);
However, I can't output that statement to the script using fprintf because it's a mix of character values and numerical values. So, how can this be done?

 Respuesta aceptada

Cedric
Cedric el 18 de Mzo. de 2013
Editada: Cedric el 18 de Mzo. de 2013
fid = fopen ('script.txt', 'a') ;
disk = 5 ; % Defined form user input.
fprintf(fid, 'se = strel(''disk'', %g);\n', disk) ;
square = 7 ; % Defined form user input.
fprintf(fid, 'se = strel(''square'', %g);\n', square) ;
% You could also have the shape given as a string from some drop-down and
% use it as in ..
shape = 'triangle' ; % Defined from drop-down.
value = 5 ;
fprintf(fid, 'se = strel(''%s'', %g);\n', shape, value) ;
% ...
fclose(fid) ;

9 comentarios

I'm looking for some way to input the structuring element all together. The way my code is written, the user selects the shape from a button group and the size from sliders. That is then placed in the se() variable. However, I can't use that se() in the fprintf function.
fprintf(fid,'img = imdilate(img, %g); \n', se)
gives an error because you can't output a structuring element like this; is there another way to do it that is less labor intensive than what you've proposed?
Cedric
Cedric el 18 de Mzo. de 2013
But it is you who create the STREL from some UI call back, no? At the moment of its creation, you could also build the string as mentioned above. Or do you receive directly a STREL object from an UI that you cannot modify/understand?
The structuring element is created once the user sets its parameters. I then use those parameters and apply that operation to the image. The strel() function allows for multiple numeric inputs, so I can't simply break the strel command into its constituent shape and value parts and do:
fprintf(fid, 'se = strel(''%s'', %g);\n, shape, value)
in the case that I have more than one value to gather for a particular shape.
Cedric
Cedric el 18 de Mzo. de 2013
Editada: Cedric el 18 de Mzo. de 2013
The syntax is
SE = strel(shape, parameters) ;
with a variable number of parameters ranging from 1 to 3 (for 'ball').
There is a point in your code where you make this call, and then you translate the strel or you perform a series of other operations. At this point, you get the shape and relevant parameters from the interface.
I don't understand what prevents you from building a string which corresponds to the relevant call at this moment; you have probably already a switch whose cases select the right type of call. The only tricky situation is if you have a mechanism in the interface for managing 'arbitrary' shapes; in such case, you would have to store the array as well.
Cedric
Cedric el 18 de Mzo. de 2013
Editada: Cedric el 18 de Mzo. de 2013
I had a look at your former question here. Note that you could build methods for managing logs, e.g.
properties
log_fid = -1
...
end
methods
function self = logOpen(self, fileLocator)
self.log_fid = fopen(fileLocator, 'w') ;
end
function logClose()
fclose(self.log_fid) ;
end
function log(self, fmt, varargin)
if self.log_fid > 0
fprintf(self.log_fid, fmt, varargin{:}) ;
end
% or throw an error/warning
end
function SE = strel(self, shape, varargin)
if self.log_fid > 0
self.log('%%Build STREL (shape=%s)\n', shape) ;
cmdStr = ['SE = strel(''', shape, '''', ...
sprintf(', %g', varargin{:}), ')\n'] ;
self.log(cmdStr) ;
end
SE = strel(shape, varargin{:}) ;
end
end
I am mentioning this to illustrate the use of varargin read as a comma -separated list.
EDIT: added self.strel() so you see how you can have a STREL function adding automatically an entry into the logs.
Image Analyst
Image Analyst el 18 de Mzo. de 2013
Is the issue that you don't know how to print the apostrophe to the text file?
The issue is that the following command doesn't work
se = strel('disk',5);
fprintf(fid, 'img = imdilate(img, %s); \n, se);
And I guess that's due partly to the syntax being incorrect because 'disk' needs double quotes around it in the fprintf() function. I was just trying to find a simple way to maybe decompose the structuring element to separate shape and parameter in order to rebuild the fprintf() command correctly.
Cedric
Cedric el 19 de Mzo. de 2013
Editada: Cedric el 19 de Mzo. de 2013
Actually if you perform
se = strel('disk',5);
img = imdilate(img, se);
what you want to do in order to save these "commands" to file is:
fprintf(fid, 'se = strel(''disk'',5);\n') ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
If you had the shape defined by some control in your GUI and saved in a string variable named shape, you could do the following:
fprintf(fid, 'se = strel(''%s'',5);\n', shape) ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
In any case, you want the string 'se' to be in the format of FPRINTF, and not the output of disp(se), as it is the se that will be created when the saved script will be run that must be passed to IMDILATE, and not "some representation" of the current se.
Caleb
Caleb el 19 de Mzo. de 2013
That will probably work. Good Call.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 18 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by