How to make a function with an image input?

24 visualizaciones (últimos 30 días)
Ivan nagara Putra wahyudi
Ivan nagara Putra wahyudi el 17 de Dic. de 2021
Comentada: Ivan nagara Putra wahyudi el 18 de Dic. de 2021
Hello, I wanted to ask for how can I do a function with an input of an image file.
I had a file called "streakprocess.m", and I wanted to have the function in the first line as [x,y,z] = streakprocess('image file'); and I wish to invoke this function in another file, so in that other file I can just load the image = imread('ivan.png') syntax, and call this streakprocess(image) function. Until now, I haven't find any way to specify the input of streakprocess('input') to an image file. Can anyone help me with this? Please let me know if the question isn't clear enough. Thank you for the help before!

Respuesta aceptada

Voss
Voss el 17 de Dic. de 2021
To create a function called streakprocess that takes a matrix representing an image as input:
function [x,y,z] = streakprocess(image_data)
x = 0;
y = 0;
z = 0;
% your code to calculate x, y and z from the image data goes here
end
And to call this function, you would say:
image = imread('ivan.png'); % read some image data from an image file
[x_ivan,y_ivan,z_ivan] = streakprocess(image); % call streakprocess with this image data and store the results as x_ivan, y_ivan, z_ivan
If instead you want to define the function to take the name of an image file as input and read the image itself:
function [x,y,z] = streakprocess(image_file_name)
image_data = imread(image_file_name);
x = 0;
y = 0;
z = 0;
% your code to calculate x, y and z from the image data goes here
end
And to call it in this case, you say:
[x_ivan,y_ivan,z_ivan] = streakprocess('ivan.png'); % call streakprocess with this image file name and store the results as x_ivan, y_ivan, z_ivan
It's also easy enough to define the function to be able to accept either a matrix of image data or an image file name:
function [x,y,z] = streakprocess(image_data)
if ischar(image_data) % input is a character array. must be a file name
image_data = imread(image_data); % read the image and store the image data
end
x = 0;
y = 0;
z = 0;
% your code to calculate x, y and z from the image data goes here
end
Then to call it you can use either of the previous syntaxes:
% reading the image first and passing the matrix to streakprocess:
image = imread('ivan.png');
[x_ivan,y_ivan,z_ivan] = streakprocess(image);
% passing the file name to streakprocess (and letting streakprocess read the image) does the same thing:
[x_ivan,y_ivan,z_ivan] = streakprocess('ivan.png');
  1 comentario
Ivan nagara Putra wahyudi
Ivan nagara Putra wahyudi el 18 de Dic. de 2021
Thank you so much for the help! It had been a little trouble for me, and now I can proceed for my project. I appreciate it, Sir!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Images en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by