-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemoGradient.m
66 lines (50 loc) · 1.26 KB
/
demoGradient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%DEMOGRADIENT Compute gradient on a planar image
%
% output = demoGradient(input)
%
% Example
% demoGradient
%
% See also
%
% ------
% Author: David Legland
% e-mail: david.legland@inra.fr
% Created: 2017-09-29, using Matlab 9.3.0.713579 (R2017b)
% Copyright 2017 INRA - Cepia Software Platform.
%% Read image, and compute norm of gradient
img = Image.read('coins.png');
% display original image
figure;
show(img);
title('original image');
% compute gradient, using default options
G = gradient(img);
GNorm = norm(G);
% display norm of gradient
figure;
show(GNorm);
title('norm of gradient image');
%% Display gradient components
% Extract gradient components
GX = channel(G, 1);
GY = channel(G, 2);
% display normalized components
figure; show(GX, [-50 50]);
title('Gradient X');
figure; show(GY, [-50 50]);
title('Gradient Y');
%% Display orientation weighted by norm
% compute orientation (between -pi and +pi)
ang = angle(G);
% create HSV image representing gradient
hue = (ang / (2*pi) + .5);
val = GNorm / max(GNorm(:));
sat = Image.ones(size(img), 'double');
% convert to RGB for display
hsv = cat(4, hue, sat, val); % channel index for Image class is 4.
rgb = hsv2rgb(hsv);
% display gradient orientation
figure;
show(rgb);
title('Gradient orientation');