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

   %% image analysis

%% Segment nuclei
                  
                    NucChannelDoG = imfilter(ch2, fspecial('gaussian', 1, 3), 'symmetric')...
        - imfilter(ch2, fspecial('gaussian', 10, 6), 'symmetric'); %it(NucChannelDoG);
                     NucMask = NucChannelDoG > 50;
                     NucMask = bwareaopen(NucMask, 10); %it(NucMask); it(ch2);
%% Segment neurons
    NeuroSegmentationIm = ch3; % Tuj1 only % vol(ch3,0,50) it(ch3);
    %vol(NeuroSegmentationIm, 0,50)
    
    
    NeuroSegmentationImGlobal = imfilter(NeuroSegmentationIm, fspecial('gaussian', 1, 3), 'symmetric'); % vol(NeuroSegmentationImGlobal,0,50)
    
    Im_Tuj1LP = imadjust(max(NeuroSegmentationImGlobal, [], 3), [0 0.01], [0 1]); %imtool(Im_Tuj1LP);
    
    
    NeuroSegmentationImLocal = imfilter(NeuroSegmentationIm, fspecial('gaussian', 1, 3), 'symmetric')...
        - imfilter(NeuroSegmentationIm, fspecial('gaussian', 5, 6), 'symmetric'); % vol(NeuroSegmentationImLocal,0,20)
    
    Im_Tuj1DoG = imadjust(max(NeuroSegmentationImLocal, [], 3), [0 0.005], [0 1]); %imtool(Im_Tuj1DoG);
    
    NeuroMaskGlobal = NeuroSegmentationImGlobal > 150; % vol(NeuroSegmentationImGlobal) % vol(NeuroMaskGlobal)
    
    Im_Tuj1_GlobalMask = max(NeuroMaskGlobal, [], 3); %imtool(Im_Tuj1_GlobalMask)
    
    NeuroMaskLocal = NeuroSegmentationImLocal > 3; % vol(NeuroSegmentationImLocal) % vol(NeuroMaskLocal)
    
    Im_Tuj1_LocalMask = NeuroMaskLocal; %imtool(Im_Tuj1_LocalMask)
    
    NeuroMask = NeuroMaskGlobal | NeuroMaskLocal; % Fill gaps % vol(NeuroMask) it(NeuroMask)

    % Remove small objects which are certainly no neurons
    NeuroMask = bwareaopen(NeuroMask, 10);
    
%% Segment TH
    
    THSegmentationIm = ch1; % 
    %vol(THSegmentationIm, 0,50)
    
    
    THSegmentationImGlobal = imfilter(THSegmentationIm, fspecial('gaussian', 1, 3), 'symmetric'); % vol(THSegmentationImGlobal,0,50)
    
    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)
    
    Im_THDoG = imadjust(max(THSegmentationImLocal, [], 3), [0 0.005], [0 1]); %imtool(Im_THDoG);
    
    THMaskGlobal = THSegmentationImGlobal > 150; % vol(THSegmentationImGlobal) % vol(THMaskGlobal)
    
    Im_TH_GlobalMask = max(THMaskGlobal, [], 3); %imtool(Im_TH_GlobalMask)
    
    THMaskLocal = THSegmentationImLocal > 3; % vol(THSegmentationImLocal) % vol(THMaskLocal)
    
    Im_TH_LocalMask = THMaskLocal; %imtool(Im_Tuj1_LocalMask)
    
    THMask = THMaskGlobal | THMaskLocal; % Fill gaps % vol(THMask)

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



        %% Collect data
        Objects = table();        
        InfoTableThisSample = InfoTableThis; 
        THinTHMask = ch1 .* uint16(THMask);
        Tuj1inTuj1Mask = ch3 .* uint16(NeuroMask);
        NucinNucMask = ch2 .* uint16(NucMask);
        Objects.THbyNucVol = sum(THinTHMask(:)) / sum(NucMask(:));
        Objects.THbyTHVol = sum(THinTHMask(:)) / sum(THMask(:)); % Average pixel value
        Objects.Tuj1byTuj1Vol = sum(Tuj1inTuj1Mask(:)) / sum(NeuroMask(:));
        Objects.Tuj1byNucVol = sum(Tuj1inTuj1Mask(:)) / sum(NucMask(:));
        Objects.THVolByTuj1Vol = sum(THMask(:)) / sum(NeuroMask(:)); % Average pixel value
        Objects.Tuj1VolByNucVol = sum(NeuroMask(:)) / sum(NucMask(:));
        Objects.THVolByNucVol = sum(THMask(:)) / sum(NucMask(:));
        Objects.Tuj1Vol = sum(NeuroMask(:)); % pixel count
        Objects.THVol = sum(THMask(:));
        Objects.NucVol = 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(100, 1.29, imSize, imSize(1)-50, 550, 10);
    %it(BarMask)
    
    % Channels with Mask
    Previewch1 = imadjust(max(ch1,[],3), [0 ; 0.009],[0 ; 1]);
    Previewch2 = imadjust(max(ch2,[],3),[0 ; 0.02],[0 ; 1]);
    Previewch3 = imadjust(max(ch3,[],3),[0 ; 0.006],[0 ; 1]);
    %it(Previewch1)
    %it(Previewch2)
    %it(Previewch3)
       
    % Channels with Mask
    PreviewTH = imoverlay2(imadjust(max(ch1,[],3), [0 ; 0.009],[0 ; 1]), bwperim(max(THMask,[],3)), [1 0 0]);
    PreviewTH = imoverlay2(PreviewTH, BarMask, [1 1 1]);
    PreviewTuj1 = imoverlay2(imadjust(max(ch3,[],3),[0 ; 0.006],[0 ; 1]), bwperim(max(NeuroMask,[],3)), [0 1 0]);
    PreviewTuj1 = imoverlay2(PreviewTuj1, BarMask, [1 1 1]);
    PreviewNuc = imoverlay2(imadjust(max(ch2,[],3),[0 ; 0.02],[0 ; 1]), bwperim(max(NucMask,[],3)), [0 0 1]);
    PreviewNuc = imoverlay2(PreviewNuc, BarMask, [1 1 1]);
    RGB = cat (3,  imadjust(max(ch1,[],3), [0 ; 0.009],[0 ; 1]), imadjust(max(ch3,[],3),[0 ; 0.006],[0 ; 1]), imadjust(max(ch2,[],3),[0 ; 0.02],[0 ; 1]));
    %it(PreviewTH)
    %it(PreviewTuj1)
    %it(PreviewNuc)
    %it(RGB)
    
    %Zoom on images
    [ZoomOverlayTH,ZoomTH,ZoomBoxinImageTH] = f_ZoomBoxRGB_JJ(PreviewTH,320,350,70,70,3,1);
    [ZoomOverlayTuj1,ZoomTuj1,ZoomBoxinImageTuj1] = f_ZoomBoxRGB_JJ(PreviewTuj1,320,350,70,70,3,1);
    [ZoomOverlayNuc,ZoomNuc,ZoomBoxinImageNuc] = f_ZoomBoxRGB_JJ(PreviewNuc,320,350,70,70,3,1);
    
    filename_ch1_TH = sprintf('%s\\%s_%s_%03d%03d%03d_ch1_TH.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ch2_Nuc = sprintf('%s\\%s_%s_%03d%03d%03d_ch2_Nuc.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ch3_Tuj1 = sprintf('%s\\%s_%s_%03d%03d%03d_ch3_Tuj1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_THMask = sprintf('%s\\%s_%s_%03d%03d%03d_THMask.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_Tuj1Mask = sprintf('%s\\%s_%s_%03d%03d%03d_Tuj1Mask.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_NucMask = sprintf('%s\\%s_%s_%03d%03d%03d_NucMask.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_RGB = sprintf('%s\\%s_%s_%03d%03d%03d_RGB.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomOverlayTH = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayTH.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomTH = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomTH.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomBoxinImageTH = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageTH.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomOverlayTuj1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayTuj1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomTuj1= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomTuj1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomBoxinImageTuj1 = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageTuj1.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomOverlayNuc = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomOverlayNuc.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomNuc= sprintf('%s\\%s_%s_%03d%03d%03d_ZoomNuc.tiff', PreviewPath, Barcode, Areaname, row, column, field);
    filename_ZoomBoxinImageNuc = sprintf('%s\\%s_%s_%03d%03d%03d_ZoomBoxinImageNuc.tiff', PreviewPath, Barcode, Areaname, row, column, field);

    imwrite(Previewch1, filename_ch1_TH);
    imwrite(Previewch2, filename_ch2_Nuc);
    imwrite(Previewch3, filename_ch3_Tuj1);
    imwrite(PreviewTuj1, filename_Tuj1Mask);
    imwrite(PreviewNuc, filename_NucMask);
    imwrite(PreviewTH, filename_THMask);
    imwrite(RGB, filename_RGB);
    imwrite(ZoomOverlayTuj1, filename_ZoomOverlayTuj1);
    imwrite(ZoomTuj1, filename_ZoomTuj1);
    imwrite(ZoomBoxinImageTuj1, filename_ZoomBoxinImageTuj1);
    imwrite(ZoomOverlayNuc, filename_ZoomOverlayNuc);
    imwrite(ZoomNuc, filename_ZoomNuc);
    imwrite(ZoomBoxinImageNuc, filename_ZoomBoxinImageNuc);
    imwrite(ZoomOverlayTH, filename_ZoomOverlayTH);
    imwrite(ZoomTH, filename_ZoomTH);
    imwrite(ZoomBoxinImageTH, filename_ZoomBoxinImageTH);
    
end

