O.T*_*.T. 2 matlab image-processing rotation
我有一个一维数字向量,它代表圆形对称物体的中心切割。向量本身围绕其中心元素对称。我想在 MATLAB 中通过围绕其中心元素旋转 1D 向量来创建原始对象的 2D 图像。
我尝试了以下代码(使用数字的虚拟原始向量),但是从生成的 2D 图像中获得的中心切割与原始 1D 向量不匹配,如果运行代码就可以看到。我将不胜感激任何帮助!
close all; clc
my1D_vector=[ zeros(1,20) ones(1,15) zeros(1,20)]; % original vector
len=length(my1D_vector);
img=zeros(len, len);
thetaVec=0:0.1:179.9; % angles through which to rotate the original vector
numRotations=length(thetaVec);
% the coordinates of the original vector and the generated 2D image:
x=(1:len)-(floor(len/2)+1);
y=x;
[X,Y]=meshgrid(x,y);
for ind_rotations=1:numRotations
theta=pi/180*thetaVec(ind_rotations);
t_theta=X.*cos(theta)+Y.*sin(theta);
cutContrib=interp1(x , my1D_vector , t_theta(:),'linear');
img=img+reshape(cutContrib,len,len);
end
img=img/numRotations;
figure('name','original vector');plot(x,my1D_vector,'b.-')
figure('name','generated 2D image'); imagesc(x,y,img); colormap(gray) ;
figure('name','comparison between the original vector and a center cut from the generated 2D image');
plot(x,my1D_vector,'b.-')
hold on
plot(x,img(find(x==0),:),'m.-')
legend('original 1D vector','a center cut from the generated 2D image')
Run Code Online (Sandbox Code Playgroud)
我没有遵循你的代码,但是这个怎么样:
V = [ zeros(1,20) ones(1,15) zeros(1,20)]; % Any symmetrical vector
n = floor(numel(V)/2);
r = [n:-1:0, 1:n]; % A vector of distance (measured in pixels) from the center of vector V to each element of V
% Now find the distance of each element of a square 2D matrix from it's centre. @(x,y)(sqrt(x.^2+y.^2)) is just the Euclidean distance function.
ri = bsxfun(@(x,y)(sqrt(x.^2+y.^2)),r,r');
% Now use those distance matrices to interpole V
img = interp1(r(1:n+1),V(1:n+1),ri);
% The corners will contain NaN because they are further than any point we had data for so we get rid of the NaNs
img(isnan(img)) = 0; % or instead of zero, whatever you want your background colour to be
Run Code Online (Sandbox Code Playgroud)
因此,我不是对角度进行插值,而是对半径进行插值。Sor表示 1D 中距 , 的每个元素中心的距离的向量V。ri然后表示距二维中心的距离,这些是我们要插值的值。然后我只使用r和的一半,V因为它们是对称的。
您可能希望将所有NaNs 设置为0之后,因为您无法对角进行插值,因为它们的半径大于 中最远点的半径V。
使用你的绘图代码我得到
和
蓝色和洋红色曲线完全重叠。
想象一下你的向量 ,V只是一个1逐个5向量。那么这张图显示了r和ri将会是什么:
我没有在图表上标记它,但r'会在中间一列。现在从代码中我们可以得到
ri = bsxfun(@(x,y)(sqrt(x.^2+y.^2)),r,r');
Run Code Online (Sandbox Code Playgroud)
根据该图,r = [2,1,0,1,2]现在对于每个像素,我们都有距中心的欧几里德距离,因此像素 (1,1) 将sqrt(r(1).^2+r(1).^2)如图sqrt(2.^2+2.^2)所示sqrt(8)。
您还会注意到我已将角像素“变灰”。这些像素比我们拥有数据的任何点距离中心更远,因为我们数据的最大半径(距中心的距离)是,2但sqrt(8) > sqrt(5) > 2因此您无法对这些数据进行插值,您必须进行推断才能获取它们的值。这些点的interp1回报。NaN
为什么插值有效?将每个像素的位置视为参考像素的中心。现在,在此图中,红色圆圈是旋转外部元素(即r==2)时发生的情况,绿色圆圈是进一步旋转元素 1 (即r==1)时发生的情况。当我们旋转它们时,您会看到获得(蓝色箭头)距离的像素sqrt(2)位于这两个半径之间,因此我们必须在这两个像素之间插入该距离。