Matlab中有符号和无符号梯度方向的计算
在计算梯度方向以用于HOG描述符提取时,我们可以选择使用0-180或0-360之间的梯度方向,如何使用Matlab生成此类角度?我有以下代码:Matlab中有符号和无符号梯度方向的计算,matlab,image-processing,computer-vision,feature-descriptor,Matlab,Image Processing,Computer Vision,Feature Descriptor,在计算梯度方向以用于HOG描述符提取时,我们可以选择使用0-180或0-360之间的梯度方向,如何使用Matlab生成此类角度?我有以下代码: Im=imread('cameraman.tif'); // reading the image Im=double(Im); // converting it to double hx = [-1,0,1]; // the gradient kernel for x direction hy = -hx; // the gradient
Im=imread('cameraman.tif'); // reading the image
Im=double(Im); // converting it to double
hx = [-1,0,1]; // the gradient kernel for x direction
hy = -hx; // the gradient kernel for y direction
grad_x = imfilter(Im,hx); //gradient image in x direction
grad_y = imfilter(Im,hy); // gradient image in y direction
% angles in 0-180:
...
%angles in 0-360:
...
通常,您可以使用生成介于-180和180之间的角度,给定水平和垂直渐变分量,从而生成有符号的角度。但是,如果希望角度从0到360或无符号角度,则只需搜索由atan2
生成的负角度,然后将360添加到每个角度。这将允许您获取[0360)
之间的角度。例如,-5度的角度实际上是355度的无符号角度。因此:
angles = atan2(grad_y, grad_x); %// Signed
angles(angles < 0) = 2*pi + angles(angles < 0); %// Unsigned
根据你的评论,你也希望做相反的事情。基本上,如果给我们一个无符号的角度,我们如何得到一个有符号的角度?只需做相反的事情。找到所有
>180
的角度,然后取这个角度减去360。例如,182个无符号的角度是-178有符号的,或者182-360=-178
因此:
angles = atan2(grad_y, grad_x); %// Signed
angles(angles < 0) = 2*pi + angles(angles < 0); %// Unsigned
弧度
度
非常感谢您的快速回答,我想问一下,如果我想将角度从0-360映射到0-180,该怎么办?@Apastrix-这是完全相反的:)搜索
>180
的任何角度,然后减去360度。@Apastrix-我已经更新了我的帖子来做相反的事情。看一看。我很不好意思在你的回答之后问你这个问题e帮助,但在我的评论中,我不想要相反的结果,我想从0-360的角度开始,到0-180的角度,所以我需要一个映射,其中0保持为0,360变为180。如果你从0-360到0-180,你忽略了构成笛卡尔空间下半部分的角度。181到360之间的角度会发生什么(不包括在内)?如何映射这些功能?您还可以使用计算机视觉系统工具箱中的extractHOGFeatures
功能。
angles(angles > pi) = angles(angles > pi) - (2*pi);
angles(angles > 180) = angles(angles > 180) - 360;