STL Creation after Savitzky Filter

8 ビュー (過去 30 日間)
Holden Tranquillo
Holden Tranquillo 2022 年 4 月 7 日
回答済み: DGM 2025 年 7 月 15 日
Hello All,
I have a program that will create STL files of these contours (see images). However, without using a Savitzky filter, the contours are very jagged. But, the triangulation only seems to work correctly when Savitszky filter is not used. Manipulate lines 46 and 47 to turn on or off the filter to see the difference.
Does anyone know how I can get the STLs being created correctly again but with using the Savitzky filter?
Thanks in advanced

回答 (2 件)

Saurav
Saurav 2024 年 3 月 1 日
Hi Holden,
I understand that you wish to create contours using your program and obtain the same outcomes as when you do not utilize the Savitzky filter.
Savitzky-Golay smoothing filters are typically used to smooth out a noisy signal whose frequency span (without noise) is large. It is more effective at preserving high frequency signal components but less successful at rejecting noise.
There are restrictions on the “sgolayfilt” function: that the polynomial order needs to be less than frame length, and the input's length needs to be greater than frame length.
The function has an upper limit on how large you may keep the frame length and, consequently, the polynomial order because the input length in your scenario is already fixed.
Also, one needs to consider that if the boundaries of the objects are very complex, a simple smoothing might not be sufficient and may need a more specialized approach to handle such complexity.
Here is a workaround that can be followed:
  • In the function “sgolayfilt”, the second argument, with a value of 1, denotes a linear polynomial. This might be too simple to properly depict the data's contour. A higher polynomial order can produce a more refined shape.
  • The frame length is the third parameter of the "sgolayfit" function, and it denotes how many points the function uses for each fit. If the frame length is too short, it might not smooth the noise very well. On the other hand, if it is too long, it could end up smoothing away some of the important details of the data. To get the desired results, try out a few different frame lengths and see what works best.
To learn more about the function “sgolayfilt”, following documentation can be followed:
I hope this helps! Thanks.

DGM
DGM 2025 年 7 月 15 日
There's nothing wrong with the filtering. The vertex lists were backwards, and the edge lists were wrong.
% Read in Map PNG
pic = imread('pleasant.png');
pic = im2gray(pic); % convert to grayscale
BW = ~imbinarize(pic,0.8); % convert to binary image
imshow(BW);
% get boundaries
[B,L] = bwboundaries(BW,'noHoles');
% Create STL's of Each Contour
col_list = 'ymcrgbk';
nobjects = numel(B);
for k = 2:nobjects
Bv = flipud(B{k}); % winding should be ccw
Bv = Bv*[0 -1; 1 0]; % apply the rotation
Bv = sgolayfilt(Bv,1,9);
if length(Bv)>3
% construct the edge list
idx = (1:size(Bv,1)).';
E = [idx circshift(idx,-1)];
% do a constrained triangulation to generate the triangles
T = delaunayTriangulation(Bv,E);
F = T.ConnectivityList(isInterior(T),:);
% shove it back in a triangulation object
T = triangulation(F,T.Points);
% Plot
linecolor = ['-' col_list(mod(k-1,numel(col_list))+1)];
tri = triplot(T,linecolor);
axis equal
hold on
% Write to STL
filename = sprintf('perimeter%d.stl',k);
stlwrite(T,filename);
end
end
% load a couple of the files to prove the results
pickthese = [2 3]; % which files to preview
for k = 1:numel(pickthese)
T = stlread(sprintf('perimeter%d.stl',pickthese(k)));
figure
trisurf(T,'facecolor','w','edgecolor','none')
view(3); view(-30,47); camlight;
axis equal; grid on
end
The results are strictly 2D (planar) triangulations, not solid models. If you need thickness, you could extrude it:

カテゴリ

Help Center および File ExchangeSmoothing and Denoising についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by