如何通过围绕中心元素旋转一维数字矢量来创建2D图像? [英] How to create a 2D image by rotating 1D vector of numbers around its center element?

查看:83
本文介绍了如何通过围绕中心元素旋转一维数字矢量来创建2D图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字矢量,代表圆形对称对象的中心切割.向量本身围绕其中心元素对称.我想在MATLAB中通过围绕其中心元素旋转1D向量来创建原始对象的2D图像.

I have 1D vector of numbers which represents a center cut of a circular symmetric object. The vector itself is symmetric around its center element. I want to create in MATLAB a 2D image of the original object by rotating the 1D vector around its center element.

我尝试了以下代码(使用虚拟的原始数字矢量),但是从生成的2D图像中获得的中心切割与原始1D矢量不匹配,如运行代码所示. 我将不胜感激!

I tried the following code (with a dummy original vector of numbers) , but the center cut I get from the generated 2D image does not match the original 1D vector, as can be seen if you run the code. I'll appreciate any help !!!

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')

推荐答案

我没有遵循您的代码,但是如何做:

I didn't follow your code but how about this:

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

因此,我不是在角度上进行插值,而是在半径上进行插值.因此,r表示一维中距V的每个元素的中心的距离的向量.然后ri代表2D中距中心的距离,这些是我们要插值的值.然后,我只使用rV的一半,因为它们是对称的.

So instead of interpolating on the angle, I interpolate on the radius. So r represents a vector of the distance from the centre of each of the elements of V, in 1D. ri then represents the distance from the centre in 2D and these are the values we want to interpolate to. I then only use half of r and V because they are symmetrical.

之后您可能想将所有NaN都设置为0,因为您无法对角进行插值,因为它们的半径大于您在V中最远点的半径.

You might want to set all the NaNs to 0 afterwards because you can't interpolate the corners because their radius is larger than the radius of your furthest point in V.

使用您得到的绘图代码

蓝色和品红色曲线完全重叠.

The blue and magenta curves overlap exactly.

想象一下,您的向量V只是一个1 -by- 5向量.然后,此图显示了rri将是什么:

Imagine that your vector, V, was only a 1-by-5 vector. Then this diagram shows what r and ri would be:

我没有在图表上标记它,但是r'是中间一栏.现在从代码中获取

I didn't mark it on the diagram but r' would be the middle column. Now from the code we have

ri = bsxfun(@(x,y)(sqrt(x.^2+y.^2)),r,r');

并根据图r = [2,1,0,1,2],因此对于每个像素,我们具有距中心的欧式距离,因此像素(1,1)将变为sqrt(r(1).^2+r(1).^2),即sqrt(2.^2+2.^2),即sqrt(8),如图所示在图中.

and according to the diagram r = [2,1,0,1,2] so now for each pixel we have the euclidean distance from the centre so pixel (1,1) is going to be sqrt(r(1).^2+r(1).^2) which is sqrt(2.^2+2.^2) which is sqrt(8) as shown in the diagram.

此外,您还会注意到我变灰"了四角像素.这些像素比我们拥有数据的任何点都离中心更远,因为我们数据的最大半径(距中心的距离)为2sqrt(8) > sqrt(5) > 2,因此您无法内插这些数据,您必须外推到为他们获取价值. interp1针对这些点返回NaN.

Also you will notice that I have "greyed" out the corners pixels. These pixels are further from the centre than any point we have data for because the maximum radius (distance from the centre) of our data is 2 but sqrt(8) > sqrt(5) > 2 so you can't interpolate those data, you would have to extrapolate to get values for them. interp1 returns NaN for these points.

为什么插值有效?将每个像素的位置视为指向像素的中心.现在在此图中,红色圆圈是当您旋转外部元素(即r==2)而绿色正在进一步旋转元素1(即r==1)时发生的情况.您会看到,在旋转两个半径时,获得sqrt(2)(蓝色箭头)距离的像素位于这两个半径之间,因此我们必须对这两个像素之间的距离进行插值.

Why does the interpolation work? Think of the position of each pixel as referring to the centre of the pixel. Now in this diagram, the red circle is what happens when you rotate the outer elements (i.e. r==2) and the green is rotating the elements 1 further in (i.e. r==1). You'll see that the pixel that gets the distance of sqrt(2) (blue arrow) lies between these two radii when we rotate them and so we have to interpolate that distance between those two pixels.

这篇关于如何通过围绕中心元素旋转一维数字矢量来创建2D图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆