计算3D矩阵的梯度 [英] calculating the gradient of a 3D matrix

查看:444
本文介绍了计算3D矩阵的梯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D矩阵,想计算该矩阵的梯度.我要做的唯一步骤是为2D图像定义图像梯度,如下所示:

I have a 3D matrix and want to calculate the gradient for this matrix. The only step which I did is defining image gradient for a 2D image as 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);

我想知道如何将这段代码扩展到3D吗?我知道在3D矩阵中我应该使用convn,但不知道该如何更改sx,sy和使sz.

I am wondering that how I can extend this code to 3D? I know that in 3D matrix I should use convn, but do not know that how I should change the sx, sy and make sz.

推荐答案

如果要计算3D渐变,则还必须将内核设为3D.您不仅要检查矩阵的i th 切片中的变化,还需要检查(i-1) th 切片和(i + 1 )个切片.

If you want to calculate a 3D gradient, you would have to make your kernel 3D as well. Not only are you checking for changes in the ith slice of your matrix, but you also need to check the (i-1)th slice and the (i+1)th slice as well.

作为次要注释,sy应该是sx的转置,但是这里没有.在执行任何操作之前,请确保先更改此设置.

As a minor comment, sy should be the transpose of sx, but you don't have that here. Make sure you change this before you do anything.

此外,随着维度的增加,您还有 3 个可能的内核,因为您需要水平检查每个切片中的变化,垂直检查每个切片中的变化或临时.这样,您只需通过沿第三维复制sx三次,或沿第三维复制sy三次,或制作其中第一个切片全为1的时态内核,第二个切片来制作sz全为零,第三个切片全为-1.因此,调用所有这些内核szxszyszz,您可以将它们创建为:

Also, with the added dimension, you have three more possible kernels because you need to check for changes horizontally in each slice, vertically in each slice, or temporally. As such, you would simply make sz either by replicating sx along the third dimension three times, or replicating sy along the third dimension three times or making a temporal kernel where the first slice is all 1, the second slice is all zero and the third slice is all -1. Therefore, calling all of these kernels szx, szy and szz, you would 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));

要计算幅度,您只需将所有这些分量的幅度一起找到,并按照建议使用convn:

To compute the magnitude, you would simply find the magnitude of all of these components together and use convn as you 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);

关于角度,这非常模棱两可,因为您现在具有三个分量,并且如果要查找角度,则需要在3D空间中具有两个矢量并在这两个矢量之间找到角度.对于2D,这已经定义好了,因为您已经创建了2个矢量,因此角度仅是垂直和水平分量之间的arctan.因为您具有三个分量,并且有两个分量可供选择,所以您可以计算三个可能的角度,每个相对于xyz轴.

As for the angle, this is very ambiguous because you now have three components, and if you want to find the angle, you would need to have two vectors in 3D space and find the angle in between those two vectors. For 2D this is well defined as you have already created 2 vectors, and so the angle is simply the arctan between the vertical and horizontal components. Because you have three components, and there are two to choose from, there are three possible angles you can compute, each with respect to either the x, y or z axis.

在这种情况下,我这里没有答案,但是来自Math StackExchange的这个特定示例可能使您进一步了解如何使用3D渐变来计算角度.重要的一点是,您需要知道要测量相对于哪个轴的角度.

I don't have an answer for you here in this case, but this particular example from Math StackExchange may give you further insight on how to calculate the angle using the 3D gradient. The important point is that you need to know which axis you are measuring the angle with respect to.

https://math.stackexchange.com/questions/569019/如何获得3d梯度方向和幅度

祝你好运!

这篇关于计算3D矩阵的梯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆