matlab - calculating the gradient of a 3D matrix -
i have 3d matrix , want calculate gradient matrix. step did defining image gradient 2d image follow:
sx = [-1 0 1; -2 0 2; -1 0 1]; sy = [1 2 1; 0 0 0; -1 -2 -1]; gx = conv2(input, sx, 'same'); gy = conv2(input, sy, 'same'); grdmg = sqrt(gx.^2+gy.^2); grddr = atan2(gy,gx);
i wondering how can extend code 3d? know in 3d matrix should utilize convn, not know how should alter sx, sy , create sz.
if want calculate 3d gradient, have create kernel 3d well. not checking changes in ith piece of matrix, need check (i-1)th piece , (i+1)th piece well.
as minor comment, sy
should transpose of sx
, don't have here. create sure alter before anything.
also, added dimension, have three more possible kernels because need check changes horizontally in each slice, vertically in each slice, or temporally. such, create sz
either replicating sx
along 3rd dimension 3 times, or replicating sy
along 3rd dimension 3 times or making temporal kernel first piece 1, sec piece 0 , 3rd piece -1. therefore, calling of these kernels szx
, szy
, szz
, create them as:
szx = cat(3, sx, sx, sx); szy = cat(3, sy, sy, sy); szz = cat(3, ones(3,3), zeros(3,3), -ones(3,3));
to compute magnitude, find magnitude of of these components , utilize convn
have suggested:
gx = convn(input, szx, 'same'); gy = convn(input, szy, 'same'); gz = convn(input, szz, 'same'); grdmg = sqrt(gx.^2 + gy.^2 + gz.^2);
as angle, ambiguous because have 3 components, , if want find angle, need have 2 vectors in 3d space , find angle in between 2 vectors. 2d defined have created 2 vectors, , angle arctan
between vertical , horizontal components. because have 3 components, , there 2 take from, there 3 possible angles can compute, each respect either x
, y
or z
axis.
i don't have reply here in case, particular illustration math stackexchange may give farther insight on how calculate angle using 3d gradient. of import point need know axis measuring angle respect to.
http://math.stackexchange.com/questions/569019/how-do-you-get-3d-gradient-direction-and-magnitude
good luck!
matlab image-processing edge-detection
No comments:
Post a Comment