How to save pixel values from set of images and store them in ASCII format?

2 views (last 30 days)
I have set of TIFF images, each corresponding to particular wavelength. For example, first image corresponds to 225.197nm, while the next one corresponds to 225.198nm. This way I have images that corresponds to about 100 different wavelengths. For a particular pixel in each of those images, I would like to extract the pixel values and put them like the attached file in two different columns. Any help will be greatly appreciated.
  2 Comments
Walter Roberson
Walter Roberson on 24 Jan 2018
Do I understand correctly that if your image was (for example) 512 pixels by 768 pixels, that you would want (512*768) = 393216 different files created, one for each pixel position, each file containing the information for all of the wavelengths at that position ?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 24 Jan 2018
Edited: Image Analyst on 24 Jan 2018
Use the FAQ to create a 3-D image, if there are not too many of them. Let's say you have 50 images, each with a different wavelength, then you'd do
image3d = zeros(rows, columns, numImages);
for k = 1 : numImages
thisImage = imread(...........
image3d(:,:,k) = thisImage;
end
Then scan the image, writing out each spectrum. You should have an array that says what the wavelength is for each image.
[rows, columns, numImages] = size(image3d);
count = 1;
for row = 1 : rows
for col = 1 : columns
thisSpectrum = image3d(row, col, :);
filename = sprintf('Spectrum %d.txt', count);
fileID = fopen(filename, 'wt');
fprintf(fileID, '%f, %f\n', wavelengths, thisSpectrum);
fclose(fileID);
end
end
  10 Comments
Syed Mashruk
Syed Mashruk on 26 Jan 2018
Edited: Syed Mashruk on 26 Jan 2018
I am still not getting my text file in the desired following form:
44406 216
44406.2 216
44406.4 215
44406.6 215
44406.8 214
Instead getting in the following form:
44406 44406.2
44406.4 44406.6
44406.8 216
216 215
215 214
I tried following code:
fprintf(fileID, '%f %f\n', wavenumbers(count), thisSpectrum);
And I am getting following message:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Any suggestions?
Syed Mashruk
Syed Mashruk on 26 Jan 2018
solved it with the following code:
fprintf(fileID, '%f %f\n', [wavenumbers(:), thisSpectrum(:)].');
Many thanks for your help.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 24 Jan 2018
Read the files in and store into a 3D array, with the third dimension increasing with wavelength. Once you have everything read in, loop over the pixels, fopen() an appropriate file name, fprintf() the wavelengths and a 3D column extracted from the array.
  2 Comments
Syed Mashruk
Syed Mashruk on 24 Jan 2018
Thanks Walter but when I am trying to use fopen() like the following code:
[listOfFiles, folder] = uigetfile ('*.tif', 'Select your image', 'MultiSelect', 'on');
for j=1:length(listOfFiles)
fullFileName = fullfile(folder, listOfFiles{j});
fileID=fopen(fullFileName);
end
I selected 500 tiff files and I am getting the following message:
Too many open files. Close files to prevent MATLAB instability.
Caused by: Message Catalog MATLAB:services was not loaded from the file. Please check file location, format or contents
Is there any way around it?
Walter Roberson
Walter Roberson on 24 Jan 2018
Do not fopen() the tif files yourself. Use imread(), or use the Tiff class (and make sure you clear the tiff object when you are done with it.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!