Gaussian ring in 2d

19 ビュー (過去 30 日間)
Denis Wolff
Denis Wolff 2016 年 10 月 4 日
編集済み: Petr Bouchal 2022 年 12 月 28 日
Hello,
I want to generate a 2d image (in form of a matrix) of a ring which is smoothed towards the outer and inner border by a Gaussian distribution, i.e. something looking like this:
That is, pixels in the image corresponding to the middle radius of the ring should have highest values and outer pixels values that are decreasing towards the borders.
Thanks, Denis
  2 件のコメント
Naresh Sharma
Naresh Sharma 2018 年 9 月 17 日
Can anyone suggest me the appropriate algorithm to find the center and the diameter(inner and outer) of the ring profile images?
Naresh
Petr Bouchal
Petr Bouchal 2022 年 12 月 28 日
編集済み: Petr Bouchal 2022 年 12 月 28 日
You can fit with the theoretical ring function using the least-squares method to find the width and central radius and calculate the inner and outer radius afterward.
%% generating ring with gaussian profile
x = linspace(-1,1,200);
y = linspace(-1,1,200);
[X,Y] = meshgrid(x,y);
[Phi,R] = cart2pol(X,Y);
R0 = 0.5; %ring radius
W = 0.2; %ring width
ring = exp(-((R-R0).^2)./W.^2);
%% finding center and diameter
fun = @(x,R) exp(-((R-x(1)).^2)./x(2).^2);
x0 = [1,1];
x = lsqcurvefit(fun,x0,R,ring);
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
R0_reconstructed = x(1);
W_reconstructed = x(2);
%% accuracy
R0_reconstructed
R0_reconstructed = 0.5000
R0-R0_reconstructed
ans = -2.7756e-15
W_reconstructed
W_reconstructed = 0.2000
W-W_reconstructed
ans = 1.2768e-15

サインインしてコメントする。

採用された回答

Seth Meiselman
Seth Meiselman 2016 年 12 月 15 日
Try something like this: create a series of circles whose amplitude is in the form of a Gaussian with respect to some defined peak radius, R0, and the usual width factor, sig.
sizex = 1024;
sizey = 1024;
[ncols, nrows] = meshgrid(1:sizex, 1:sizey);
centerx = sizex/2;
centery = sizey/2;
R0 = 300;
sig = 20;
gring = zeros(sizex,sizey)';
for i=1:400
iR = i;
oR = i+sig;
array2D = (nrows - cy).^2 + (ncols - cx).^2;
ringPixels = array2D >= iR^2 & array2D <= oR^2;
gaussring = gaussring + ringPixels.*(1/(sig*sqrt(2*pi)))*exp(-((iR-R0)/(2*sig))^2);
end
figure(1);
surf(gaussring); axis tight; shading flat; view(2); colormap('jet');
This results in the plot:
and a non-trivial slice through the image
figure(2);
plot(gring(sizex/4,:)); axis tight;
  2 件のコメント
Seth Meiselman
Seth Meiselman 2016 年 12 月 15 日
I should have also said, since this is made on a discrete grid- it creates the matrix you are looking for, but also has some drawbacks- it's very 'pixelated'. You can see this by rotating the surface plot and observing spikes that pop up. Nothing a quick smoothing function wouldn't remove if it was critical to remove.
Denis Wolff
Denis Wolff 2016 年 12 月 19 日
Thank you Seth, that solves it!

サインインしてコメントする。

その他の回答 (3 件)

Petr Bouchal
Petr Bouchal 2022 年 12 月 23 日
編集済み: Petr Bouchal 2022 年 12 月 28 日
Hi, I would say the most appropriate approach is the following:
x = linspace(-1,1,200);
y = linspace(-1,1,200);
[X,Y] = meshgrid(x,y);
[Phi,R] = cart2pol(X,Y);
R0 = 0.5; %ring radius
W = 0.2; %ring width
ring = exp(-((R-R0).^2)./W.^2);
figure(); hold on;
imagesc(ring); axis equal; colormap gray;
plot(size(ring,1)*ring(0.5*size(ring,1),:));

KSSV
KSSV 2016 年 10 月 4 日
clc; clear all ;
M = 10 ;
N = 100 ;
R1 = 0.5 ; % inner radius
R2 = 1 ; % outer radius
nR = linspace(R1,R2,M) ;
nT = linspace(0,2*pi,N) ;
%nT = pi/180*(0:NT:theta) ;
[R, T] = meshgrid(nR,nT) ;
% Convert grid to cartesian coordintes
X = R.*cos(T);
Y = R.*sin(T);
[m,n]=size(X);
%
D = (X.^2+Y.^2) ;
% D(D<R1) = 1 ;
% D(D>R2) = 1 ;
Z = gauss(D);
surf(X,Y,Z);
colormap('gray')
shading interp ;
view([0 90]) ;
set(gca,'color','k')
  1 件のコメント
Denis Wolff
Denis Wolff 2016 年 10 月 4 日
Thank you, but this is actually not what I am looking for. This code only applies a Gaussian distribution with mu in the center of the circle, smoothing towards the outer border only. What I am looking for is a ring which is smoothed towards outer and inner border. There are many Gaussians so to say which have mus that correspond to the pixels on the middle radius of the ring.

サインインしてコメントする。


Joe Yeh
Joe Yeh 2016 年 10 月 4 日
編集済み: Joe Yeh 2016 年 10 月 4 日
I suppose you're looking for Laplacian of Gaussian ( Mexican hat )filter ? It fits your description. You can get a laplacian of gaussian kernel by :
log_kernel = fspecial('log');
The attached picture is a surf plot of a laplacian of gaussian kernel.
  1 件のコメント
Denis Wolff
Denis Wolff 2016 年 10 月 4 日
編集済み: Denis Wolff 2016 年 10 月 4 日
Thanks, that's an interesting hint! The only problem with this approach is that the smoothing is not the same for the inside as for the outside (cf. output below, it's much steeper for the inner border).
There has to be some symmetrical Gaussian ring distribution which is looking like this but without the peak in the middle...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by