【问题标题】:Oblique to rectangular coordinate basis transformation of a matrix矩阵的斜向直角坐标变换
【发布时间】:2017-02-06 15:40:59
【问题描述】:

我的二维数据存储在[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

【问题讨论】:

    标签: matlab vectorization coordinate-systems coordinate-transformation


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2018-08-21
      • 2022-01-17
      • 2016-02-28
      相关资源
      最近更新 更多