MATLAB Answers

Background Subtraction In Images:

2 views (last 30 days)
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 Comment

abhimanyu dubey
abhimanyu dubey on 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.

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 23 Nov 2020
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 Comments

Show 1 older comment
Bjorn Gustavsson
Bjorn Gustavsson on 23 Nov 2020
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
abhimanyu dubey
abhimanyu dubey on 23 Nov 2020
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!