function B = boundaries(imageBinary, imageConnectivity , imageDirection)
    if nargin < 3
        imageDirection = 'clockWise';
    end
    if nargin < 2
        imageConnectivity = 8;
    end
    imageBinaryLabel = bwlabel(imageBinary, imageConnectivity );
    numberObjects = max(imageBinaryLabel(:));
    if numberObjects > 0
        B = cell(numberObjects, 1);
    else
        B = {};
    end
    imageLabelPad = padarray(imageBinaryLabel, [1 1], 0, 'both');
    imageLabelPadSize = size(imageLabelPad,1);
    if imageConnectivity == 8
        imagePadOffsets = [-1, imageLabelPadSize-1, imageLabelPadSize, imageLabelPadSize+1, 1, -imageLabelPadSize+1, -imageLabelPadSize, -imageLabelPadSize-1];
        nextSearchDirectionLookup = [8 8 2 2 4 4 6 6];
        searchDirectionLookup = [2 3 4 5 6 7 8 1];
    else
        imagePadOffsets = [-1, imageLabelPadSize, 1, -imageLabelPadSize];
        nextSearchDirectionLookup = [4 1 2 3];
        searchDirectionLookup=[2 3 4 1];
    end
    START = -1;
    BOUNDARY = -2;
    scratch = zeros(100, 1);
    [rr,cc] = find((imageLabelPad(2:end-1, :)>0)&(imageLabelPad(1:end-2, :) == 0));
    rr = rr+1;
    for k = 1:length(rr)
        r = rr(k);
        c = cc(k);
        if (imageLabelPad(r, c) > 0) && (imageLabelPad(r-1, c) == 0)&&isempty(B{imageLabelPad(r, c)})
            idx = (c-1)*size(imageLabelPad,1)+r;
            which = imageLabelPad(idx);
            scratch(1) = idx;
            imageLabelPad(idx) = START;
            numPixels = 1;
            currentPixel = idx;
            initial_departure_direction = [];
            done = false;
            next_search_direction = 2 ;
            while ~done
                imageDirection = next_search_direction;
                found_next_pixel = false;
                for k = 1:length(imagePadOffsets)
                    neighbor = currentPixel+imagePadOffsets(imageDirection);
                    if imageLabelPad(neighbor) ~= 0
                        if (imageLabelPad(currentPixel) == START)&&isempty(initial_departure_direction)
                            initial_departure_direction = imageDirection;
                        elseif (imageLabelPad(currentPixel) == START) && (initial_departure_direction == imageDirection)
                            done = true;
                            found_next_pixel = true;
                            break;
                        end
                        next_search_direction = nextSearchDirectionLookup(imageDirection);
                        found_next_pixel = true;
                        numPixels = numPixels+1;
                        if numPixels > size(scratch, 1)
                            scratch(2*size(scratch, 1)) = 0;
                        end
                        scratch(numPixels) = neighbor;
                        if imageLabelPad(neighbor) ~= START
                            imageLabelPad(neighbor) = BOUNDARY;
                        end
                        currentPixel = neighbor;
                        break;
                    end
                    imageDirection = searchDirectionLookup(imageDirection);
                end
                if ~found_next_pixel
                    numPixels = 2;
                    scratch(2) = scratch(1);
                    done = true;
                end
            end
            [imageRow,imageColumn] = ind2sub(size(imageLabelPad), scratch(1:numPixels));
            B{which} = [imageRow-1, imageColumn-1];
        end
    end
    if strcmp(imageDirection, 'counterClockWise')
        for k = 1:length(B)
            B{k} = B{k}(end:-1:1, :);
        end
    end
end rewrite this function but make sure it works the same