function varargout = analyse(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @analyse_OpeningFcn, ...
                   'gui_OutputFcn',  @analyse_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 analyse is made visible.
function analyse_OpeningFcn(hObject, eventdata, handles, varargin)

global Anz_Mats;
global matfarbe;
global Image;
global Seg_Mat;

handles.mask = [];

Anz_Mats = 1;
matfarbe = char('og','og','ob','oy');
Image = ones(1944,2580);
Seg_Mat = ones(1944,2580);

axes(handles.bildflaeche);
imshow(Image);
axes(handles.bildflaeche);
imshow(Seg_Mat);

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = analyse_OutputFcn(hObject, eventdata, handles) 
% Get default command line output from handles structure
varargout{1} = handles.output;
%varargout{1} = getappdata(hObject,’result’);


% --------------------------------------------------------------------
function Bild_Callback(hObject, eventdata, handles)



% --------------------------------------------------------------------
function Oeffnen_Callback(hObject, eventdata, handles)

[ipFileName,PathName]=uigetfile( '*.tif');
c=uiimport(strcat(PathName,ipFileName));%c is a structure
% use FILEPARTS to separate the file name
[pname,fname,ext] = fileparts(strcat(PathName,ipFileName));

% use fname as a dynamic field name
global Image;
global Seg_Mat;
global Leer;
global R G;
global b;
global schonmarkiert;
global BW;

schonmarkiert=false(1,50);
b=1;
Image = c.(fname);

Seg_Mat = zeros(size(Image,1),size(Image,2),'int8');
Leer = uint8(ones(size(Image,1),size(Image,2)));
R = uint8(zeros(size(Image,1),size(Image,2),3));
G = uint8(zeros(size(Image,1),size(Image,2),3));
BW = false(size(Image,1),size(Image,2));
axes(handles.bildflaeche);
imshow(Image);
clearvars -global  x y BUNT BUNT2  J ;

% --------------------------------------------------------------------
function Speichern_Callback(hObject, eventdata, handles)
[arq,dir] = uiputfile('*.tif','Output Files');
fileName=fullfile(dir,arq);
f=getframe(handles.bildflaeche);
[x,map]=frame2im(f);
imwrite(x,fileName,'tif');

% --- Executes on button press in start_seg_button.
function start_seg_button_Callback(hObject, eventdata, handles)
global x;
global y;
global Image;
global J;
global Seg_Mat;
global G;
global Leer;
global R;
global schonmarkiert;
global BW;

handles.schwellwert = str2double(get(handles.schwellwert,'String'));
for i = 1:size(y,1)
    if schonmarkiert(i) == false
        set(handles.Fortschritt,'String',['Segment ',num2str(i),'/',num2str(size(y,1))]);
        [P, J] = regionGrowing(Image, [y(i,1) x(i,1)],handles.schwellwert, Inf, [], true, false);
        schonmarkiert(i)=true;
        Seg_Mat = double(Seg_Mat)+double(J);
    end
end
G=label2rgb(~Seg_Mat,'gray','g','shuffle');
BW((BW==1) & (Seg_Mat==1)) = 0;
R=label2rgb(~BW,'gray','r','shuffle');

Rest = Leer-G(:,:,2)-R(:,:,1);
Rest_RGB = label2rgb(~Rest,'gray','b','shuffle');
BUNT = imabsdiff(imabsdiff(R,G),Rest_RGB);

Image_gray = cat(3, Image,Image,Image);
BUNT2 = imadd(BUNT,Image_gray);
axes(handles.bildflaeche);
imshow(BUNT2);

% --- Executes on button press in markerSetzen_button.
function markerSetzen_button_Callback(hObject, eventdata, handles)
global matfarbe;
global Seg_Mat;
global x;
global y;
global b;

axes(handles.bildflaeche);
mat = 1;
maus_taste=1;

while maus_taste ==1
    [x_temp,y_temp,maus_taste] = ginput(1);
    if maus_taste == 1 
        x(b,mat)= uint16(x_temp);
        y(b,mat)= uint16(y_temp);       
        Seg_Mat(y(b,mat),x(b,mat)) = 1;
        hold on
        plot(x(b,mat),y(b,mat),matfarbe(mat,:));
        b=b+1;      
    end;
end;


function schwellwert_Callback(hObject, eventdata, handles)


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

% --- Executes during object creation, after setting all properties.
function schwellwert_CreateFcn(hObject, eventdata, handles)


% 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 glatt_markieren_button.
function glatt_markieren_button_Callback(hObject, eventdata, handles)
global Image;
global G;
global R;
global Leer;
global Seg_Mat;
global BW;

graumax = str2double(get(handles.maxgrau,'String'));
graumin = str2double(get(handles.mingrau,'String'));
GRAU_MIN_MAX = [0 graumax; graumin 255]; 
Image_eq = histeq(Image);
GRAUSTUFE = Image_eq;
GRAUSTUFE( (GRAUSTUFE<GRAU_MIN_MAX(1,1)) | (GRAUSTUFE>GRAU_MIN_MAX(1,2))) = 255;
GRAUSTUFE( (GRAUSTUFE<GRAU_MIN_MAX(2,1)) | (GRAUSTUFE>GRAU_MIN_MAX(2,2))) = 0;
BW = im2bw(GRAUSTUFE);
BW((BW==1) & (Seg_Mat>=1)) = 0;
R=label2rgb(~BW,'gray','r','shuffle');

Rest = Leer-G(:,:,2)-R(:,:,1);
Rest_RGB = label2rgb(~Rest,'gray','b','shuffle');
BUNT = imabsdiff(imabsdiff(R,G),Rest_RGB);

Image_gray = cat(3, Image,Image,Image);
BUNT2 = imadd(BUNT,Image_gray);
axes(handles.bildflaeche);
imshow(BUNT2);


% --- Executes on button press in retour_button.
function retour_button_Callback(hObject, eventdata, handles)
global Seg_Mat;
global BW;
global G;
global R;
global Image;
global b;
global schonmarkiert;

Seg_Mat = zeros(size(Image,1),size(Image,2),'int8');
R = uint8(zeros(size(Image,1),size(Image,2),3));
G = uint8(zeros(size(Image,1),size(Image,2),3));
BW = false(size(Image,1),size(Image,2));

axes(handles.bildflaeche);
imshow(Image);

b=1;
schonmarkiert=false(1,50);

clearvars -global  x y BUNT BUNT2  J ;



function mingrau_Callback(hObject, eventdata, handles)

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


% --- Executes during object creation, after setting all properties.
function mingrau_CreateFcn(hObject, eventdata, handles)

% 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 maxgrau_Callback(hObject, eventdata, handles)

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


% --- Executes during object creation, after setting all properties.
function maxgrau_CreateFcn(hObject, eventdata, handles)

% 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 markieren_button.
function markieren_button_Callback(hObject, eventdata, handles)
global BUNT2;
global h;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Hier soll ein Bereich markiert werden
im_h = imshow(BUNT2);
h=imfreehand(handles.bildflaeche);
handles.mask = createMask(h,im_h);

delete(h);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% --- Executes on button press in hinzufuegen_button.
function hinzufuegen_button_Callback(hObject, eventdata, handles)
axes(handles.bildflaeche);

global BUNT2;
global BUNT;
global G;
global Seg_Mat;
global Leer;
global BW;
global R;
global Image;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Hier soll der markierte Bereuch
Seg_Mat = and(Seg_Mat,~handles.mask);
G=label2rgb(MAT1,'gray','g','shuffle');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Rest = Leer-G(:,:,2)-R(:,:,1);
BW((BW==1) & (Seg_Mat==0)) = 0;
R=label2rgb(~BW,'gray','r','shuffle');
Rest_RGB = label2rgb(~Rest,'gray','b','shuffle');
BUNT = imabsdiff(imabsdiff(R,G),Rest_RGB);
Image_gray = cat(3, Image,Image,Image);
BUNT2 = imadd(BUNT,Image_gray);

% --- Executes on button press in entfernen_button.
function entfernen_button_Callback(hObject, eventdata, handles)

