Reading image save as matrix and store filename information

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

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?
I have some X number of images. These images I want to read in matlab and save the pixel map (intensity counts (1 to 255)) into a matrix for each image.
I guess I would have to upload the image matrix into an array?

Sign in to comment.

 Accepted Answer

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

Thank you, this solves the reading the filename and saving the data into variables I need.
How can I save all images as matrix in array?
dir does work. You probably need to use an absolute path:
urlImage = 'C:/Users/me/Desktop/folder/';
for file = dir(fullfile(ulrImage, '*.jpg'))'
img = imread(file.name);
...
end
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
Yes, that is the possible patterns for the filenames that I give the images to be uploaded.

Sign in to comment.

More Answers (2)

...
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).
Follow-up question 2:
I am not to familiar with arrays or cells in MATLAB. I would like to make an array that contains in one cell;
Image Matrix (Pixel value (size x size) matrix of image) - M
string - imageType
double value - pos
double value - exposure
How do I do this?

2 Comments

Please start another question for this. (so that it can be searched and so that whoever answers it gets the credit for it)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!