You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: convert_dataset.m
    path of file: ./repos/nyu-depth-v2-tools
    
    the code of the file until where you have to start completion: function [] = convert_dataset(dir, factor)
% function [] = convert_dataset()
%
% Converts the NYU Depth V2 dataset to be BSDS500 conform. Therefore, the
% images are halfed in size and split into a training and test set
% according to list_train.txt and list_test.txt.
%
% The output directory can be adjusted by having a look at the functions
% generate_half, generate_depth and
% generate_groundtruth_medfilt
    
function generate_groundtruth_medfilt(labels, instances, mask_border, list, outdir, factor)
% function generate_groundtruth_medfilt(labels, instances, mask_border, list, outdir, factor)
%
% This function is used to find and fill regions of the depth images that
% require filling.
%
% The factor defines the resizing factor for the images.

    if ~exist(outdir)
        system(['mkdir -p ' outdir]);
    end;

    maxlabel = 1000;
    assert(max(labels(:)) <= maxlabel);
    
    for ii=list'
      id = num2str(ii, '%08d');
      
      % find out the region that needs to be filled, use dilate/erode
      margin = 5;
      foreground = (labels(:, :, ii) > 0);
      foreground = imerode(imdilate(foreground, strel('disk', margin)), strel('disk', margin));
      cc = bwconncomp(~foreground);
      A = regionprops(cc, 'Area');
      min_background_area = 500;
      indA = find([A.Area] < min_background_area);
      
      for k = indA
          foreground(cc.PixelIdxList{k}) = 1;
      end