Matlab 求矩阵中给定方向的值
在Matlab中,我创建了一个矩阵,在代码的前一个阶段,选择了一个特定元素。从矩阵的这一点出发,我想找到一个最大值,不仅是给定半径的所有周围邻域之间的最大值,而且是给定方向角下的最大值。让我用一个例子来解释这一点: 这是矩阵A: A= 在第一阶段选择的元素是A(2,4)中的4,下一个元素应该是最大值,例如,方向角为315度,即A(5,7)中的7 我所做的是,根据角度,将矩阵A细分为不同的象限,并用该象限的值生成一个新的矩阵(A的子矩阵) 因此,在本例中,子矩阵将是A的第四象限: q_A= 现在,我的问题是,如何提取7Matlab 求矩阵中给定方向的值,matlab,image-processing,matrix,Matlab,Image Processing,Matrix,在Matlab中,我创建了一个矩阵,在代码的前一个阶段,选择了一个特定元素。从矩阵的这一点出发,我想找到一个最大值,不仅是给定半径的所有周围邻域之间的最大值,而且是给定方向角下的最大值。让我用一个例子来解释这一点: 这是矩阵A: A= 在第一阶段选择的元素是A(2,4)中的4,下一个元素应该是最大值,例如,方向角为315度,即A(5,7)中的7 我所做的是,根据角度,将矩阵A细分为不同的象限,并用该象限的值生成一个新的矩阵(A的子矩阵) 因此,在本例中,子矩阵将是A的第四象限: q_A= 现在,
我能做的唯一一件事(它是有效的)就是找到超过阈值的所有值,然后计算这些点的方向。然后,保存与给定方向(本例中为315度)具有相似方向的所有值,并最终查找其中的最大值。这是我的理论,但是我没有图像处理工具箱来测试它。也许有人可以评论
%make (r,c) the center by padding with zeros
if r > size(A,1)/2
At = padarray(A, [size(A,1) - r], 0, 'pre');
else
At = padarray(A, [r-1], 0 'post');
if c > size(A,2)/2
At = padarray(At, [size(A,2) - c], 0, 'pre');
else
At = padarray(At, [c-1], 0 'post');
%rotate by your angle (maybe this should be -angle or else 360-angle or 2*pi-angle, I'm not sure
Ar = imrotate(At,angle, 'nearest', 'loose'); %though I think nearest and loose are defaults
%find the max
max(Ar(size(Ar,1)/2, size(Ar,2)/2:end); %Obviously you must adjust this to handle the case of odd dimension sizes.
另外,根据您的阵列要求,使用
-inf
填充可能比0
更好。以下是解决此问题的一个相对便宜的解决方案,尽管我发现将头绕在矩阵坐标系上是一个真正的痛点,而且可能还有空间进行整理。它只是沿着起点周围的直线以提供的角度跟踪所有矩阵条目(所有坐标和角度当然基于矩阵索引单位):
您是否尝试过
imrotate
使用最近邻插值,以元素(r,c)为中心,使角度始终为0?然后它只是max(旋转(r,c:end))
旋转。。。听起来不错!我试试看。但是,我的问题是,我寻找的值应该在给定角度附近。也许,通过在给定的角度上下两个角度来做你所建议的事情可以奏效。谢谢大家!@karl71但是就像我说的,我没有工具箱来测试这些,所以我建议你运行每一行并检查输出,以确保它是有意义的。特别是因为它可能很容易被一行/列关闭等。
4 3 2 8 1 0
3 3 2 2 1 0
3 2 2 2 1 0
3 3 2 7 2 1
2 3 2 3 2 1
%make (r,c) the center by padding with zeros
if r > size(A,1)/2
At = padarray(A, [size(A,1) - r], 0, 'pre');
else
At = padarray(A, [r-1], 0 'post');
if c > size(A,2)/2
At = padarray(At, [size(A,2) - c], 0, 'pre');
else
At = padarray(At, [c-1], 0 'post');
%rotate by your angle (maybe this should be -angle or else 360-angle or 2*pi-angle, I'm not sure
Ar = imrotate(At,angle, 'nearest', 'loose'); %though I think nearest and loose are defaults
%find the max
max(Ar(size(Ar,1)/2, size(Ar,2)/2:end); %Obviously you must adjust this to handle the case of odd dimension sizes.
A = [ 0 1 1 1 0 0 9 1 0
0 2 2 4 3 2 8 1 0
0 2 2 3 3 2 2 1 0
0 1 1 3 2 2 2 1 0
0 8 2 3 3 2 7 2 1
0 1 1 2 3 2 3 2 1 ];
alph = 315;
r = 2;
c = 4;
% generate a line through point (r,c) with angle alph
[nr nc] = size(A);
x = [1:0.1:nc]; % overkill
m = tan(alph);
b = r-m*c;
y = m*x + b;
crd = unique(round([y(:) x(:)]),'rows');
iok = find((crd(:,1)>0) & (crd(:,1)<=nr) & (crd(:,2)>0) & (crd(:,2)<=nc));
crd = crd(iok,:);
indx=sub2ind([nr,nc],crd(:,1),crd(:,2));
% find max and position of max
[val iv]=max(A(indx)); % <-- val is the value of the max
crd(iv,:) % <-- matrix coordinates (row, column) of max value
val =
7
iv =
8
ans =
5 7