我的2D数据存储在[K,K]
矩阵中。索引表示由其应变(q_1, q_2)
定义的倾斜坐标系中的坐标-0.5<gamma<0.5
。目标是将数据转换为直角坐标系,该坐标系由坐标:
q_x = q_1
q_y = q_2 - gamma*q_1
结果如下图所示:
下面的代码在逐个像素的基础上实现了这种转换。有人会碰巧知道更优雅和矢量化的方法获得相同的结果吗?
% Oblique-to-rectangular coordinate transformation
K = 10; % number of pixels
gamma = 0.37; % some arbitrary strain position range (-0.5; 0.5)
Koffset = (1-(-1).^(K-1))/4; % =0.5 when K is even, =0.0 when K is odd
% Mock data
S0 = rand(K,K); % data collected in the oblique coordinate system
qindex = -ceil((K-1)/2) : floor((K-1)/2); % all the possible q-values, with the zero'th element in the middle. Must be in this order to comply with FFT's convention
S = zeros(K,K); % data to be transformed to the rectangular coordinate system
% let indices (i,j) run through all the positions of the oblique matrix
for i=1:K
for j=1:K
% obtain the q-values corresponding to the current matrix position (i,j)
q1 = qindex(i);
q2 = qindex(j);
% apply the coordinate transformation to get the q-values in the rectangular system
qx = round(q1);
qy = round(q2-gamma*q1);
% apply periodic boundary condition
qy = qy - K*round((qy+Koffset)/K); % should be a unique value in the range of qindex
% find out the indices in the rectangular system
ii = i;
jj = find(qindex == qy);
% add the element
S(ii,jj) = S(ii,jj) + S0(i,j);
end
end
答案 0 :(得分:1)
执行此操作的最佳方法是使用meshgrid
创建点网格,使用转换使网格变形,然后使用interp2
在这些位置对原始图像进行采样。
% Desired output range
[xx,yy] = meshgrid(-3:0.01:3, -3:0.01:3);
% Transform these X and Y coordinates to q1 and q2 coordinates
q1 = xx;
q2 = yy + gamma*q1;
% Sample the original image using these coordinates where q1range and q2
% range and the q1 and q2 values corresponding to each element in the image qdata
output = interp2(q1range, q2range, qdata, q1, q2);