Dear all,
I had written a code which was constructing A background image from a given set of images and then subtracting it so as to remove noise. Now it was working perfectly on my macbook, but when I'm trying to run on windows it's giving me an error.
I'm using sprintf to keep the length of the string constant. like img_00001, img_0002, ....., img_00011, ... img00100, .... etc. This insures that images are properly ordered. Can anyone help me out in figuring, what's the error here. Why it is not working on windows when it was working on MACos.
Thanks,
Matlab Code:
%% Creating And Subtracting a Background Image:
clc ; close all ; clear ;
%%
% Get the names of files to be Imported:
% Directory for importing the images:
import_directory = uigetdir('Locate Folder for Input Images') ;
image_extension = '*.tif' ;
direc = dir([import_directory , filesep , image_extension] ) ;
filenames = {} ;
[filenames{1:numel(direc) , 1}] = deal(direc.name) ;
filenames = sortrows(filenames) ;
num_of_images = length(filenames) ;
%% Preallocating Space for Images:
pj = uint16(zeros(size(imread([import_directory , filesep , filenames{1}])))) ;
% Select Directory For Exporting the Images:
export_directory = uigetdir('Locate Folder for Output Images') ;
tic
for i = 1:num_of_images
name2 = sprintf([export_directory , filesep , 'Background_Subtracted_Output_File_%6.6i.tif'] , i) ;
imwrite(pj , name2) ;
end
t(1) = to

1 Kommentar

abhimanyu dubey
abhimanyu dubey am 23 Nov. 2020
The reason I'm trying to run this code on a windows is that, this windows machine is a 128gb workstation. And I've 14 sets of 6000 images, that I need to process. I had used my personal laptop to develop and test the code and then I'm trying to get things done in my lab's workstation.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Bjorn Gustavsson
Bjorn Gustavsson am 23 Nov. 2020

0 Stimmen

Instead of using sprintf to generate the full (or relative, doesn't matter which) path to your images you should use fullfile. That should be a robust way of generating the full filename with the correct filesep, but without creating the error you've run into.
Also note that this is not the error you get stuck on, the error is that imwrite gets confused about the image-format. That might be because of the peculiar filename youve gotten. Exactly what causes that is tricky to check, it should work with a filename with the .tif extension, your filename should have a correct numbering from your '%6.6i', it might be because imwrite for some reason combines "\" and "A" in your path into "\A" and gets distracted?
HTH

4 Kommentare

abhimanyu dubey
abhimanyu dubey am 23 Nov. 2020
The code was actually working fine, ( As you can see in the attached screenshot) and I had tested it on about 3000 Images on my laptop itself.
But does fullfile has this functionality of keeping the length of filename constant.
Like for
  • Image No. 9, filename = im_00009,
  • Image No. 99, filename = im_00099
  • Image No. 9999, filename = im_09999
You get the idea. I had looked into fullfile(), but didn't find this functionality there. Kindly please let me know in case I had missed something or if fullfile() does have this functionality. It's very Imp for me to have constant length of the string, or else it'll create a mess in subsequent stages.
Thanks once again Sir, for taking out time to reply.
fullfile just takes care of generating a sensible full(relative)-pathed filename for the OS. You have to generate the file-name, just as you do:
idx = 27; % as you loop...
img_file_name = sprintf('Background_subtracted_Output_file_%6.6d.tif',idx);
Then you can join that with the directory-names into a full filename with fullfile:
full_filename = fullfile('dir1','dir2','etc',img_file_name);
That should be as robust as you can make it for use on diverse OS-es...
The warning is about \A not being an OK character, then the error is that imwrite cannot determine the image format from the file-name. The formating of the filename looks OK. Another problem might be the white-spaces in the directory-names, that can mess up things, what happens if you rename those directories?
HTH
Yup, it works.
I modified the code as,
for i = 1:num_of_images
name2 = fullfile( export_directory , sprintf(('Background_Subtracted_Output_File_%6.6i.tif') , i) ) ;
imwrite(pj , name2) ;
end
and it worked like a charm. Thanks a lot for being patient enough for me to understand that, and thank you for your time Sir.
Bjorn Gustavsson
Bjorn Gustavsson am 23 Nov. 2020
My pleasure.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by