Skip to content

Instantly share code, notes, and snippets.

@arccoder
Last active January 16, 2017 02:35
Show Gist options
  • Save arccoder/c5c0818acebc588d84392c3a20743076 to your computer and use it in GitHub Desktop.
Save arccoder/c5c0818acebc588d84392c3a20743076 to your computer and use it in GitHub Desktop.
Edge detection example explained using 1D toy example.
clc; clear all; close all;
% Data Generation
x = 0:10:2000;
y = sigmf(x,[0.1 1000]);
n = rand(1,201)/40 - 0.0125;
f = y;% + n;
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 8 2]);
plot(x,f), ylim([-0.2 1.2]), grid on;
saveas(fig,'signal.png');
% Filter Generation
h = fspecial('gaussian', [1 21], 2.5);
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 2 2]);
plot(0:20,h), grid on
saveas(gcf,'filter.png')
% Derivative of filter
dh = h(2:end) - h(1:end-1);
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 2 2]);
plot(0:19,dh), grid on;
saveas(gcf,'dfilter.png')
% Second order derivative of filter
d2h = dh(2:end) - dh(1:end-1);
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 2 2]);
plot(0:18,d2h), grid on;
saveas(gcf,'d2filter.png')
% Conv data and filter
hf = conv(f,h,'same');
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 8 2]);
plot(0:200,hf), ylim([-0.2 1.2]), grid on;
saveas(gcf,'convdatafilter.png')
% Derivative of a conv
dhf1 = hf(2:end) - hf(1:end-1);
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 8 2]);
plot(0:199,dhf1), grid on;
saveas(gcf,'dconvdatafilter.png')
% Conv data and differentiated filter
dhf = conv(f,dh,'same');
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 8 2]);
plot(0:200,dhf), grid on;
saveas(gcf,'convdatadfilter.png')
% Conv data and second order differentiated filter
d2hf = conv(f,d2h,'same');
fig = figure;
set(fig, 'PaperUnits', 'inches');
set(fig, 'PaperPosition', [0 0 8 2]);
plot(0:200,d2hf), grid on;
saveas(gcf,'convdatad2filter.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment