How do I perform image difference Between 2 images and extract difference ??

4 views (last 30 days)
Hello, I will like to use apply the code found https://www.mathworks.com/matlabcentral/answers/276217-write-an-image-name-to-particular-folder-using-imwrite to perform image difference between 2 Grayscale images. First to read images from it's folders, resize them and lastly write results with it's real name to a specific folder. I tried the code below but when i imshow difference_image, the results are different (Dark) from the usual results (Gray) I get using codes from https://www.mathworks.com/matlabcentral/answers/259045-difference-between-2-image-and-exctrat-difference. Please find attached. Can anyone explain the reasons ?
%% Showing difference of image
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd; % or 'C:\Program Files\MATLAB' or wherever...
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName1 = fullfile(startingFolder, '*.*');
[baseFileName1, folder1] = uigetfile(defaultFileName1, 'Select Bicubic file');
defaultFileName2 = fullfile(startingFolder, '*.*');
[baseFileName2, folder2] = uigetfile(defaultFileName2, 'Select Other method file');
if baseFileName1 == 0
% User clicked the Cancel button.
return;
end
fullSourceFileName1 = fullfile(folder1, baseFileName1)
if baseFileName2 == 0
% User clicked the Cancel button.
return;
end
fullSourceFileName2 = fullfile(folder2, baseFileName2)
% Read images to be differenced from source file names
firstImage = imread(fullSourceFileName1);
secondImage = imread(fullSourceFileName2);
% Resize images
firstImageresize = (imresize(firstImage, [504, 504])); % Bicubic Method
secondImageresize = (imresize(secondImage, [504, 504])); % Methods to compare
% Image difference
difference_image = double(secondImageresize) - double(firstImageresize);
imshow(difference_image)
% Saving results
% Create destination filename
destinationFolder = 'C:/Set5 [X2]';
if ~exist(destinationFolder, 'dir')
mkdir(destinationFolder);
end
% Strip off extenstion from input file
[sourceFolder, baseFileNameNoExtenstion, ext] = fileparts(fullSourceFileName1);
% Create jpeg filename. Don't use jpeg format for image analysis!
outputBaseName = [baseFileNameNoExtenstion, '.JPG']
fullDestinationFileName = fullfile(destinationFolder, outputBaseName);
% Write the jpg file. This will convert whatever format you started with to the hated jpg format.
imwrite(difference_image, fullDestinationFileName);
Thanks

Accepted Answer

Image Analyst
Image Analyst on 18 Nov 2018
To display properly, do this
imshow(difference_image, [])
DO NOT save as a jpg file! Save with the extension PNG.
outputBaseName = [baseFileNameNoExtenstion, '.PNG']
To save as a standard image, first convert to uint8
uint8Image = uint8(255 * mat2gray(difference_image));
imwrite(uint8Image, fullDestinationFileName
or else leave as double and save in a .mat file
  3 Comments
Image Analyst
Image Analyst on 19 Nov 2018
You could save in BMP - it's also a lossless way because it's uncompressed. But it atkes up more space and CPUs these days are much faster than disk IO so it's faster to compress and save than to save uncompressed. In addition, I think BMP is primarily a Windows format. PNG is pretty much a de facto standard now.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!