function [Objects] = Analysis(InfoTableThis, f, DataPath, ch1, ch2, ch3)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

   %% image analysis
   %% Zoom explanation
%[ZoomOverlay Zoom] = f_ZoomBoxRGB(Im, bottomleft_x, bottomleft_y, width, hight, zoomfactor, linewidth)

%% Raw Images
 % Scalebar
    imSize = [size(ch1, 1), size(ch1, 2)];
    [BarMask, BarCenter] = f_barMask(50, 0.646, imSize, imSize(1)-50, 550, 10);
    %it(BarMask)
    
    %Im_NucleiRaw = imadjust(max(ch1, [], 3), [0 0.02], [0 1]); 
    %imtool(Im_NucleiRaw); %imwrite(Im_NucleiRaw,'Im_NucleiRaw.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(Im_NucleiRaw, 390, 360, 50, 50, 3, 1);
    % ZoomOverlay = imoverlay2(ZoomOverlay, BarMask, [1 1 1]);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'Im_NucleiRaw_ZO.png')
    %it(Zoom) %imwrite(Zoom,'Im_NucleiRaw_Z.png')
    
    %Im_Ki67Raw = imadjust(max(ch2, [], 3), [0 0.015], [0 1]); 
    %imtool(Im_Ki67Raw); %imwrite(Im_Ki67Raw,'Im_Ki67Raw.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(Im_Ki67Raw, 390, 360, 50, 50, 3, 1);
    % ZoomOverlay = imoverlay2(ZoomOverlay, BarMask, [1 1 1]);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'Im_Ki67Raw_ZO.png')
    %it(Zoom) %imwrite(Zoom,'Im_Ki67Raw_Z.png')

   
%% Segment nuclei
                  
                    NucChannelDoG = imfilter(ch1, fspecial('gaussian', 10, 2), 'symmetric')...
        - imfilter(ch1, fspecial('gaussian', 20, 6), 'symmetric'); %it(NucChannelDoG);
    
    %Im_NucChannelDoG = imadjust(max(NucChannelDoG, [], 3), [0 0.003], [0 1]); 
    %imtool(Im_NucChannelDoG); %imwrite(Im_NucChannelDoG,'Im_NucChannelDoG.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(Im_NucChannelDoG, 390, 360, 50, 50, 3, 1);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'Im_NucChannelDoG_ZO.png')
    %it(Zoom) %imwrite(Zoom,'Im_NucChannelDoG_Z.png')                 
                     
                     NucMask = NucChannelDoG > 15;
                     NucMask = bwareaopen(NucMask, 10); %it(NucMask); it(ch1);
    %imtool(NucMask); %imwrite(NucMask,'NucMask.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(NucMask, 390, 360, 50, 50, 3, 1);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'NucMask_ZO.png')
    %it(Zoom) %imwrite(Zoom,'NucMask_Z.png') 
                        
%% Segment TH
    
    THSegmentationIm = ch3; % 
    %vol(THSegmentationIm, 0,50)
    
    
    THSegmentationImGlobal = imfilter(THSegmentationIm, fspecial('gaussian', 1, 3), 'symmetric'); % it(THSegmentationImGlobal)
    
    Im_THLP = imadjust(max(THSegmentationImGlobal, [], 3), [0 0.01], [0 1]); %imtool(Im_THLP);
    
    
    THSegmentationImLocal = imfilter(THSegmentationIm, fspecial('gaussian', 1, 3), 'symmetric')...
        - imfilter(THSegmentationIm, fspecial('gaussian', 5, 6), 'symmetric'); % vol(THSegmentationImLocal,0,20) it(THSegmentationImLocal)
    
    Im_THDoG = imadjust(max(THSegmentationImLocal, [], 3), [0 0.005], [0 1]); %imtool(Im_THDoG);
    
    THMaskGlobal = THSegmentationImGlobal > 100; % vol(THSegmentationImGlobal) % vol(THMaskGlobal) it(THMaskGlobal)
    
    Im_TH_GlobalMask = max(THMaskGlobal, [], 3); %imtool(Im_TH_GlobalMask)
    
    THMaskLocal = THSegmentationImLocal > 5; % vol(THSegmentationImLocal) % vol(THMaskLocal) it(THMaskLocal)
    
    Im_TH_LocalMask = THMaskLocal; 
    
    THMask = THMaskGlobal | THMaskLocal; % Fill gaps % vol(THMask)

    % Remove small objects which are certainly no TH neurons
    THMask = bwareaopen(THMask, 10); %it(THMask) it(ch3)
%% Segment Ki67

    ch2LP = imfilter(ch2, fspecial('gaussian', 10, 1), 'symmetric');%it(ch2LP) it(ch2)
    %Ki67LP = imadjust(max(ch2LP, [], 3), [0 0.01], [0 1]);
    %imtool(Ki67LP); %imwrite(Ki67LP,'Ki67LP.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(Ki67LP, 390, 360, 50, 50, 3, 1);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'Ki67LP_ZO.png')
    %it(Zoom) %imwrite(Zoom,'Ki67LP_Z.png') 
    
    Ki67Mask = ch2LP > 60;
    Ki67Mask = bwareaopen(Ki67Mask, 150);%it(Ki67Mask)
    
    %imtool(Ki67Mask); %imwrite(Ki67Mask,'Ki67Mask.png')
    %[ZoomOverlay Zoom] = f_ZoomBoxRGB(Ki67Mask, 390, 360, 50, 50, 3, 1);
    %it(ZoomOverlay) %imwrite(ZoomOverlay,'Ki67Mask_ZO.png')
    %it(Zoom) %imwrite(Zoom,'Ki67Mask_Z.png') 
    
        %% Collect data
        Objects = table();        
        InfoTableThisSample = InfoTableThis; 
        THinTHMask = ch3 .* uint16(THMask);
        Ki67inKi67Mask = ch2 .* uint16(Ki67Mask);
        Objects.THbyNucArea = sum(THinTHMask(:)) / sum(NucMask(:));
        Objects.THbyTHArea = sum(THinTHMask(:)) / sum(THMask(:)); % Average pixel value
        Objects.Ki67byNucArea = sum(Ki67inKi67Mask(:)) / sum(NucMask(:));
        Objects.Ki67AreaByNucArea = sum(Ki67Mask(:)) / sum(NucMask(:));
        Objects.THAreaByNucArea = sum(THMask(:)) / sum(NucMask(:));
        Objects.Ki67Area = sum(Ki67Mask(:)); % pixel count
        Objects.THArea = sum(THMask(:));
        Objects.NucArea = sum(NucMask(:));
        Objects = [InfoTableThisSample, Objects];
        
         %% save 2D previews
    
    PreviewPath = [DataPath, filesep, 'Previews'];
    Barcode = InfoTableThis.Barcode{:};
    Areaname = InfoTableThis.AreaName{:}; 
    row = InfoTableThis.Row;
    column = InfoTableThis.Column;
    field = InfoTableThis.field;
    
    % Scalebar
    imSize = [size(ch1, 1), size(ch1, 2)];
    [BarMask, BarCenter] = f_barMask(50, 0.646, imSize, imSize(1)-50, 550, 10);
    %it(BarMask)
    chEmpty = max(zeros(size(ch1),'uint16'),[],3);
   
    Previewch2 = imadjust(ch2, [0; 0.02],[0; 1]);
    Previewch1 = imadjust(ch1, [0; 0.02],[0; 1]);
    Previewch3 = imadjust(ch3, [0; 0.01],[0; 1]);
    %it(Previewch2)
    %it(Previewch1)
    %it(Previewch3)
%     Previewch2 = imadjust(max(ch2,[],3));
%     Previewch1 = imadjust(max(ch1,[],3));
%     Previewch3 = imadjust(max(ch3,[],3));
    
    % Channels with Mask
%     PreviewTH = imoverlay2(imadjust(max(ch3,[],3)), bwperim(max(THMask,[],3)), [1 0 0]);
%     PreviewTH = imoverlay2(PreviewTH, BarMask, [1 1 1]);
%     PreviewKi67 = imoverlay2(imadjust(max(ch2,[],3)), bwperim(max(Ki67Mask,[],3)), [0 1 0]);
%     PreviewKi67 = imoverlay2(PreviewKi67, BarMask, [1 1 1]);
%     PreviewNuc = imoverlay2(imadjust(max(ch1,[],3)), bwperim(max(NucMask,[],3)), [0 0 1]);
%     PreviewNuc = imoverlay2(PreviewNuc, BarMask, [1 1 1]);
    %it(PreviewTH)
    %it(PreviewKi67)
    %it(PreviewNuc)
    PreviewTH = imoverlay2(imadjust(max(ch3,[],3)), bwperim(max(THMask,[],3)), [1 0 0]);
    PreviewTH = imoverlay2(PreviewTH, BarMask, [1 1 1]);
    PreviewKi67 = imoverlay2(Previewch2, bwperim(max(Ki67Mask,[],3)), [0 1 0]);
    PreviewKi67 = imoverlay2(PreviewKi67, BarMask, [1 1 1]);
    PreviewNuc = imoverlay2(Previewch1, bwperim(max(NucMask,[],3)), [0 0 1]);
    PreviewNuc = imoverlay2(PreviewNuc, BarMask, [1 1 1]);
    %it(PreviewTH)
    %it(PreviewKi67)
    %it(PreviewNuc)
    RGB = cat(3, Previewch2, chEmpty ,Previewch1);% imtool(RGB)
    RGB = imoverlay2(RGB,bwperim(max(Ki67Mask,[],3)), [0 1 0]);
    RGB = imoverlay2(RGB, BarMask, [1 1 1]);
    %Zoom on images
    [ZoomOverlayRGB,ZoomRGB,ZoomBoxinImageRGB] = f_ZoomBoxRGB_JJ(RGB,350,367,80,80,3,1);
    
    
    filename_ch3_TH = sprintf('%s\\%s_%s_%03d%03d%03d_ch3_TH.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ch1_Nuc = sprintf('%s\\%s_%s_%03d%03d%03d_ch1_Nuc.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ch2_Ki67 = sprintf('%s\\%s_%s_%03d%03d%03d_ch2_Ki67.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_THMask = sprintf('%s\\%s_%s_%03d%03d%03d_THMask.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_Ki67Mask = sprintf('%s\\%s_%s_%03d%03d%03d_Ki67Mask.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_NucMask = sprintf('%s\\%s_%s_%03d%03d%03d_NucMask.png', PreviewPath, Barcode, Areaname, row, column, field);
    filename_RGB = sprintf('%s\\%s_%s_%03d%03d%03d_RGB.png', PreviewPath, Barcode, Areaname, row, column, field);
      
    filename_ZoomOverlayRGB = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayRGB.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomRGB= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomRGB.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomBoxinImageRGB = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageRGB.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    
    imwrite(Previewch3, filename_ch3_TH);
    imwrite(Previewch1, filename_ch1_Nuc);
    imwrite(Previewch2, filename_ch2_Ki67);
    imwrite(PreviewKi67, filename_Ki67Mask);
    imwrite(PreviewNuc, filename_NucMask);
    imwrite(PreviewTH, filename_THMask); 
    imwrite(RGB, filename_RGB);
    imwrite(ZoomOverlayRGB, filename_ZoomOverlayRGB);
    imwrite(ZoomRGB, filename_ZoomRGB);
    imwrite(ZoomBoxinImageRGB, filename_ZoomBoxinImageRGB);
   
    
end

