Code covered by the BSD License  

Highlights from
Drawing Transparent Object on Image

image thumbnail
from Drawing Transparent Object on Image by Tan Chin Luh
Drawing transparent object on top of an image, and change the object properties ...

TransGUI(varargin)
function varargout = TransGUI(varargin)
% TRANSGUI M-file for TransGUI.fig
%      TRANSGUI, by itself, creates a new TRANSGUI or raises the existing
%      singleton*.
%
%      H = TRANSGUI returns the handle to a new TRANSGUI or the handle to
%      the existing singleton*.
%
%      TRANSGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TRANSGUI.M with the given input arguments.
%
%      TRANSGUI('Property','Value',...) creates a new TRANSGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before TransGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to TransGUI_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 TransGUI

% Last Modified by GUIDE v2.5 04-Nov-2005 21:54:40

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @TransGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @TransGUI_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 TransGUI is made visible.
function TransGUI_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 TransGUI (see VARARGIN)

% Choose default command line output for TransGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = TransGUI_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 pbLoad.
function pbLoad_Callback(hObject, eventdata, handles)
% hObject    handle to pbLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg';'*.bmp';'*.tif'});
S = imread([pathname filename]);
handles.S = S;
axes(handles.axes1);
imshow(S);
handles.output = hObject;
guidata(hObject, handles);


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

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pbColor1.
function pbColor1_Callback(hObject, eventdata, handles)
% hObject    handle to pbColor1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
color1 = colorui;
color1 = round(color1*100)/100;
set(handles.editColor1,'String',['[',num2str(color1),']']);

% --- Executes on button press in pbColor2.
function pbColor2_Callback(hObject, eventdata, handles)
% hObject    handle to pbColor2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
color2 = colorui;
color2 = round(color2*100)/100;
set(handles.editColor2,'String',['[',num2str(color2),']']);

% --- Executes on button press in pbRun.
function pbRun_Callback(hObject, eventdata, handles)
% hObject    handle to pbRun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tdelay = 0.01;
S = handles.S;
sz = size(S);
cnt = 0;
x = [0 cnt cnt 0];
y = [0 0  sz(1)+1 sz(1)+1];
z = [1 1 1 1];
tcolor(1,1,1:3) = str2num(get(handles.editColor1,'string'));
edgecolor = str2num(get(handles.editColor2,'string'));
transparency = str2num(get(handles.editTrans,'string'));

h = patch(x,y,z,tcolor,'FaceAlpha',transparency,'edgecolor',edgecolor);

if get(handles.radioHorizontal,'value')
    cnt = 0;
    x = [0 cnt cnt 0];
    y = [0 0  sz(1)+1 sz(1)+1];
    set(h,'Xdata',x);
    set(h,'Ydata',y);

    for cnt = 0:1:sz(2)
        set(h,'Xdata',[0 cnt cnt 0]);
        pause(tdelay)
    end

    for cnt = sz(2):-1:0
        set(h,'Xdata',[0 cnt cnt 0]);
        pause(tdelay)
    end
elseif get(handles.radioVertical,'value')
    x = [0 sz(2)+1 sz(2)+1 0];
    y = [0 0 cnt cnt];
    set(h,'Xdata',x);
    set(h,'Ydata',y);

    for cnt = 0:1:sz(1)
        set(h,'Ydata',[0 0  cnt cnt]);
        pause(tdelay)
    end

    for cnt = sz(1):-1:0
        set(h,'Ydata',[0 0  cnt cnt]);
        pause(tdelay)
    end
elseif get(handles.radioBeam,'value')
    cnt = 0;
    cnt2 = 0;
    x = [cnt cnt+10 cnt+10 cnt];
    y = [cnt2 cnt2  cnt2+10 cnt2+10];
    z = [1 1 1 1];
    h = patch(x,y,z,tcolor,'FaceAlpha',transparency,'edgecolor',edgecolor);

    for cnt2 = 0:10:sz(1)
        for cnt = 0:10:sz(2)+10
            set(h,'Xdata',[cnt cnt+10 cnt+10 cnt]);
            set(h,'Ydata',[cnt2 cnt2  cnt2+10 cnt2+10]);
            pause(tdelay)
        end
    end
end



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

% Hints: get(hObject,'String') returns contents of editTrans as text
%        str2double(get(hObject,'String')) returns contents of editTrans as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


Contact us at files@mathworks.com