Reading image save as matrix and store filename information

조회 수: 1 (최근 30일)
Lizan
Lizan 2014년 9월 15일
댓글: Image Analyst 2014년 9월 15일
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?
  댓글 수: 2
Guillaume
Guillaume 2014년 9월 15일
Please start another question for this. (so that it can be searched and so that whoever answers it gets the credit for it)

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by