Reading image save as matrix and store filename information
4 views (last 30 days)
Show older comments
Hi,
I have X number of images I need to upload and obtain the data stored in the file name for later operations. For example a file name would be:
image_TM_pos13p72_exp2p456.jpg
or
image_TE_pos12p56_exp2p800.jpg
I need to upload and save the image as a matrix with pixel intensities and I need to obtain the data from the corresponding image (matrix) the filename TE or TM, position XX.XX and exposure time X.XXX.
How do I do this?
2 Comments
Guillaume
on 15 Sep 2014
What do you mean by 'upload'? From your description it sounds like you just want to open and read some files.
Are you trying to open all the files in a directory, or just some files that follow a specific pattern? If the latter what exactly is the pattern?
Accepted Answer
Image Analyst
on 15 Sep 2014
You need to parse the filename with strfind() and str2double. Try this:
function test
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
[imageType, pos, exposure] = ParseFileName('image_TM_pos13p72_exp2p456.jpg')
[imageType, pos, exposure] = ParseFileName('image_TE_pos12p56_exp2p800.jpg')
function [imageType, pos, exposure] = ParseFileName(filename)
imageType = filename(7:8); % Extract TM or TE
% Parse pos
posLocation = strfind(filename, '_pos') + 4;
posString = filename(posLocation:posLocation + 4);
posString = strrep(posString, 'p', '.'); % Convert p to dot
pos = str2double(posString);
% Parse exposure
expLocation = strfind(filename, '_exp') + 4;
expString = filename(expLocation:expLocation + 4);
expString = strrep(expString, 'p', '.'); % Convert p to dot
exposure = str2double(expString);
4 Comments
Guillaume
on 15 Sep 2014
What are the possible patterns for imagetype, position and exposure?
- Is imagetype just one of: {TM, TE}
- Is position always integer digits + p + integer digits
- Is exposure always integer digits + p + integer digits
More Answers (2)
Guillaume
on 15 Sep 2014
...
jpegFiles = dir(filePattern);
for jpegFile = jpegFile'
baseFileName = jpegFile.name;
parts = regexp(baseFileName, 'image_(..)_pos(\d+p\d+)_exp(\d+p\d+)', 'tokens', 'once')
if isempty(parts)
error('%s does not follow pattern', baseFileName);
else
imagetype = parts{1};
pstring = parts{2}; pstring(pstring == 'p') = '.';
position = str2num(pstring);
%same with exposure
decodes any pattern with image_ followed by any two characters (imagetype) followed by _pos followed by 1 or more digit + p + 1 or more digit (position) followed by _exp followed by 1 or more digit + p + 1 or more digit (exposure).
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!