我想弄清楚如何创建一个直方图数组来比较 matlab 中图像梯度向量的大小和方向。我要使用 sobel masks 来找到梯度,到目前为止我有:
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');
现在我需要弄清楚如何创建直方图以将其与其他图像进行比较。
最佳答案
您可以将计算出的 gx 和 gy 矩阵视为长向量,然后将它们分组为一个梯度向量,其大小为:2 x (# number of gx 或 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);
关于matlab - matlab中的梯度直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795107/