
function leafOnly = leafDetect(capture1)

    capture = rgb2gray(capture1);
    
    level = graythresh(capture);
    capture_bw = im2bw(capture,level);
    capture_bw =not(capture_bw);

    image = bwlabel (capture_bw);
    [height, width] = size(image);

    Properties = regionprops (image, 'BoundingBox');
%     figure; imshow(capture1);
    NumberOfBoxes = max(size(Properties));
    i=1;
    for k   = 1:NumberOfBoxes
        Box = Properties(k).BoundingBox;
        if ((Box(3) >= Box(4)*0.9 && Box(4) >= Box(3)*0.9) ||(Box(4) >= Box(3)*0.5)) && Box(4) > height * 0.1 && Box(4) < height * 0.6
%             rectangle('Position', [Box(1), Box(2), Box(3), Box(4),],...
%                 'EdgeColor', 'r', 'LineWidth', 2);
            boxX{i}= Box(1);
            boxY{i}= Box(2);
            boxWidth{i}= Box(3);
            boxHeight{i}= Box(4);
             i=i+1
        end
    end  
    
    k=1;
    for m = boxX{1}: (boxX{1}+boxWidth{1})
        circle1_x(k)= m;
        k=k+1;
    end
    
     k=1;
     for m= boxY{1}: (boxY{1}+boxHeight{1})
       circle1_y(k)= m;
        k=k+1;
     end
    
    k=1;
    for m= boxX{2}: (boxX{2}+boxWidth{2})
        circle2_x(k)= m;
        k=k+1;
    end
    cx= intersect(circle1_x, circle2_x);
     
     k=1;
     for m= boxY{2}: (boxY{2}+boxHeight{2})
         circle2_y(k) = m;
         k=k+1;
     end
     cy = intersect(circle1_y, circle2_y);
 
    % Identifying position of circles
    if(~(isempty(cx)))                  %if X-axis common, circles are vertically alligned
        xCenter = boxX{2} +(boxWidth{2}/2);
        if (xCenter > width/2)          %circles are on right side of image
             if (boxX{1} <boxX{2})
                leaf= (imcrop(capture1, [1,1, boxX{1}-20, height]));
             else                       
                 leaf = (imcrop(capture1, [1,1, boxX{2}-20, height]));
             end
        else                            %circles are on left side of image
             if (boxX{1} > boxX{2})
                leaf =(imcrop(capture1, [boxX{1}+ boxWidth{1}+20,1, width, height]));
             else
                 leaf= (imcrop(capture1, [boxX{2}+ boxWidth{2}+20,1, width, height]));
            end
        end

    else if(~(isempty(cy)))             %if Y-axis common, circles are Horizontally alligned
         yCenter = boxY{2} +(boxHeight{2}/2);
         if (yCenter > height/2)        %circles are on bottom of image
             if (boxY{1}< boxY{2})
                leaf= (imcrop(capture1, [1,1, width, boxY{1}-20]));
             else
                 leaf = (imcrop(capture1, [1,1, width, boxY{2}-20]));
             end
         else                           %circles are on upper of image
             if (boxY{1}< boxY{2})
                leaf= (imcrop(capture1, [1,boxY{2}+boxHeight{2}+20, width, height]));
             else
                 leaf= (imcrop(capture1, [1,boxY{1}+boxHeight{1}+20, width, height]));
             end
         end
        end
    end
    figure;
    imshow(leaf);
    figure;
    leaf = imresize(leaf, 0.4);
    leafOnly = RemoveBackground(leaf);
    
%     imshow(leafOnly);
end