function [imageCornerULx,imageCornerULy,imageCornerURx,imageCornerURy,imageCornerDLx,imageCornerDLy,imageCornerDRx,imageCornerDRy,Image_filled] = rectangleCorners(Image_BIN,subplotVal1,subplotVal2,subplotVal3)
% Funkce vraci x a y souradnice rohovych bodu prostredniho obdelniku 
% a vyobrazi vstupni binarni obraz v zadanem subplotu 
% s vizualizaci procesu hledani rohovych bodu

% imageCorner{pozice}{osa} = souradnice 
% Image_filled = vyplneny binarni obraz

%%  Image boundaries
[ImgY,ImgX,~] = size(Image_BIN); % rozmery Image

Image_filled = imfill(Image_BIN,'holes');
Image_bwboundaries = bwboundaries(Image_filled);
[Image_boundary_num,~]=size(Image_bwboundaries);
Image_boundaries = [];
for i=1:Image_boundary_num
    Image_boundary = Image_bwboundaries{i};
    Image_boundary_tmp = Image_BIN;
    Image_boundary_tmp(:,:,:) = [0];
    for i=1:length(Image_boundary)
        Image_boundary_tmp(Image_boundary(i,1),Image_boundary(i,2),:) = [1];
    end
    Image_boundaries_filled = imfill(Image_boundary_tmp,'holes');
    if Image_boundaries_filled(round(ImgY/2),round(ImgX/2))==1
        Image_boundaries = Image_boundary_tmp;
        Image_filled = Image_boundaries_filled;
    end
end

%% find 4 sides in Image
[~,ImageFirstX] = find(Image_boundaries==1,1,'first');
[~,ImageLastX] = find(Image_boundaries==1,1,'last');
[ImageTopY,~] = find(sum(Image_boundaries, 2)>0, 1, 'first');
[ImageBottomY,~] = find(sum(Image_boundaries, 2)>0, 1, 'last');

ImgW = (ImageLastX-ImageFirstX)*0.3;
ImgH = (ImageBottomY-ImageTopY)*0.3;

ImageUp = Image_boundaries;
ImageUp(:,[1:round((ImageFirstX+((ImageLastX-ImageFirstX)/2))-(ImgW)),round((ImageFirstX+((ImageLastX-ImageFirstX)/2))+(ImgW)):end]) = [0];
ImageUp(round(ImageTopY+((ImageBottomY-ImageTopY)/2)):end,:) = [0];

ImageDown = Image_boundaries;
ImageDown(:,[1:round((ImageFirstX+((ImageLastX-ImageFirstX)/2))-(ImgW)),round((ImageFirstX+((ImageLastX-ImageFirstX)/2))+(ImgW)):end]) = [0];
ImageDown(1:round(ImageTopY+((ImageBottomY-ImageTopY)/2)),:) = [0];

ImageLeft = Image_boundaries;
ImageLeft(:,round(ImageFirstX+((ImageLastX-ImageFirstX)/2)):end) = [0];
ImageLeft([1:round((ImageTopY+((ImageBottomY-ImageTopY)/2))-(ImgH)),round((ImageTopY+((ImageBottomY-ImageTopY)/2))+(ImgH)):end],:) = [0];

ImageRight = Image_boundaries;
ImageRight(:,1:round(ImageFirstX+((ImageLastX-ImageFirstX)/2))) = [0];
ImageRight([1:round((ImageTopY+((ImageBottomY-ImageTopY)/2))-(ImgH)),round((ImageTopY+((ImageBottomY-ImageTopY)/2))+(ImgH)):end],:) = [0];


[ImageUpY,ImageUpX] = find(ImageUp==1);
[ImageDownY,ImageDownX] = find(ImageDown==1);
[ImageLeftY,ImageLeftX] = find(ImageLeft==1);
[ImageRightY,ImageRightX] = find(ImageRight==1);
[ImageY,ImageX] = find(Image_BIN==1);


%% get corners
ImageUpVector = polyfit(ImageUpX,ImageUpY,1);
ImageDownVector = polyfit(ImageDownX,ImageDownY,1);
ImageLeftVector = polyfit(ImageLeftX,ImageLeftY,1);
ImageRightVector = polyfit(ImageRightX,ImageRightY,1);

ImageUpLine = polyval(ImageUpVector, (1:ImgX));
ImageDownLine = polyval(ImageDownVector, (1:ImgX));
ImageLeftLine = polyval(ImageLeftVector, (1:ImgX));
ImageRightLine = polyval(ImageRightVector, (1:ImgX));

imageCornerULx = fzero(@(x) polyval(ImageUpVector-ImageLeftVector,x),3);
imageCornerULy = polyval(ImageUpVector,imageCornerULx);

imageCornerURx = fzero(@(x) polyval(ImageUpVector-ImageRightVector,x),3);
imageCornerURy = polyval(ImageUpVector,imageCornerURx);

imageCornerDLx = fzero(@(x) polyval(ImageDownVector-ImageLeftVector,x),3);
imageCornerDLy = polyval(ImageDownVector,imageCornerDLx);

imageCornerDRx = fzero(@(x) polyval(ImageDownVector-ImageRightVector,x),3);
imageCornerDRy = polyval(ImageDownVector,imageCornerDRx);

%% plot results
subplot(subplotVal1,subplotVal2,subplotVal3);
hold on;
plot(ImageX,ImageY,'.');
plot(ImageUpX,ImageUpY,'.');
plot(ImageDownX,ImageDownY,'.');
plot(ImageLeftX,ImageLeftY,'.');
plot(ImageRightX,ImageRightY,'.');

plot(imageCornerULx,imageCornerULy,'r*');
plot(imageCornerURx,imageCornerURy,'r*');
plot(imageCornerDLx,imageCornerDLy,'r*');
plot(imageCornerDRx,imageCornerDRy,'r*');

plot((1:ImgX),ImageUpLine);
plot((1:ImgX),ImageDownLine);
plot((1:ImgX),ImageLeftLine);
plot((1:ImgX),ImageRightLine);

axis([0 ImgX 0 ImgY]);
axis ij

end