Adding additional points to matrix image

6 visualizzazioni (ultimi 30 giorni)
Thabo Mulambo
Thabo Mulambo il 6 Gen 2020
Commentato: DGM il 29 Apr 2025
Hallo,
I have a matrix called my_data and unfortunately the data points are sparse and they produce the following image:
How can I edit the matrix to produce a smooth curve? I have tried interp2 and pcolor with shading interp but the points are still not jointed.
Here is the code I have found on the Internet and edited to suit my application
[A,B] = meshgrid(1:size(my_data,2), 1:size(my_data,1));
[A2,B2] = meshgrid(1:0.01:size(my_data,2), 1:0.01:size(my_data,1));
interped = interp2(A, B, my_data, A2, B2, 'linear');
imagesc(interped)
  2 Commenti
KALYAN ACHARJYA
KALYAN ACHARJYA il 6 Gen 2020
It would be increse grid points.
Thabo Mulambo
Thabo Mulambo il 6 Gen 2020
When I increase the grid points I run into memory issues and the computer freezes...

Accedi per commentare.

Risposte (1)

Raag
Raag il 27 Apr 2025
Hi Thabo,
From what I understand, the issue involves resizing or interpolating an image, and you are looking for a way to upscale or process the image while preserving quality and reducing artifacts.
The best workaround for this is to use MATLAB’s built-in functions such as ‘imresize or ‘interp2’. These functions allow you to specify interpolation methods like 'bicubic' or 'bilinear' to achieve smoother and more accurate results when resizing or transforming your image.
Below is an example how you can use those functions in you code:
% Using imresize with bicubic interpolation
resizedImage = imresize(originalImage, scaleFactor, 'bicubic');
% Using interp2 with bilinear interpolation
[X, Y] = meshgrid(1:size(originalImage,2), 1:size(originalImage,1));
[Xq, Yq] = meshgrid(linspace(1, size(originalImage,2), newWidth), linspace(1, size(originalImage,1), newHeight));
resizedImage = interp2(X, Y, double(originalImage), Xq, Yq, 'linear');
For a better understanding of the above solution, refer to the following documentations:
  1. imresize - https://www.mathworks.com/help/matlab/ref/imresize.html
  2. interp2 - https://www.mathworks.com/help/matlab/ref/interp2.html
  1 Commento
DGM
DGM il 29 Apr 2025
That's already what OP did, and based on their description, it's not what they wanted.
OP had some sort of scattered data which had been gridded somehow. They wanted a smooth curve.
% some fake data
inpict = imread('scatter.png');
inpict = im2double(inpict);
% a common parameter
scalefactor = 4;
% use imresize()
op1 = imresize(inpict,scalefactor,'bilinear');
% use interp2()
szi = size(inpict,1:2);
szo = round(szi*scalefactor);
[X0,Y0] = meshgrid(linspace(0,1,szi(2)),linspace(0,1,szi(1)));
[Xq,Yq] = meshgrid(linspace(0,1,szo(2)),linspace(0,1,szo(1)));
op2 = interp2(X0,Y0,inpict,Xq,Yq,'linear');
% the results are effectively identical
% they're just bigger copies of the original
montage({inpict op1 op2},'size',[1 3],'bordersize',5,'backgroundcolor','m')
immse(op1,op2)
ans = 8.7804e-05
Obviously, upscaling a rasterized representation of scattered points won't make the scattered points form a continuous curve.
How should it have been solved? We'd have to presume a lot of things that OP isn't here to clarify. To me, this sounds like a bit of an XY problem. I suspect that the data wasn't originally gridded, but that OP used griddata() or something. If that's the case, then we should probably start from the scattered data instead of trying to back-calculate what it used to be.
At that point, we'd need to ask what the actual goal is. If all OP needed is a rasterized polyline, that's simple enough. It could be more complicated if the points are not ordered. If something else is expected, it's up to us to guess what it would have been.

Accedi per commentare.

Categorie

Scopri di più su Interpolation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by