矩阵的斜向直角坐标基变换

Oblique to rectangular coordinate basis transformation of a matrix

我将二维数据存储在 [K,K] 矩阵中。索引表示由其应变 -0.5<gamma<0.5 定义的倾斜坐标系中的坐标 (q_1, q_2)。目标是将数据转换为直角坐标系,由坐标给出:

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

执行此操作的最佳方法是使用 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);