2D matrix resize or interpolation

25 views (last 30 days)
Russell
Russell on 10 Jan 2025
Commented: Cris LaPierre on 27 Jan 2025
Hello,
I'm new to matlab and I'm not sure what would be the best way to do the following.
I have an image/data matrix, I, of size (rows,cols) = (y,x) = [384,512].
The spatial resolution in the x direction is 0.078021 cm (total span is ~40cm)
The spatial resolution in the y direction is 0.078021 (total span is ~30cm)
Q1.
If I would like to have a finer sampling of the data by doubling the samples (eg Inew = [768, 1024], which function would you suggest?
Q2.
If I had another separate image but with offset x,y values, how would I interpolate this data onto the same grid as the other matrix?
Thank you for your help,
RR
ps. I found some info on the interp2 function? not sure if this is the best function to use?
ps. I have attached a zip file with
  • dicom image (of a radiotherapy dose on a single plane - my image)
  • excel workbook with the data extracted
  • an m script with what I tried to do but I got an error
Error using griddedInterpolant
The grid must be created from grid vectors which are strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 228)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 136)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in LoadDosePlaneBailey2D (line 70)
Id_cgy_q = interp2(X,Y,Id_cgy,Xq,Yq);

Answers (3)

Cris LaPierre
Cris LaPierre on 10 Jan 2025
Edited: Cris LaPierre on 10 Jan 2025
Q2: What type of images are you working with? Please share an example. You can attach images to your post using the paperclip icon.
  16 Comments
Cris LaPierre
Cris LaPierre on 26 Jan 2025
Edited: Cris LaPierre on 27 Jan 2025
I'm defnitely not an expert in this space, so I'm still confused. Based on the metadata in the dsf files
  • Image_measured_30x40.dxf is actually 21.5x28.7
  • Image_prediction1_43x43.dxf is actually 31.1x31.1
  • Image_prediction2_30x40.dxf is actually 21.4x28.5
It looks like you have a 2-step process - first to extract the desired region using indexing, then resizing. Given that, I think the best approachis to use imresize on the extracted iage. Pick the approprite method for your task.
Cris LaPierre
Cris LaPierre on 27 Jan 2025
Heres a function that will read in your dxf files. Not necessarily the best code, but does the job.
unzip Image_prediction1_43x43.zip
params = readDXF('Image_prediction1_43x43.dxf');
figure
imagesc(params.Pixels)
Xsz = params.Size1*params.Res1
Xsz = 311.4286
Ysz = params.Size2*params.Res2
Ysz = 311.4286
function data = readDXF(fname)
data = struct;
fid = fopen(fname);
while ~feof(fid)
ln = fgetl(fid);
[param,val] = strtok(ln,"=");
switch param
case {'[General]','[Geometry]','[Interpretation]','[Patient]','[Field]','[PortalDose]'}
continue
case '[Data]'
fseek(fid,0,'eof');
fgetl(fid);
otherwise
val = strtok(val,'=');
pat = asManyOfPattern(characterListPattern("0123456789-+."));
nums = extract(val,pat);
if length(val)==length(nums{1})
data.(param) = str2double(val);
else
data.(param) = val;
end
end
end
fclose(fid);
val = readmatrix(fname,'FileType','text','NumHeaderLines',48);
data.Pixels = val;
end

Sign in to comment.


Les Beckham
Les Beckham on 10 Jan 2025
For Q1, I would suggest imresize. Can you clarify what you mean by "offset x,y values" in Q2?
  1 Comment
Russell
Russell on 10 Jan 2025
Hello, I added some information as requested by Cris. Not sure if this helped?
The first image will be a dose plane
X size = ~40cm with 512pix samples at a res of ~0.078cm
Y size = ~30cm with 384pix samples at res of ~0.078cm
The second image will be a x-ray image but the detector may not exactly be centered (this is where the offset comes from)
xoffset, yoffset
I would like to resample either the dose plane calc on the same positions as the imager or vice versa.
Does this help clarify?

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Jan 2025
If I would like to have a finer sampling of the data by doubling the samples (eg Inew = [768, 1024], which function would you suggest?
If you were willing to have Inew = [767, 1023] instead of [768, 1024] then you can average adjacent rows and adjacent columns and manually insert the averaged values into the proper place. To be honest, though, calling interp2() is a heck of a lot easier.
The reason the output would be Inew = [767, 1023] instead of [768, 1024] is that you would be calculating new points between each existing point. If you had 3 points across then you have 2 intermediate points, for a total of 5. Calculating intermediate points gets you output of size 2*N-1 not output of size 2*N
Getting output exactly twice the original size requires interpolation at "just less than half" apart, like positions 1, 1.49, 1.98, 2.47,...
  9 Comments
Russell
Russell on 14 Jan 2025
Thanks. I guess I'm a little confusd over the spatial vector? I have something like this now
so I'm not sure how to get the interpolated image Iq from I, x[i], y[j] if I have another set of positions xq[i], yq[j] that I want to sample?
Russell
Russell on 15 Jan 2025
Hi Walter,
I guess I'm still having problems understanding how to do this. I added another pdf showing what I'm trying to do. I'm unable to upload the calculated dicom planar dose image. I was wondering if what I'm asking is possible with what you showed me earlier? (this will be compared to the actual dose image measured that I showed earlier)
Thanks for your help.

Sign in to comment.

Categories

Find more on DICOM Format in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!