Clear Filters
Clear Filters

How can I map this matrix into another coordinate system, so it can be added to a second matrix?

21 views (last 30 days)
I am trying to solve a problem which I may struggle to describe, so will attempt with the aid of the following picture (please bear with me!):
I have two matrices which are defined on different coordinate spaces (u,v) for matrix A and (x,y) for matrix B. They have different grid sizes and different numbers of pixels. My goal is to apply a scaling factor S to the matrix A, and then to simply add it to matrix B. (For context, this is an optical imaging problem, where matrix A is located at an object plane, matrix B is located at an image plane, and S is the magnification).
So, I would like to create a new matrix C which is the equivalent of A but brought into the new coordinates (x,y). Matrix C should have the same number of rows and columns as B.
A minimum example of A and B is shown below, where the red dashed lines on the right illustrate the effective physical regions occupied by matrix A's pixels:
This is produced by the following code:
%%% Inputs for matrix A %%%
M = 4; % num columns in matrix A
N = 4; % num rows in matrix A
du = 13; % horizontal size of a pixel in matrix A [mm]
dv = 13; % vertical size of a pixel in matrix A [mm]
%%% Set up matrix A %%%
Lu = (M-1)*du; % physical hor. coord. of centre of last pixel [mm]
Lv = (N-1)*dv; % physical ver. coord. of centre of last pixel [mm]
u = -Lu/2:du:Lu/2; % hor. coordinates for matrix A [mm]
v = -Lv/2:dv:Lv/2; % ver. coordinates for matrix A [mm]
A = zeros(N,M);
A(1,1) = 1; % Set a few values to 1 for testing
A(2,3) = 1;
A(3,4) = 1;
%%% Inputs for matrix B %%%
dx = 0.1; % grid step in matrix B [mm]
dy = 0.1; % grid step in matrix B [mm]
Lx = 6; % physical hor. coord. of centre of last pixel [mm]
Ly = 6; % physical ver. coord. of centre of last pixel [mm]
%%% Set up matrix B %%%
x = -Lx/2:dx:Lx/2;
y = -Ly/2:dy:Ly/2;
B = rand(length(y),length(x));
figure('color','w');
subplot(1,2,1);imagesc(u, v, A); axis equal tight;
subplot(1,2,2);imagesc(x, y, B); axis equal tight;
S = 1/20; % scale factor from matrix A's corrdinates to matrix B's
% C = ?
In this example, I have set the pixel size of matrix A to be 13mm, and the scaling factor to be 1/20. This means that in B's coordinates each pixel should be 13/20 = 0.65mm. This is bigger than the grid size dx=0.1mm, and so in this case the result should be that, after mapping, pixels should span multiple grid points. Any region outside the total extent of matrix A should be padded with zeros.
Is there a simple way (or built-in function) which would quickly generate matrix C (ideally without using loops over each pixel)?
Thank you!

Answers (1)

Gaurav Garg
Gaurav Garg on 10 Aug 2020
Edited: Gaurav Garg on 10 Aug 2020
Hi,
There are multiple functions you can refer to, to accomplish the task you are referring to. Kindly go through this link which would tell you about some functions useful to transform an image.
In your case, you should be able to scale the matrix A using imwarp or imresize. There are plenty of more functions available for your use referred in the documentation.
  2 Comments
teeeeee
teeeeee on 10 Aug 2020
Thank you for your response. Could you provide an example using imresize() following on from my existing MWE. I am not sure how to determine the scaling factor (number of pixels can be odd or even), or the interpolation method.
Thanks for your help.
Gaurav Garg
Gaurav Garg on 10 Aug 2020
Edited: Gaurav Garg on 10 Aug 2020
Hey,
You can follow the example given here for your case.
For more general examples, you can refer here.

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!