Reading image save as matrix and store filename information

3 次查看(过去 30 天)
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 个评论
Guillaume
Guillaume 2014-9-15
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 2014-9-15
编辑:Lizan 2014-9-15
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?

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-9-15
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 个评论
Guillaume
Guillaume 2014-9-15
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 2014-9-15
Yes, that is the possible patterns for the filenames that I give the images to be uploaded.

请先登录,再进行评论。

更多回答(2 个)

Guillaume
Guillaume 2014-9-15
...
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 2014-9-15
编辑:Lizan 2014-9-15
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!

Translated by