How to make contour smooth

조회 수: 65 (최근 30일)
University
University 2023년 8월 20일
편집: Bruno Luong 2023년 8월 21일
Please, how can I smooth a contour better than the one I attached. The main data is the .xlsx file
X =linspace(-0.3, 0.3, 51);
Y = linspace(0, 3, 51);

채택된 답변

Star Strider
Star Strider 2023년 8월 20일
편집: Star Strider 2023년 8월 20일
There are a few options, one being to plot fewer levels, and the second being to downsample the matrix using interp2 and also choose fewer levels, and possibly different interpolation method options as well.
Experiment with those approaches here —
figure
imshow(imread('tau_51_51.png'))
M1 = readmatrix('data_tau_51.xlsx');
X = linspace(-0.3, 0.3, 51);
Y = linspace(0, 3, 51);
figure
[c,h] = contourf(X, Y, M1, 6); % Fewer Levels
colormap(turbo)
colorbar
figure
surfc(X, Y, M1);
colormap(turbo)
colorbar
title('Surface Plot of Matrix')
[X1,Y1] = meshgrid(X,Y);
Xr = linspace(min(X), max(X), 26);
Yr = linspace(min(Y), max(Y), 26);
[X2,Y2] = meshgrid(Xr,Yr);
Mr = interp2(X1,Y1,M1,X2,Y2); % Interpolate (Downsample) Matrix
figure
[c,h] = contourf(X2, Y2, Mr, 6); % Fewer Levels Of Interpolated Matrix
colormap(turbo)
colorbar
EDIT — Corrected typographical errors.
.
  댓글 수: 2
University
University 2023년 8월 20일
Thank you so much. This really helps
Star Strider
Star Strider 2023년 8월 20일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2023년 8월 20일
Looks like you used contourf on some gray scale image. You can get smoother contours by blurring your image before calling contourf. Use imfilter
kernel = ones(9,9) / 9^2;
blurryImage = imfilter(grayImage, kernel);

John D'Errico
John D'Errico 2023년 8월 20일
편집: John D'Errico 2023년 8월 20일
As others have pointed out, you don't want to try to make the contours smooth AFTER the fact. Instead you want to smooth the surface in advance, THEN build your contours from the smoothed matrix. This will be a better solution. What method of smoothing you employ is a difficult choice, especially since the surface you have appears to have very sharp curvature in places. That will make it difficult no matter what you choose.
  댓글 수: 3
Image Analyst
Image Analyst 2023년 8월 21일
Yes, but @John D'Errico and I believe he doesn't want an edge preserving filter. It's the edges that are giving rise to a blocky edges/outlines. What you want is a blurring filter so the edges will be smoother. But actually I'm not sure why he wants them to be smoother anyway. @University What's wrong with the original, more accurately placed contour lines? Why smooth them out? I don't think it's necessary unless you just like smoother lines for aesthetic reasons.
Again, seeing the original image would help. Please post it.
Bruno Luong
Bruno Luong 2023년 8월 21일
편집: Bruno Luong 2023년 8월 21일
@Image Analyst Anisotropic diffussion filter will smooth the data along the edge and keep the normal derivative significantly unchanged. Edge preserving filter will smooth the contours while keeping the density (high at the edge) unmodified. That is exactly what it needs, I think.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by