3

我试图弄清楚如何创建一个直方图数组来比较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');

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

4

1 回答 1

6

您可以将计算出的gxgy矩阵视为长向量,然后将它们分组为大小为 2 x 的梯度向量(# number of elements in gx or gy

% create the gradient vectors
    grad_vector(1,:) = gx(:);
    grad_vector(2,:) = gy(:);

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

%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

然后可以通过决定如何将结果划分为多个 bin 来创建直方图。例如,您可以选择将方向划分为 4 个 bin,将幅度划分为 3,则:

% find histograms, dividing into appropriate bins
    histdir = hist(dir,4);
    histmag = hist(magn,3);
于 2013-02-10T05:51:14.127 回答