Reading image save as matrix and store filename information

5 views (last 30 days)
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
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?
Lizan
Lizan on 15 Sep 2014
Edited: Lizan on 15 Sep 2014
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

Image Analyst
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
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
Lizan
Lizan on 15 Sep 2014
Yes, that is the possible patterns for the filenames that I give the images to be uploaded.

Sign in to comment.

More Answers (2)

Guillaume
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).

Lizan
Lizan on 15 Sep 2014
Edited: Lizan on 15 Sep 2014
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?

Community Treasure Hunt

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

Start Hunting!