How do I make an image name contain another image's name?

6 visualizaciones (últimos 30 días)
Danielle
Danielle el 16 de Nov. de 2014
Editada: Stephen23 el 2 de Dic. de 2014
I have
ima1 = 'oranges.png';
ima2 = 'tangerines.png';
but I need to create a new image where the name is 'oranges_vs_tangerines.png'. I am using imwrite, but I don't understand what to put to show the images names. I left it at
imwrite(nim,'<ima1>_vs_<ima2>.png.')
Then I have to insert the answer into my output
out = 'The RBG values are different: see %s<ima1>_vs_<ima2>.png.'
But I'm writing a function so I can't just put %soranges_vs_tangerines.png. How do I call up the name of the file afterwards? Can I store it in a variable?

Respuestas (2)

Image Analyst
Image Analyst el 18 de Nov. de 2014
Try this:
ima1 = 'oranges.png';
ima2 = 'tangerines.png';
[folder1, baseFileName1, ext1] = fileparts(ima1)
[folder2, baseFileName2, ext2] = fileparts(ima2)
outputBaseFileName = sprintf('%s_vs_%s.png', baseFileName1, baseFileName2)
folder = pwd; % Whatever.....
fullOutputFileName = fullfile(folder, outputBaseFileName);
imwrite(nim, fullOutputFileName)
fprintf('The RGB values are different:\nsee %s\n', fullOutputFileName)
  3 comentarios
Image Analyst
Image Analyst el 2 de Dic. de 2014
Do you have any more questions? If not, can you mark it as Accepted?
Stephen23
Stephen23 el 2 de Dic. de 2014
Editada: Stephen23 el 2 de Dic. de 2014
Image Analyst gave some excellent advice, for example:
  • using fileparts . Consider what would happen in your code if the filename contained a period character, or no file extension.
  • using sprintf to join the filename strings together.
  • using fullfile to create a new filename.
You should consider reviewing Image Analyst's code, it really contains some good ideas for your work... and good ways to use MATLAB.
Also note that your code
r = im1(:,:,1) == im2(:,:,1);
g = im1(:,:,2) == im2(:,:,2);
b = im1(:,:,3) == im2(:,:,3);
nim = cat(3, r, g, b);
is simply equivalent to (and could be replaced by):
nim = im1==im2;

Iniciar sesión para comentar.


Stephen23
Stephen23 el 2 de Dic. de 2014
This is the core code that you need:
>> ima1 = 'oranges.png';
>> ima2 = 'tangerines.png';
>> [~,nam1] = fileparts(ima1);
>> [~,nam2] = fileparts(ima2);
>> ima3 = sprintf('%s_vs_%s.png',nam1,nam2)
ima3 =
oranges_vs_tangerines.png

Categorías

Más información sobre Convert Image Type 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!

Translated by