How draw spline on image

15 Ansichten (letzte 30 Tage)
ELVIS PRESLY
ELVIS PRESLY am 5 Jul. 2015
Bearbeitet: DGM am 22 Mär. 2023
Hi all, I'v an image of a series of concentric circle (or something like that), and I want to extrapolate (by phisical click, like trace a spline i.e. photoshop) the spline equation for all of them, how can I do? Someone have an idea? Thanks in advance!
Moore details: have an image like this:
I thought to import the point in a matrix from the intersection between the circles and a group of lines centred on the max point (89 12) .
The aim of this work is to make a blinear interpolation with this diagram of a generic point P. To make this I have to find which is the cell contains the guessed point P (if is in the cell 1, cell2, or cell....). I can approximate each cell with segment, but this fail when I try to locate a point like P'.
My problem is make an interpolation of a function Z = f(x, y),with a set of given points on the isolevel curves,in matlab. I splitted the problem in this way:
1) INTERPOLATION of isolevel CURVES:
So the first thing I drew a bunch of lines on the picture (centered at the point of maximum (the function is more or less like a bell), then I picked up the points of intersection between the curves isolevel Z1, Z2, ... ., ZNK and the bundle of straight lines, obtaining a series of matrices (x1, x2, .... XNP; y1, y2, ... ynp; z1, .... ZNP) one for each nk curve isolevel. (Nk = number of curves isolevel considered; np = number of points collected for each curve isolevel = number of beam lines, equally spaced 360 / np °)
2) INTERPOLATION SCHEME
To interpolate the function I wanted to use a 'bilinear interpolation from 4 points. As a first step I was looking for a system to place the generic point P = (x *, y *) within a generic cell (Cell1, Cell2, ....) formed by the intersection of two iso-level curves and from the straight from me drawn, centered at the point of maximum.
PROBLEM:
I am seeking a method to implement in matlab I has the cell in which is included my point P = (x *, y *) so as to perform my interpolation on the basis of the 4 vertices of that cell (A, B, C, D ). But since my curves and especially similar to ellipses (without correspondence in x and y) do not know how to approximate the contours of the single cell. If approximates the line segments with curved sides could be wrong cell belongs (in the image point P 'falls in Cell1 had to interpolate the points with straight lines, but actually, considering how curved sides, P' falls in a ' other cell !!)
  2 Kommentare
Image Analyst
Image Analyst am 5 Jul. 2015
What are you starting with. Exactly what does "an image of a series of concentric circle" mean? Can you give a screenshot of what you're starting with?
ELVIS PRESLY
ELVIS PRESLY am 6 Jul. 2015
Bearbeitet: ELVIS PRESLY am 6 Jul. 2015
First of all thanks a lot for your reply!!!
I have an image like this:
I thought to import the point in a matrix from the intersection between the circles and a group of lines centred on the max point (89 12) .
The aim of this work is to make a blinear interpolation with this diagram of a generic point P. To make this I have to find which is the cell contains the guessed point P (if is in the cell 1, cell2, or cell....). I can approximate each cell with segment, but this fail when I try to locate a point like P'.
My problem is make an interpolation of a function Z = f(x, y),with a set of given points on the isolevel curves,in matlab. I splitted the problem in this way:
1) INTERPOLATION of isolevel CURVES:
So the first thing I drew a bunch of lines on the picture (centered at the point of maximum (the function is more or less like a bell), then I picked up the points of intersection between the curves isolevel Z1, Z2, ... ., ZNK and the bundle of straight lines, obtaining a series of matrices (x1, x2, .... XNP; y1, y2, ... ynp; z1, .... ZNP) one for each nk curve isolevel. (Nk = number of curves isolevel considered; np = number of points collected for each curve isolevel = number of beam lines, equally spaced 360 / np °)
2) INTERPOLATION SCHEME
To interpolate the function I wanted to use a 'bilinear interpolation from 4 points. As a first step I was looking for a system to place the generic point P = (x *, y *) within a generic cell (Cell1, Cell2, ....) formed by the intersection of two iso-level curves and from the straight from me drawn, centered at the point of maximum.
PROBLEM:
I am seeking a method to implement in matlab I has the cell in which is included my point P = (x *, y *) so as to perform my interpolation on the basis of the 4 vertices of that cell (A, B, C, D ). But since my curves and especially similar to ellipses (without correspondence in x and y) do not know how to approximate the contours of the single cell. If approximates the line segments with curved sides could be wrong cell belongs (in the image point P 'falls in Cell1 had to interpolate the points with straight lines, but actually, considering how curved sides, P' falls in a ' other cell !!)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

DGM
DGM am 22 Mär. 2023
Bearbeitet: DGM am 22 Mär. 2023
This is how I would do it.
Start by taking the image and converting it to a set of XY data describing each contour. You could do that using getpts() or the like, but I've taken to doing that sort of work in Inkscape and then importing the SVG (attached). This gives me the luxury of being able to interactively fit a smooth spline to the curves. In this example, some of the curves are interpolated across gaps for sake of connectedness, but the interpolated segments that extend beyond the plot box can be discarded if desired.
Since there are no X,Y labels, I have no idea what the x or y coordinates of any point are. I elected to assert that the plot box is defined by the outermost major grid lines (the green rectangle), and that these extents correspond to unit-scale in both X and Y.
%% READ DATA FROM SVG
% using the following FEX tools:
% https://www.mathworks.com/matlabcentral/fileexchange/72225-load-svg-into-your-matlab-code
% filename of manually-fit svg file (attached)
fname = 'interpcontours.svg';
% data range from original image axis labels
% this is where the rectangle is drawn in the SVG
xrange = [0 1]; % i'm assuming these values
yrange = [0 1];
% spline discretization parameter [0 1]
coarseness = 0.01;
% get plot box geometry
str = fileread(fname);
str = regexp(str,'((?<=<rect)(.*?)(?=\/>))','match');
pbx = regexp(str,'((?<=x=")(.*?)(?="))','match');
pby = regexp(str,'((?<=y=")(.*?)(?="))','match');
pbw = regexp(str,'((?<=width=")(.*?)(?="))','match');
pbh = regexp(str,'((?<=height=")(.*?)(?="))','match');
pbrect = [str2double(pbx{1}{1}) str2double(pby{1}{1}) ...
str2double(pbw{1}{1}) str2double(pbh{1}{1})];
% get coordinates representing the curve
S = loadsvg(fname,coarseness,false);
for k = 1:numel(S) % there are multiple curves
x = S{k}(:,1);
y = S{k}(:,2);
% rescale to fit data range
x = xrange(1) + diff(xrange)*(x-pbrect(1))/pbrect(3);
y = yrange(1) + diff(yrange)*(pbrect(4) - (y-pbrect(2)))/pbrect(4);
% get rid of nonunique points
% this may or may not be needed depending on the shape
% of the curves and how they're to be processed
[x,idx,~] = unique(x,'stable');
y = y(idx);
% shove the prepared data back into S for later
S{k} = [x y];
end
% plot the curves by themselves
for k = 1:numel(S)
x = S{k}(:,1);
y = S{k}(:,2);
plot(x,y); hold on
end
grid on;
xlim(xrange)
ylim(yrange)
%% PROCESS AND INTERPOLATE
% contour levels (interior outward)
% this is the order that the curves were drawn
L = [89.12 89 88.5 88 87.5 87 86 85 84 82 80 78 76];
% incorporate Z data and consolidate all data segments
for k = 1:numel(S)
S{k} = [S{k} L(k)*ones(size(S{k},1),1)];
end
XYZ = vertcat(S{:});
% if desired, discard points that are outside the plot box
%goodpoints = XYZ(:,1)>xrange(1) & XYZ(:,1)<xrange(2) ...
% & XYZ(:,2)>yrange(1) & XYZ(:,2)<yrange(2);
%XYZ = XYZ(goodpoints,:);
% generate the interpolating function
F = scatteredInterpolant(XYZ(:,1),XYZ(:,2),XYZ(:,3),'linear');
% create gridded query points (or any other query points you want)
N = 30;
xv = linspace(xrange(1),xrange(2),N);
yv = linspace(yrange(1),yrange(2),N);
[xq yq] = meshgrid(xv,yv);
% do the interpolation to find the intermediate Z values
newZ = F(xq,yq);
% plot it if you want
surf(xq,yq,newZ); hold on
shading flat
camlight
% plot the contours for sake of comparison
for k = 1:numel(S)
x = S{k}(:,1);
y = S{k}(:,2);
z = L(k)*ones(numel(x),1);
plot3(x,y,z,'k','linewidth',2)
end
Note where the drawn contours extend below y = 0 and beyond x = 0 and x = 1.
Also, consider that there's a lot of extrapolated garbage in the top and SE corner of the image. It would probably be worthwhile to draw an extra closed curve in the SVG to define a region where you want to actually do the interpolation. That way you can just discard the garbage in those regions. A logical mask could be generated from such a curve using poly2mask().

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by