from Chroma Key-ing DEMO by Aleksandar Andonov
Makes Chroma Keying on pictures

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

% Last Modified by GUIDE v2.5 19-Mar-2006 13:02:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SOS_OpeningFcn, ...
                   'gui_OutputFcn',  @SOS_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 SOS is made visible.
function SOS_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 SOS (see VARARGIN)

% Choose default command line output for SOS
handles.output = hObject;
guidata(hObject, handles);
%handles
handles.p = zeros(1,3);
guidata(hObject, handles);

handles

%handles.p=[0 0 0];


% Update handles structure

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


% --- Outputs from this function are returned to the command line.
function varargout = SOS_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;
%varargout{3} = handles.tried;
clear all;
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)

d1=get(hObject,'Value');
z=get(handles.slider1,'Value')*128;
z1=num2str(z);

set(handles.edit6,'String',z1);

% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider




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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
d2=get(hObject,'Value')

z=get(handles.slider2,'Value')*128;
z1=num2str(z);

set(handles.edit5,'String',z1);

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject, handles); % 
handles.d1=get(hObject,'Value');
z=get(handles.slider3,'Value')*128;
z1=num2str(z);

set(handles.edit4,'String',z1);

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rgb=get(handles.checkbox1,'Value');
gray=get(handles.checkbox2,'Value');
hsv=get(handles.checkbox3,'Value');
select=get(handles.checkbox4,'Value');
p=zeros(1,3);
p=handles.p;

global a b ime1 ime2;

a=imread(ime1);
b=imread(ime2);
p_hsv=rgb2hsv(p)
a_hsv=rgb2hsv(a);
siz=size(a);
mask1=zeros(siz(1),siz(2));
mask2=zeros(siz(1),siz(2));
mask3=zeros(siz(1),siz(2));
mask=zeros(siz(1),siz(2));
mak=zeros(siz);
d1=get(handles.slider1,'Value')*128;
d2=get(handles.slider2,'Value')*128;
d3=get(handles.slider3,'Value')*128;
p1=p(1);p1h=p_hsv(1);
p2=p(2);p2h=p_hsv(2);
p3=p(3);p3h=p_hsv(3);
output.m1=get(handles.checkbox2,'Value');
if (output.m1==1)
mask1((a(:,:,1)>=(p1-d1))&(a(:,:,1)<=(p1+d1)))=1;
mask2((a(:,:,2)>=(p2-d2))&(a(:,:,2)<=(p2+d2)))=1;
mask3((a(:,:,3)>=(p3-d3))&(a(:,:,3)<=(p3+d3)))=1;
end
output.m3=get(handles.checkbox3,'Value');
if output.m3==1
    p3h=p(3)/255;
mask1((a_hsv(:,:,1)>=(p1h-d1*.8/128))&(a_hsv(:,:,1)<=(p1h+d1*.8/128)))=1;
mask2((a_hsv(:,:,2)>=(p2h-d2*.8/128))&(a_hsv(:,:,2)<=(p2h+d2*.8/128)))=1;
mask3((a_hsv(:,:,3)>=(p3h-d3*.8/128))&(a_hsv(:,:,3)<=(p3h+d3*.8/128)))=1;
end    
output.m2=get(handles.checkbox1,'Value');
if output.m2==1
    p=(p1+p2+p3)/3
    a_grskle=rgb2gray(a);
    d=get(handles.slider3,'Value')*128
    mask1((a_grskle(:,:)>=(p-d))&(a_grskle(:,:)<=(p+d)))=1;
    mask2((a_grskle(:,:)>=(p-d))&(a_grskle(:,:)<=(p+d)))=1;
    mask3((a_grskle(:,:)>=(p-d))&(a_grskle(:,:)<=(p+d)))=1;
end
if (output.m1==0)&(output.m1==0)&(output.m1==0)
mask1((a(:,:,1)>=(p1-d1))&(a(:,:,1)<=(p1+d1)))=1;
mask2((a(:,:,2)>=(p2-d2))&(a(:,:,2)<=(p2+d2)))=1;
mask3((a(:,:,3)>=(p3-d3))&(a(:,:,3)<=(p3+d3)))=1;
end
    
%output.m2=get(handles.checkbox1,'Value');
%if output.m2==1
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%tuka po luminancija
%if output.m2==1
 %   dlum=(d1+d2+d3)/3;

%figure, imshow(1-mask1);
%figure, imshow(1-mask2);
%figure, imshow(1-mask3);
clear  siz;
clear p1 p2 p3 ;
mask=mask1&mask2&mask3;
clear mask1 mask2 mask3;
mak(:,:,1)=mask;
mak(:,:,2)=mask;
mak(:,:,3)=mask;
%clear mask;
%figure, imshow(mak);
a=double(a)/255;
b=double(b)/255;
guidata(hObject,handles);
handles.output.p=p;
handles.output.d1=d1;
handles.output.d2=d2;
handles.output.d3=d3;
handles.output.m1=rgb;
handles.output.m2=gray;
handles.output.m3=hsv;
guidata(hObject,handles);
output.p=p;
output.d1=d1;
output.d2=d2;
output.d3=d3;
output.m1=rgb;
output.m2=gray;
output.m3=hsv;
output.ime=ime1;
save mask output;
handles.output
if select==0
figure, imshow((1-mak).*a+mak.*b);
c=(1-mak).*a+mak.*b;
else
    figure, imshow(mak.*a+(1-mak).*b);
end

%figure,imshow((1-mak).*a);figure,imshow(mak.*b);
%figure, imshow(c,[]);
%figure,imshow((1-mask).*a(2),[]);
%figure,imshow(mask.*a(2),[]);
handles.output



% --- 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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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)
global  ime1;
ime1=get(hObject,'String');
ime1=char(ime1);


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


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
global a ime1;

a=imread(ime1);
%b=imread('bscap0012.JPG');
figure,imshow(a);
p1=impixel;
close figure No. 1;
guidata(hObject,handles)
handles.p=p1;
guidata(hObject,handles)
handles

%prefrli gi gore


% 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
d1=get(hObject,'Value');
d2=get(handles.checkbox2,'Value');
d3=get(handles.checkbox3,'Value');
if d1==1&(d2==0 & d3==0)
    set(handles.checkbox2,'visible','off');
    set(handles.checkbox3,'visible','off');
    set(handles.slider2,'enable','off');
    set(handles.slider1,'enable','off');
    set(handles.text7,'String','Illumination');
    set(handles.text8,'String','');
    set(handles.text9,'String','');
    
    set(handles.edit5,'enable','off');
    set(handles.edit6,'enable','off');
else 
    set(handles.checkbox2,'visible','on');
    set(handles.checkbox3,'visible','on');
    set(handles.slider2,'enable','on');
    set(handles.slider1,'enable','on');
    set(handles.text7,'String','Red'); 
    set(handles.text8,'String','Green');
    set(handles.text9,'String','Blue');
   
    set(handles.edit5,'enable','on');
    set(handles.edit6,'enable','on');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
d=get(hObject,'Value');
% Hint: get(hObject,'Value') returns toggle state of checkbox2
d1=get(hObject,'Value');
d2=get(handles.checkbox1,'Value');
d3=get(handles.checkbox3,'Value');
if d1==1&(d2==0 & d3==0)
    set(handles.checkbox1,'visible','off');
    set(handles.checkbox3,'visible','off');
    set(handles.text7,'String','Red');
    set(handles.text8,'String','Green');
    set(handles.text9,'String','Blue');
else 
    set(handles.checkbox1,'visible','on');
    set(handles.checkbox3,'visible','on');
    set(handles.text7,'String','Red');
    set(handles.text8,'String','Green');
    set(handles.text9,'String','Blue');

end

% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
d1=get(hObject,'Value');
d2=get(handles.checkbox1,'Value');
d3=get(handles.checkbox2,'Value');
if d1==1&(d2==0 & d3==0)
    set(handles.checkbox1,'visible','off');
    set(handles.checkbox2,'visible','off');
    set(handles.slider2,'enable','on');
    set(handles.slider1,'enable','on');
    set(handles.text7,'String','Hue');
    set(handles.text8,'String','Saturation');
    set(handles.text9,'String','Value');

else 
    set(handles.checkbox1,'visible','on');
    set(handles.checkbox2,'visible','on');
    set(handles.slider2,'enable','on');
    set(handles.slider1,'enable','on');
    set(handles.text7,'String','Red'); 
    set(handles.text8,'String','Green');
    set(handles.text9,'String','Blue');
end
% Hint: get(hObject,'Value') returns toggle state of checkbox3


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox4




% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ime2
ime2=get(hObject,'String');
ime2=char(ime2);% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
global b ime2;
b=imread(ime2);

% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%z1=get(handles.slider2,'Value')
%set(hObject,'String',z1)
z1=0;
z=get(hObject,'String');
hObject
z1=char(z)
z1=str2double(z1)/128
set(handles.slider3,'Value',z1);
% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
z1=0;
z=get(hObject,'String');
hObject
z1=char(z)
z1=round(str2double(z1))/128;
set(handles.slider2,'Value',z1);
% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
z1=0;
z=get(hObject,'String');
hObject
z1=char(z)
z1=str2double(z1)/128
set(handles.slider1,'Value',z1);
% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


Contact us at files@mathworks.com