function [imseg,fn] = gen_im_segs(im,nseg,im_dir,im_file_name)
    
imseg = cell(nseg);
fn = cell(size(nseg));
[h,~] = size(im);
dh = floor(h/nseg(1));
hu = zeros(nseg(1),1);
hb = zeros(nseg(1),1);
for i = 1:nseg
    hu(i) = 1+(i-1)*dh;
    hb(i) = i*dh;
    if i == nseg(1)
        imseg{i} = im(hu(i):end,:,:);
    else
        imseg{i} = im(hu(i):hb(i),:,:);
    end
    fn{i} = sprintf(sprintf('%s/%s_seg_b_%d.png',im_dir,im_file_name,i));
    imwrite(imseg{i},fn{i});
    imseg{i} =rgb2gray(imseg{i});
end

