Code covered by the BSD License  

Highlights from
Demos from Image Processing Webinar 2006

image thumbnail

Demos from Image Processing Webinar 2006

by

 

08 Feb 2006 (Updated )

These demos were used in the Image Webinar recorded on January 19th, 2006

SHAPE(varargin)
% Copyright 2005-2006 The MathWorks, Inc. 
function varargout = SHAPE(varargin)
%#ok
% SHAPE M-file for SHAPE.fig
%      SHAPE, by itself, creates a new SHAPE or raises the existing
%      singleton*.
%
%      H = SHAPE returns the handle to a new SHAPE or the handle to
%      the existing singleton*.
%
%      SHAPE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SHAPE.M with the given input arguments.
%
%      SHAPE('Property','Value',...) creates a new SHAPE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SHAPE_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SHAPE_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help SHAPE

% Last Modified by GUIDE v2.5 08-Feb-2006 16:29:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SHAPE_OpeningFcn, ...
                   'gui_OutputFcn',  @SHAPE_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before SHAPE is made visible.
function SHAPE_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SHAPE (see VARARGIN)

% Choose default command line output for SHAPE
handles.output = hObject;
% Update handles structure
handles.rgb = []; 
handles.noback = [];
guidata(hObject, handles);

% This sets up the video camera and starts the preview
% Only do this when it's invisible
if strcmp(get(hObject,'Visible'),'off')
  try
    handles.vidobj = videoinput('winvideo');
    % Update handles structure
    start(handles.vidobj);
    guidata(hObject, handles);
    vidRes = get(handles.vidobj, 'VideoResolution');
    nBands = get(handles.vidobj, 'NumberOfBands');
    hImage = image(zeros(vidRes(2), vidRes(1), nBands), 'Parent',...
                   handles.PreviewWindow);
    preview(handles.vidobj,hImage);
  catch
    msgbox('No Camera connected. candy.jpg used as default.')
    hImage = image(imread('candy.jpg'), 'Parent', handles.PreviewWindow);   
  end   
end

% UIWAIT makes SHAPE wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function PreviewWindow_CreateFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to PreviewWindow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate PreviewWindow


% --- Executes during object creation, after setting all properties.
function CapturedImageWindow_CreateFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to CapturedImageWindow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate CapturedImageWindow


% --- Executes during object creation, after setting all properties.
function AnalysisResultsTextBox_CreateFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to AnalysisResultsTextBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function ShapeClassification_CreateFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to ShapeClassification (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Outputs from this function are returned to the command line.
function varargout = SHAPE_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in CaptureButton - Capture
function CaptureButton_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to CaptureButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Take a snapshot form the video object
try
    handles.rgb = getsnapshot(handles.vidobj);
catch
    handles.rgb = imread('candy.jpg');  % Use the candy image if a camera
                                        % is not connected.
end
% Update handles structure
guidata(hObject, handles);
image(handles.rgb,'Parent',handles.CapturedImageWindow);
axis off

% --- Executes on button press in AnalyzeButton - Analyze
function AnalyzeButton_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to AnalyzeButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isempty(handles.rgb), return, end

rad = 15; % Estimated radius of objects to exclude from background
lab = applycform(handles.rgb,makecform('srgb2lab'));
I = lab(:,:,1);
% Define and subtract background illumination
background = imclose(I,strel('disk',rad));
handles.noback = imsubtract(background,I);
% Update handles structure
guidata(hObject, handles);
BW = im2bw(handles.noback,graythresh(handles.noback));
fill = imfill(BW,'holes');
analyze(hObject, fill, handles);


% --- Executes on button press in AdjustButton - Adjust
function AdjustButton_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to AdjustButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if isempty(handles.rgb), return, end
if isempty(handles.noback), return, end

level = thresh_tool(handles.noback);
BW = im2bw(handles.noback,level/255);
BW = bwareaopen(BW, 10);  % Get rid of regions less than 10 pixels
fill = imfill(BW,'holes');
analyze(hObject, fill, handles);


% --------------------------------------------------------------------
function File_menu_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to File_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Save_Image_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to Save_Image (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% current captured image
rgb = getimage(handles.CapturedImageWindow);
if isempty(rgb), return, end

%save to file
fileTypes = supportedImageTypes;
[f,p] = uiputfile(fileTypes);
if f==0, return, end

fName = fullfile(p,f);
imwrite(rgb,fName);
disp(['Current image saved to file ' fName]);

%working directory follows user around
if ~strcmp(p,pwd) %path changed
  cd(p);
end


% --------------------------------------------------------------------
function exit_GUI_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to exit_GUI (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
delete(handles.vidobj);
delete(handles.figure1);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
%#ok
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(handles.vidobj);
delete(hObject);


% --- Executes on selection change in AnalysisResultsTextBox.
function AnalysisResultsTextBox_Callback(hObject, eventdata, handles)
%#ok
% hObject    handle to AnalysisResultsTextBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns AnalysisResultsTextBox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from AnalysisResultsTextBox


% --- Subfunction to Analyze the image.
function analyze(hObject, image, handles)

discrep = .5; % Allowed discrepancy from mean area
% Label elements in image
[labeled,numObjects] = bwlabel(image,4);
stats = regionprops(labeled,'Eccentricity','Area','BoundingBox');
% Areas and minimum areas for objects
areas = [stats.Area];
minSize = mean(areas)- discrep * std(areas);
% Use feature analysis to identify broken objects
eccentricities = [stats.Eccentricity];
idxOfDefects = find(areas < minSize & eccentricities > .5);
statsDefects = stats(idxOfDefects);
% Label broken objects
axes(handles.CapturedImageWindow);
imshow(image);
axis off
hold on;
numDefects = length(statsDefects);
for idx = 1 : numDefects
    h = rectangle('Position',statsDefects(idx).BoundingBox,'LineWidth',2);
    set(h,'EdgeColor',[.75 0 0]);
    hold on;
end
hold off;
% Display the results
set(handles.AnalysisResultsTextBox,'String',sprintf('%i Pieces, %i Good, %i Bad',...
                           numObjects,numObjects-numDefects,numDefects));
% Update handles structure
guidata(hObject, handles);

% --------------------------------------------------------------------
function fileTypes = supportedImageTypes
%#ok
% Helper function: data to populate image file I/O dialogs
fileTypes = {'*.jpg','JPEG (*.jpg)';'*.tif','TIFF (*.tif)';...
             '*.bmp','Bitmap (*.bmp)';'*.*','All files (*.*)'};

Contact us