# Repair damaged circles and create closed curves

1 view (last 30 days)
장훈 정 on 26 Jun 2022
Edited: DGM on 26 Jun 2022
I've asked a similar question in the past. Related links are below.
In order to recover the damaged image, the Cartesian coordinate system is changed to the polar coordinate system, and interpolation is performed here.
If you change the polar coordinate system to the Cartesian coordinate system again, the damaged image is restored.
In this question, the recovery to the closed curve worked well.
But in this question, we want to restore the damaged circle in the same way.
I used the same method, but there is a problem that a closed curve is not formed as shown in the following figure. I'm not sure what method to write to solve this.
After loading the x and y coordinates, the code looks like this:
x = a(:,1); y = a(:,2);
[th1,r1] = cart2pol(x,y);
b = [th1, r1];
b = rmoutliers(b,'percentiles',[0 97]);
th = b(:,1);
r = b(:,2);
[th, sortmap] = sort(th,'ascend');
r = r(sortmap);
% one approach would be to fit a spline to both interpolate and denoise
newth = linspace(-pi,pi,1000)';
pp = fit(th,r,'smoothingspline','smoothingparam',1-1E-5);
newr = pp(newth);
[newx, newy] = pol2cart(newth,newr);
figure(999)
plot(newx,newy,'.','MarkerFaceColor',[0,0,1])
xlim([-200 200])
ylim([-200 200])
grid on
grid minor
pbaspect([1 1 1])

DGM on 26 Jun 2022
Edited: DGM on 26 Jun 2022
I suppose my solution has a weakness that should've been obvious to me. It fails if it has to do extrapolation to bridge a gap. In other words, if there is a gap at theta=0, there's nothing to suggest that the ends of the spline should meet. They'll just go off wherever the endslope takes them.
There's a workaround. It's not very elegant, but this is what I came up with in the time I had available at the moment.
x = a(:,1); y = a(:,2);
[th1,r1] = cart2pol(x,y);
b = [th1, r1];
b = rmoutliers(b,'percentiles',[0 97]);
th = b(:,1);
r = b(:,2);
[th, sortmap] = sort(th,'ascend');
r = r(sortmap);
% offset angles so that there isn't a gap at th=0
thoffset = pi/2;
th = mod(th+thoffset,2*pi)-pi; % shift angles
% one approach would be to fit a spline to both interpolate and denoise
newth = linspace(-pi,pi,1000)';
newth = mod(newth-thoffset,2*pi)-pi;
pp = fit(th,r,'smoothingspline','smoothingparam',1-1E-5); % feel free to adjust
newr = pp(newth);
% shift angles back
th = mod(th-thoffset,2*pi)-pi;
newth = mod(newth-thoffset,2*pi)-pi;
[newx, newy] = pol2cart(newth,newr);
subplot(2,1,1)
plot(th,r,'.','MarkerFaceColor',[0,0,1])
xlim([-4 4])
grid on
subplot(2,1,2)
plot(newth,newr,'.','MarkerFaceColor',[0,0,1])
xlim([-4 4])
grid on figure
subplot(1,2,1)
plot(x,y,'.','MarkerFaceColor',[0,0,1])
xlim([-200 200])
ylim([-200 200])
grid on
axis equal
subplot(1,2,2)
plot(newx,newy,'.','MarkerFaceColor',[0,0,1])
xlim([-200 200])
ylim([-200 200])
grid on
axis equal ### Categories

Find more on Interpolation in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!