Error message with imwrite and tiffread2 (fopen and fread)

4 views (last 30 days)
I have a big piece of code using the function “tiffread2” to read tiff files (this is the function created by francois nedelec and available in the matlab forum). The image is then converted to a matrix, some operations are performed on it and a new tiff file is created from that. The problem is that then the function tiffread2 tries to open this new file and doesn’t manage to, even if Microsoft Photo Viewer can open the tiff. My colleagues who are using the 2019 version of Matlab have no problem with the code, while I’m using the 2020a version.
Here are the pieces of the main code with the problem:
%% open tiff image file
clear all
addpath('D:\Particle tracking\matlab\tracking code 2')
directory='D:\Particle tracking\test';
fileName='QD655-beta2ar';
file=strcat(directory,'\',fileName);
[image,nImage]=tiffread2(file);
%% convert the image to a matrix
I=zeros(image(1).height,image(1).width,nImage);
tI=1;
for t=1:nImage
I(:,:,tI)=double(image(t).data);
tI=tI+1;
end
%% make new tiff file
imwrite(uint16(I(:,:,1)),strcat(directory,'\Tifftest.tif'),'Compression','none');
if nImage>1
for t = 2:nImage
imwrite(uint16(I(:,:,t)),strcat(directory,'\Tifftest.tif'),'Compression','none','WriteMode','append');
end
end
%% read the new tiff file
tiffread2('D:\Particle tracking\test\Tifftest')
Here is the first part of the code defining the tiffread2 function (I stopped at the part giving the error message):
function [stack, img_read] = tiffread2(filename, img_first, img_last)
% tiffread, version 2.4
%Optimization: join adjacent TIF strips: this results in faster reads
consolidateStrips = 1;
%if there is no argument, we ask the user to choose a file:
if (nargin == 0)
filename, pathname] = uigetfile('*.tif;*.stk;*.lsm', 'select image file');
filename = [ pathname, filename ];
end
if (nargin<=1); img_first = 1; img_last = 10000; end
if (nargin==2); img_last = img_first; end
global TIF;
TIF = [];
%counters for the number of images read and skipped
img_skip = 0;
img_read = 0;
% set defaults values :
TIF.SampleFormat = 1;
TIF.SamplesPerPixel = 1;
TIF.BOS = 'l'; %byte order string
if isempty(findstr(filename,'.'))
filename = [filename,'.tif'];
end
TIF.file = fopen(filename,'r','l');
if TIF.file == -1
filename = strrep(filename, '.tif', '.stk');
TIF.file = fopen(filename,'r','l');
if TIF.file == -1
error(['file <',filename,'> not found.']);
end
end
% read header
% read byte order: II = little endian, MM = big endian
byte_order = fread(TIF.file, 2, '*char');
if ( strcmp(byte_order', 'II') )
TIF.BOS = 'l'; %normal PC format
elseif ( strcmp(byte_order','MM') )
TIF.BOS = 'b';
else
error('This is not a TIFF file (no MM or II).');
end
And here is the error message:
Error using tiffread2 (line 108)
This is not a TIFF file (no MM or II).
Error in tifftest (line 24)
tiffread2('D:\Particle tracking\test\Tifftest')

Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!