Code covered by the BSD License  

Highlights from
Introduction to MATLAB® Webinar materials

Introduction to MATLAB® Webinar materials

by

 

20 Mar 2007 (Updated )

Presentation and demo materials from the live Webinar "Introduction to MATLAB®" (March 20, 2007)

finddiff(threshold,handles)
function handles = finddiff(threshold,handles)

% Copyright 2007 The MathWorks, Inc.

%% Import images
if nargin==1
  [filename1, pathname1] = uigetfile('*.jpg','Pick first image');
  [filename2, pathname2] = uigetfile('*.jpg','Pick second image');
  handles.filename1 = fullfile(pathname1, filename1);
  handles.filename2 = fullfile(pathname2, filename2);
  handles.DisplayPanel = gcf;
end
    
% filename1 = 'planes1.jpg';
% filename2 = 'planes2.jpg';

% if no files specified, don't do anything
if ~isfield(handles, 'filename1') || ~exist(handles.filename1, 'file') || ...
    ~exist(handles.filename2, 'file')
  return;
end

planes1 = imread(handles.filename1);
planes2 = imread(handles.filename2);


%% Compute the difference and display
D = planes1 - planes2;

%% Threshold
% threshold = 18;
if ndims(D)>2, D = rgb2gray(D);end
Dbw = D > threshold;

%% Remove the noise (pixel groups smaller than 10)
% We will next use an Image Processing Toolbox function to remove all small
% groups of pixels.
Dbw = bwareaopen(Dbw, 10);  % Get rid of regions less than 10 pixels

%% Dilate
% We will also spread the objects to make them more identifyable.
se1 = strel('line',3,0);
se2 = strel('line',3,90);
Dil=imdilate(Dbw,[se1 se2],'full');
Dbw=Dil(2:end-1,2:end-1);

%% Identify objects
% Then we will use another function to identify, count
% and get the positions of the objects in the image.
[L,NumDiffs] = bwlabel(Dbw);
s = regionprops(L);
c = [s.Centroid];

%% Display first image and highlight change areas
% Finally, we will display the first image and highlight where we think
% there was a change.
delete(get(handles.DisplayPanel,'Children'))

handles.axes(1) = axes('Parent',handles.DisplayPanel,'Units','normalized','OuterPosition',[0 0 .5 1]);
imshow(planes1);
[pathname, filename] = fileparts(handles.filename1);
title(filename)
colormap gray
hold on;
for i = 1 : NumDiffs
    h(i)=rectangle('Position',[c(2*i-1)-15 c(2*i)-15  30 30],'Curvature',[1 1],'LineWidth',4);
    set(h(i),'EdgeColor',[1 0 1]);
end
hold off

%% Display second image and confirm changes
% Which we can confirm plotting the second image and see that we have
% correctly detected that three planes have gone.
handles.axes(2) = axes('Parent',handles.DisplayPanel,'Units','normalized','OuterPosition',[0.5 0 .5 1]);
imshow(planes2);
[pathname, filename] = fileparts(handles.filename2);
title(filename)
hold on;
for i = 1 : NumDiffs
    h(i)=rectangle('Position',[c(2*i-1)-15 c(2*i)-15  30 30],'Curvature',[1 1],'LineWidth',4);
    set(h(i),'EdgeColor',[1 0 1]);
end
hold off

%% Title
[junk, f1]= fileparts(handles.filename1);
[junk, f2]= fileparts(handles.filename2);

ts = [num2str(NumDiffs) ' differences found between ' f1  ' and ' f2];
set(handles.TitleText,'String',ts)

set(handles.ThresholdText,'String',num2str(round(threshold)))

Contact us