function [ dem_struct ] = demEdges( dem_struct, params )
disp('Get edge mask');

edge_threshold = params.edge_threshold;

dem = dem_struct.max_dem;
grid_size = size(dem);
[height,width] = size(dem);

dem = medfilt2(dem);
dem_grd = imgradient(dem);

%Get img/grad peaks
ispeak = false(size(dem));
for r=1:height
    [~, locs] = findpeaks(dem_grd(r,:));
    ispeak(r,locs) = true;
end
for c=1:width
    [~, locs] = findpeaks(dem_grd(:,c));
    ispeak(locs,c) = true;
end

grd_pks = dem_grd;
grd_pks(~ispeak)=0;

edges = grd_pks>edge_threshold;
%     edges = bwmorph(edges,'clean');
%     edges = bwmorph(edges,'fill');
%     edges = bwmorph(edges,'remove');
%     f1 = imfilter(double(edges),[1,0,1])==1;
%     f2 = imfilter(double(edges),[1;0;1])==1;
%     edges = edges | (f1&f2);
edges(1,:)      = true;
edges(end,:)    = true;
edges(:,1)      = true;
edges(:,end)    = true;

edges_mag = zeros(grid_size);
edges_mag(edges) = dem_grd(edges);

dem_struct.edges = edges;
dem_struct.edges_mag = edges_mag;
end

