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

3 visualizaciones (últimos 30 días)
Hello, I will like to use apply the code found 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 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;
% 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.
fullSourceFileName1 = fullfile(folder1, baseFileName1)
if baseFileName2 == 0
% User clicked the Cancel button.
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);
% Saving results
% Create destination filename
destinationFolder = 'C:/Set5 [X2]';
if ~exist(destinationFolder, 'dir')
% 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);

Respuesta aceptada

Image Analyst
Image Analyst el 18 de Nov. de 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 comentarios

Iniciar sesión para comentar.

Más respuestas (0)


Community Treasure Hunt

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

Start Hunting!

Translated by