二维高斯正态分布函数(转)

分类: Matlab

二维高斯正态分布函数(原创)

二维高斯正态分布函数在很多地方都用的到,比如说在滤波中,自己编了个,但感觉IDL中应该有现成的函数??(我没找到)。如有,请高手指点。

 

;-----------------------------
;二维高斯正态分布函数
;x,y:输入的一维数组(维数应该相同)
;x0,y0:均值
;sd:标准差
function Gaussian2DDistribution,x,y,x0,y0,sd
xsize=N_ELEMENTS(x)
ysize=N_ELEMENTS(y)
if (xsize ne ysize) then begin
   Result = DIALOG_MESSAGE('输入的X和Y数组的维数不相同!',/error,title='警告')
   return,0
endif

pz=fltarr(xsize,ysize)
for i=0,xsize-1 do begin
   for j=0,ysize-1 do begin
    a=-((x[i]-x0)*(x[i]-x0)+(y[j]-y0)*(y[j]-y0))/(2.0*sd*sd)
    pz[i,j]=exp(a)/(2*3.1415926*sd*sd)
   endfor
endfor

return,pz

end

;-------------------------------
pro Gaussian2DDistribution_test
x=FINDGEN(11)-5.0
y=FINDGEN(11)-5.0

z=Gaussian2DDistribution(x,y,0.0,0.0,2.5)
WINDOW,/free, XSIZE=500, YSIZE=500,title='Gaussian 2D Distribution'
SHADE_SURF, z ,x,y
SURFACE, Z,x,y, XST = 4, YST = 4, ZST = 4, /NOERASE
print,total(z)

end

 

 

 

-------------------------------------------------

补充:后来找了找,在CANNY函数中找到了相同的内容,把它贴出来吧

;;---------------------------------------------------------------------------
;; Canny_gaussian
;;
;; Purpose:
;;   Create a guassian kernal for smoothing
;;
;; Parameters:
;;   SIGMA - sigma value
;;
;;   WIDTH - desired width of the kernel
;;
;; Keywords:
;;   NONE
;;
FUNCTION canny_gaussian, sigma, width

IF n_elements(sigma) NE 1 THEN sigma = 1.0d
;; if not specified create a 5x5 kernel
IF (n_elements(width) EQ 0) THEN width = 5

;; create X and Y indices
x = (dindgen(width)-width/2) # replicate(1, width)
y = transpose(x)

;; create kernel
kernel = exp(-((x^2)+(y^2))/(2*double(sigma)^2)) / (sqrt(2.0*!pi) * double(sigma))
;; give it an appropriate scale factor
scale = 1/min(kernel) < 16384
kernel = TEMPORARY(kernel)*scale + 1d-6

return, long(kernel)

END

posted on 2014-12-28 17:13  淡淡的柳如烟1  阅读(88)  评论(0)    收藏  举报