Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. --::::::::::::::::::::::::::::::::
  2.  
  3. --::::::::::::::::::::::::::::::::
  4.  
  5. -- Retourne un kernel de gauss
  6. function getGaussianKernel(radius,sigma)
  7.     local kernel = {}
  8.     local width = 2*radius+1
  9.     local height = width
  10.     local GKF , e = 0 , 0
  11.     --
  12.     for i = 0-radius,radius do
  13.         for j = 0-radius,radius do
  14.             e = math.exp( (-1*((i*i)+(j*j))) / (2*sigma*sigma))
  15.             GKF = GKF + e
  16.             if not(kernel[i+radius]) then kernel[i+radius] = {} end
  17.             kernel[i+radius][j+radius] = e
  18.         end
  19.     end
  20.     --
  21.     for i = 0-radius,radius do
  22.         for j = 0-radius,radius do
  23.             kernel[i+radius][j+radius] = kernel[i+radius][j+radius] / GKF  
  24.         end
  25.     end
  26.     return kernel
  27. end
  28.  
  29. -- Retourne le voisinage d'un pixel
  30. function GetV(p,x,y,r)
  31.     local V = {}
  32.     for i = 0 , r do
  33.         V[i] = {}
  34.         for j = 0 , r  do
  35.             if (x+i >= 0 and y+j >=0 and x+i < p:width() and y+j < p:height()) then V[i][j] = p:pixel(x+i,y+j) end
  36.     end end
  37.     return V
  38. end
  39.  
  40. -- Determine le facteur de normalisation
  41. local function GetNormalizeFactor(k)
  42.     local p,n = 0 , 0
  43.     for i,line in pairs(k) do
  44.         for j, value in pairs(line) do
  45.             if (value < 0) then n = n + value
  46.             else p = p + value end
  47.         end
  48.     end
  49.     p , n = math.abs(p),math.abs(n)
  50.     return (((p > n) and p) or n)
  51. end
  52.  
  53. -- Retourne une image flouter
  54. function gaussianBlur(picture,radius,sigma)
  55.     local kernel = getGaussianKernel(radius,sigma)
  56.     local factor = GetNormalizeFactor(kernel)
  57.     local w,h = picture:width(),picture:height()
  58.     local target = Image.createEmpty(w,h)
  59.     for x = 0 , w-1 do
  60.         for y = 0 , h-1 do
  61.             -- On recupere le voisinage du pixel
  62.             local V = GetV(picture,x,y,radius)
  63.             -- On lui applique le noyau de gauss
  64.             for i,v in pairs(V) do
  65.                 for j,k in pairs(v) do
  66.                     if k then
  67.                         local c = k:colors()
  68.                         for key,value in pairs(c) do c[key] = math.floor(math.abs(value * kernel[i][j])/factor) end
  69.                         target:pixel(x,y,Color.new(c.r,c.g,c.b))       
  70.                     end
  71.                 end
  72.             end
  73.         end
  74.     end
  75.     return target
  76. end
  77.  
  78. local i = Image.load("pic.jpg")
  79. local o = gaussianBlur(i,3,2)
  80.  
  81. --[[
  82. while true do
  83.     screen:clear()
  84.     screen:blit(0,0,o)
  85.     screen.flip()
  86.     screen.waitVblankStart()
  87. end
  88. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement