Smooth 2D surface of surface plot

8 vues (au cours des 30 derniers jours)
Martina Clairand
Martina Clairand le 30 Avr 2021
Hello,
The attached object has been constructed from an hemi-ellipsoid and an hemisphere. The curvature of the object however is not smooth everywhere.
Is there a way to make it smooth and to store the new coordinates Xsmooth , Ysmooth and Zsmooth of the object to reconstruct it with surf(Xsmooth, Ysmooth, Zsmooth)?
Thank you in advance for your help!
  3 commentaires
Image Analyst
Image Analyst le 1 Mai 2021
Can you post a screenshot (PNG file) so we can see it here instead of having to dowload the .fig file, switch to MATLAB, then open it there? Make it easy for us to help you.
DGM
DGM le 1 Mai 2021
I set shading flat and camlight on to help illustrate that the surface is quite smooth except for the step caused by misalignment.

Connectez-vous pour commenter.

Réponse acceptée

DGM
DGM le 1 Mai 2021
Modifié(e) : DGM le 2 Mai 2021
If the goal is to improve the smoothness of the volume of rotation, then we should go back to that. There are a few things we could do, but I have a feeling that if you're building the approximation from primitives, then the exactness of the shape isn't terribly important. I'll just throw some stuff at it. Let's start with the lumpy eggplant we left off with:
On one hand, we can try smoothing the source object image and do some oversampling to try to get a better curve to begin with:
%% do the same thing, but oversampling
clc; clf; clear variables
IM = imread('pear.jpg');
E = imbinarize(IM);
object=bwareafilt(E,1);
% try to smooth the image
resamplefactor = 4;
object = imfilter(double(object),fspecial('disk',20));
object = imresize(object,resamplefactor)>0.5;
% i guess the object is already centered at y=140
A = regionprops(object,'centroid');
ycenter = A.Centroid(2);
% select the upper curve
[~,upper] = max(object);
xextent = [find(upper>1,1,'first') find(upper>1,1,'last')];
upper = ycenter-upper(upper>1); % trim off invalid radii
% close the curve
upper([1,end]) = 0;
Ncirc = 200; % number of faces around circumference
Nlong = 300; % number of faces along length
% rescale and resample to specified resolution
xextent = xextent/resamplefactor;
x0 = linspace(xextent(1),xextent(2),numel(upper));
xf = linspace(xextent(1),xextent(2),Nlong);
upper = interp1(x0,upper,xf,'linear')/resamplefactor;
% one last bit of smoothing
upper = smooth(upper,20);
% run the surf plot
[Y Z X] = cylinder(upper,Ncirc);
surf(X*diff(xextent)+xextent(1),Y,Z)
axis equal
shading flat
colormap(ccmap)
camlight
That's better, but it still is a bit lumpy in spots. If that's not smooth enough, maybe we can discard the curve and replace it with a spline approximation:
%% do the same thing, but do a curve fit
clc; clf; clear variables
IM = imread('pear.jpg');
E = imbinarize(IM);
object=bwareafilt(E,1);
% try to smooth the image
resamplefactor = 2;
object = imfilter(double(object),fspecial('disk',20));
object = imresize(object,resamplefactor)>0.5;
% i guess the object is already centered at y=140
A = regionprops(object,'centroid');
ycenter = A.Centroid(2);
% select the upper curve
[~,upper] = max(object);
xextent = [find(upper>1,1,'first') find(upper>1,1,'last')];
upper = ycenter-upper(upper>1); % trim off invalid radii
% close the curve
upper([1,end]) = 0;
Ncirc = 200; % number of faces around circumference
Nlong = 200; % number of faces along length
xextent = xextent/resamplefactor;
x0 = linspace(xextent(1),xextent(2),numel(upper));
xf = linspace(xextent(1),xextent(2),Nlong);
upper = interp1(x0,upper,xf,'linear')/resamplefactor;
% smooth the thing with a spline fit
fm = fit(xf',upper','smoothingspline','smoothingparam',0.01);
upper = fm(xf);
upper([1,end]) = 0;
% run the surf plot
[Y Z X] = cylinder(upper,Ncirc);
surf(X*diff(xextent)+xextent(1),Y,Z)
axis equal
shading flat
colormap(ccmap)
camlight
That's a lot better, but idk if you have fit(). There may be some other smoothing spline tools in the base toolbox or on the FEX.
  1 commentaire
Martina Clairand
Martina Clairand le 1 Juin 2021
Thank you! it was a little bit better

Connectez-vous pour commenter.

Plus de réponses (1)

Martina Clairand
Martina Clairand le 1 Mai 2021
Hello,
Thank you for your answers. @DGM my mistake! there was a misalignement but now I've corrected it and there is still a mismacht of the curvature at the intersection between the hemisphere and the hemiellipsoid
Otherwise I tried to reconstruct the object with "cylinder" function and the 2D boundaries of my flat object but there are much more irregularities:
@DGM, @Image Analyst do you have an idea how to fix any of those heterogeneities and to extract the coordinates of the smooth plot?
Thank you for your help!

Community Treasure Hunt

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

Start Hunting!

Translated by