How do I make an image name contain another image's name?
3 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Image Analyst
on 18 Nov 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 Comments
Stephen23
on 2 Dec 2014
Edited: Stephen23
on 2 Dec 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;
Stephen23
on 2 Dec 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
0 Comments
See Also
Categories
Find more on Convert Image Type 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!