function Gapson(limit1,directory,directory2,directory4,ending,from,threshold,threshold_2,crop,last_number)
%Function Gapson creates BW figures
%Gapson connects ends of objects what gives in the end correct single fiber
%
obrazek=dir([directory,ending]);
%Test or not
if last_number == 1
    last=length(obrazek);
else
    last=1;
end
for i=from:last;
    A=[];
    % obrazek=dir([directory,'\*.jpg']);
    nazwa=obrazek(i).name;
    % Open image
    pic=imread([directory,'\',nazwa]);
    disp(sprintf('Progress: %d / %d',i,length(obrazek)));
    % Green part of image
    pic=pic(:,:,2);
    % Crop of image
    pic = imcrop(pic,crop);
    % Image average
    h = fspecial('average');
    pic = imfilter(pic, h);
    
    %Rremove additional objects smaller than our fiber
    BW_mask = pic > threshold_2;
    [BW_lab_every_obj, number] = bwlabel(BW_mask, 8);
    area_of_interest = regionprops(BW_lab_every_obj, 'Area');
    for f=1:number;
         a = [area_of_interest(f).Area, f];
         A = vertcat(A, a);
    end
    [value1, location] = max(A(:));
    [Rr1,Cc1] = ind2sub(size(A),location);
    Obj_of_interest=(BW_lab_every_obj==Rr1);
    %
    BW = pic > threshold;
    
    if last_number == 1
        %disp('Obliczam dalej')
    else
        filename = [directory2,'\','raw.bmp'];
        filename2 = [directory2,'\','mask.bmp'];
        filename3 = [directory2,'\','BW.bmp'];
        imwrite(pic,filename);
        imwrite(BW_mask,filename2);
        imwrite(BW,filename3);
        disp('Test')
    end
    
    
    %Here we use mask Obj_of_interest
    BW = BW .* Obj_of_interest;     
    % Set object to insert in fiber size [4 65] like observed fiber    
    se = strel('rectangle',[4 15]);
    % Morphological process
    BW_corrected = BW;%imclose(BW,se);
    % Thinning of the fiber   
        %BW_thin = bwmorph(BW_corrected,'thin', Inf);
    % Deletion of single pixels
        %BW3 = bwmorph(BW2,'clean', Inf);
        %BW_endpoints = bwmorph(BW_thin,'endpoints', Inf);
    %Label not connected objects on fiber   
    [BW_labeled, num] = bwlabel(BW_corrected, 8);
    %area_label = regionprops(BW_labeled, 'Area');
    Z=[];
    for f=1:num;
        BW_spured = bwmorph(BW_labeled==f,'spur',2);
        BW_thin_prim = bwmorph(BW_spured,'thin', Inf);
        BW_endpoints_prim = bwmorph(BW_thin_prim,'endpoints', Inf);
        r = regionprops(BW_endpoints_prim, 'PixelIdxList');
        [y1,x1] = ind2sub(size(BW_endpoints_prim), r(1).PixelIdxList);

        % Used when pixels touch each other
        if numel(x1)>1 && numel(r)>1 
            x2=x1(end);
            x1=x1(1);
            y2=y1(end);
            y1=y1(1);
            z = [x1, y1, f;
                 x2, y2, f];
        % Used when object shrinks to a point
        elseif numel(r)==1
            x1=x1(1);
            y1=y1(1);
            z = [x1, y1, f;
                 x1, y1, f];
        % Used when normal situation takes place and we have two points
        elseif numel(r)>1
            [y2,x2] = ind2sub(size(BW_endpoints_prim), r(2).PixelIdxList);
            if numel(x2)>1
                x2=x2(end);
                %x1=x1;
                y2=y2(end);
                %y1=y1(1);               
            end
            z = [x1, y1, f;
                 x2, y2, f];
        else
            [y2,x2] = ind2sub(size(BW_endpoints_prim), r(2).PixelIdxList);
             z = [x1, y1, f;
                  x2, y2, f];
        end      
        Z = vertcat(Z, z);
    end
    %Z
    SORT=[];
    %szukamy ile jest x-ow
    for k = 1:(numel(Z(:,1)))
        DIST=[];
        for l = 1:(numel(Z(:,1)))
            if Z(k,3)==Z(l,3)
                %disp('Ten sam obiekt')
            else
                dist=[sqrt((Z(k,1)-Z(l,1))^2+(Z(k,2)-Z(l,2))^2),Z(k,1),Z(k,2),Z(l,1),Z(l,2),Z(k,3),Z(l,3)];
                dist;
                if dist(:,1) < limit1
                    DIST = vertcat(DIST, dist);
                end
            end
        if l==numel(Z(:,1)) && ~isempty(DIST)   
            DIST;
            col=DIST(:,1);
            minol=find(col==(min(min(col))));
            minolek=DIST(minol(1),:);
            SORT = vertcat(SORT, minolek);
        end
        end
    end
    if ~isempty(SORT)
        for k = 1:numel(SORT(:,1))
            imshow(BW);
            hLiner = imline(gca,[SORT(k,2) SORT(k,4)],[SORT(k,3) SORT(k,5)]);
            binaryImager2 = hLiner.createMask();
            BW_corrected(binaryImager2) = 255;
        end
    end  
    BW_thin = bwmorph(BW_corrected,'thin', Inf);
%-------------------------SAVE-------------------------%
    filename = [directory2,'\',nazwa(1:end-3),'bmp'];
    imwrite(BW_corrected,filename);
    filename2 = [directory4,'\',nazwa(1:end-3),'bmp'];
    imwrite(BW_thin,filename2);
    
end