以一定角度旋转矩阵

4
下午浪费在以下问题上:
我有一个深度值矩阵,位于某个空间域中。我在该域中定义一条线,并擦除该线下方的值。
代码使用findnearest函数查找数组中距离某个值最近的元素的索引。
clear all
close all

dx = 5;
dz = 1;

%angle between line and ground
a=atan(0.05);

%%%define domain
xi = 0:dx:20e3;
zi = 0:dz:1000;
m=length(zi);
n=length(xi);

%create grid
[x,z] = meshgrid(xi,zi);

%z where line starts
zs = 700;
%set line
 for i = 1:findnearest(xi,zi(zs)*1/a)
  xind(i) = i;
  zind(i) = findnearest(zi, fix(-xi(i)*a +zi(zs))); 
 end


depth = repmat(zi',1,n); %simply the coordinate zi repeated for all xi

 %calculate distance from the line
 for ii=1:n %for every x

    zslope = -a*xi(ii)+zi(zs);%equation of the line

    zz(ii)=zslope;
   if zslope>=0 %if the line is still in the domain (z>0)
     for jj=1:m %for every z

       if zi(jj)>=zslope %above the line

         Zs(jj,ii) = zi(jj)-zslope; %height above the line

        elseif zi(jj)<zslope %below the line (ground)
%            
          Zs(jj,ii)=NaN; 

       end
     end%for on z

   elseif zslope<0 %the line is no longer in the domain

       for jj=1:m %for every z

          Zs(jj,ii) = zi(jj)-zslope; %height above the line

       end
   end
end%for on x 

figure
imagesc(Zs)
colorbar
title('distance from the line')

%zone above the line
maskINT=zeros(m,n);
inds = find(Zs>=0); %erase values under the line
maskINT(inds)=1;


figure
imagesc(depth);colorbar
title('depth')

figure
imagesc(depth.*maskINT);colorbar
title('depth  above the line')

figure
contour(depth.*maskINT);colorbar
set(gca,'YDir','Reverse')
title('depth')

生成的深度矩阵如下所示:

depth

使用contour表示如下:

depth_c

我想将深度矩阵旋转一个角度(-pi/2-a?)或对其应用某些变换,使深度轮廓垂直于与第一条线平行的线:

idea

我尝试了各种旋转矩阵,但效果不佳...

1
你尝试过简单地使用 imrotate(depth,angleInDegrees) 吗? - Leander Moesinger
'findnearest' 函数是什么? - Mendi Barel
@MendiBarel 我已经编辑了问题,抱歉。 - shamalaia
@LeanderMoesinger 那不起作用,因为-一旦旋转-矩阵就会有一些空白区域。 - shamalaia
1个回答

1
我没有完整的答案,但是我通过一些调整找到了解决这个问题的方法。
我所做的:
当你有分割线时,那些都是重要的数值。对于旋转区域内的点,我找到了沿着x方向的数值,并将其从原始深度矩阵中复制出来。为了简单起见,我让x数据上的值与y具有相同的距离。
在这些调整之后,我得到了这个: this 请注意,我还对我所做的事情进行了注释。
然而,在您的代码中,X/Y比率不相同。如果我只是将我的代码部分复制到您的代码中,由于您的角度约为0,我会得到垂直线条,尽管在图像中接近45度。 为了进行适当的调整,您需要实际按值而不是索引进行比较,就像我所做的那样。
以下是经过调整的代码(用于比较,请查找%ADDED注释):
clear all
close all

dx = 1; %ADDED
dz = 1;

%angle between line and ground
angle=pi/3;  %ADDED
a=atan(angle);

%%%define domain
xi = 0:dx:1000;%ADDED
zi = 0:dz:1000;%ADDED
m=length(zi);
n=length(xi);



%%%ADDED %prealocating
Zs=zeros(m,n);
Zs2=zeros(m,n);
depth2=zeros(m,n);
zz=zeros(1,n);



%create grid
[x,z] = meshgrid(xi,zi);

%z where line starts
zs = 700;
%set line
for i = 1:findnearest(xi,zi(zs)*1/a)
    xind(i) = i;
    zind(i) = findnearest(zi, fix(-xi(i)*a +zi(zs)));
end


depth = repmat(zi',1,n); %simply the coordinate zi repeated for all xi

%calculate distance from the line
for ii=1:n %for every x

    zslope = -a*xi(ii)+zi(zs);%equation of the line

    zz(ii)=zslope;
    if zslope>=0 %if the line is still in the domain (z>0)
        for jj=1:m %for every z

            if zi(jj)>=zslope %above the line

                Zs(jj,ii) = zi(jj)-zslope; %height above the line

            elseif zi(jj)<zslope %below the line (ground)
                %
                Zs(jj,ii)=NaN;

                %ADDED
                Zs2(jj,ii)=abs(zi(jj)-zslope);%height above the line
                depth2(jj,ii)= depth(jj+round(Zs2(jj,ii)*cos(angle)),ii);

            end
        end%for on z

    elseif zslope<0 %the line is no longer in the domain

        for jj=1:m %for every z

            Zs(jj,ii) = zi(jj)-zslope; %height above the line

        end
    end
end%for on x

figure
imagesc(Zs)
colorbar
title('distance from the line')


%ADDED
figure
imagesc(depth2)
colorbar
title('depth2')


%zone above the line
maskINT=zeros(m,n);
inds = find(Zs>=0); %erase values under the line
maskINT(inds)=1;

%ADDED
figure
imagesc(depth2+depth.*maskINT)
colorbar
title('depth summed')


figure
imagesc(depth);colorbar
title('depth')

figure
imagesc(depth.*maskINT);colorbar
title('depth  above the line')

figure
contour(depth.*maskINT);colorbar
set(gca,'YDir','Reverse')
title('depth')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接