sequence of image processing using the same background

Hi everybody I have a batch of images. For each image, I am using the same background image. How can I analyze all images using the same background image? When I read the one image, I will have to change the name of the background image as well, but I can not manage to change the background image name for each image that I need to analyze. I would also appreciate if you give me some info, suggestion regarding analyzing the batch of images in the same code?
Thanks!!!

 Accepted Answer

Inside the loop, create the new background image filename and read it in with imread(). Then read in the test image and use both of them to do your analysis.

11 Comments

Thanks for the reply. I have been trying to sort it out, but I could not. I have images and I can import them in the matlab, but I don't have mat1.mat or file1.txt? What are those? Do I need to just read the images then, and delete the other codes? Inside the loop, do I need to read the background image (it is the same for other images that I wanna process the algorithm) as in reading batch of images?
Did you see both code samples? Or just the first one? What are your files named? Any pattern you want, or just analyze all of them with a particular extension?
Yes I have seen. Let's say I have 10 images such as image1.png,image2.png....image10.png . and I have only one background image which is backgroundimage.png. How can I implement this into my code? Please tell me If I am not clear, as I am new in Matlab.
read the background image before the loop
backgroundImage = imread(.........
fileList = dir(fullfile(yourFolder, '*.png'));
for k = 1 : length(fileList)
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
thisImage = imread(thisFileName);
% Do something with thisImage and backgroundImage....
end
I tried to read the background image first but still, I could not make it work. I sent my initial code. What would you suggest based on the code I sent to read a batch of images by using just one background image.
You read in the very same file name for both the RGB image and the background image. Plus you did not have a loop over all images. Please answer these 3 questions:
  1. What is the name of the background image you want to use with every image?
  2. What is the file pattern for the RGB images you want to batch process? For example *.png or whatever...
  3. How do you want to use the background image with the RGB images? For example, divide them, subtract them, or what???
  1. Let's say the name of the background image is example_background.png which will be used with every image.
  2. PNG
  3. Divide them
Try this:
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
backgroundFullFileName = fullfile(myFolder, 'example_background.png');
if ~exist(backgroundFullFileName, 'file')
errorMessage = sprintf('Error: Background file does not exist:\n%s', backgroundFullFileName);
uiwait(errordlg(errorMessage));
else
backgroundImage = imread(backgroundFullFileName);
[rowsB, columnsB, numberOfColorChannelsB] = size(backgroundImage);
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.PNG'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
thisImage = imread(fullFileName);
imshow(thisImage); % Display image.
[rows, columns, numberOfColorChannels] = size(backgroundImage);
drawnow; % Force display to update immediately.
if rows ~= rowsB || columns ~= columnsB
thisImage = imresize(thisImage, size(backgroundImage));
end
correctedImage = double(thisImage) ./ double(backgroundImage);
imshow(correctedImage);
drawnow;
% Now do whatever you want with correctedImage, such as image analysis.
end
Thank you very much. I was able to read all images including my background image. However, When I run the programme, correctedImage was full of white. I checked its pixel, it consist of just pixel 1, which is totaly white. What would be the problem? I could get a good background-corrected image.
Unfortunately you forgot to include your code, and I'm heading off to the airport in a couple of hours. Try this:
imshow(correctedImage, []);
It's possible that your background image is darker than your test images so the ratio is always greater than one so you need the empty brackets in imshow().

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!