matlab中的梯度直方图

Fra*_*ank 3 matlab image-processing histogram

我试图弄清楚如何创建一个直方图数组来比较matlab中图像的梯度向量的大小和方向.我将使用sobel面具找到渐变,到目前为止我有:

sobel_x = [-1 -2 -1;0  0  0;1  2  1];
sobel_y = [-1  0  1;-2  0  2;-1  0  1];

gx = filter2(sobel_x,im,'same');
gy = filter2(sobel_y,im,'same');
Run Code Online (Sandbox Code Playgroud)

现在我需要弄清楚如何创建直方图以将其与其他图像进行比较.

sal*_*cia 6

您可以将计算出的gxgy矩阵作为长向量处理,然后将它们分组为大小为2 x的梯度向量(#xgy中的元素数)

% create the gradient vectors
    grad_vector(1,:) = gx(:);
    grad_vector(2,:) = gy(:);
Run Code Online (Sandbox Code Playgroud)

然后你可以通过各种方式找到每个梯度向量的大小和方向,例如:

%find magnitude and direction of each gradient vector
    for i=1:size(grad_vector,2);
       magn(i) = norm(grad_vector(:,i));
       dir(i) = atand(grad_vector(2,i)/grad_vector(1,i));
    end
Run Code Online (Sandbox Code Playgroud)

然后可以通过决定如何将结果分成多个箱来创建直方图.例如,您可以选择将方向划分为4个区间,将大小划分为3个,然后:

% find histograms, dividing into appropriate bins
    histdir = hist(dir,4);
    histmag = hist(magn,3);
Run Code Online (Sandbox Code Playgroud)