MATLAB: Background Subtraction In Images:

digital image processingimage processingImage Processing ToolboxMATLABmatlab function

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

Best Answer

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