jgtrosh

fronts.lua

May 7th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. require "path"
  2. require "gd"
  3.  
  4. local chemin1 = path({point(156, 118), point(748, 104)})
  5. local chemin2 = path({point(136, 172), point(737, 166)})
  6. local npaths = 10
  7. local chemins = {}
  8. local x1step = (chemin2[1].x-chemin1[1].x)/(npaths-1)
  9. local x2step = (chemin2[2].x-chemin1[2].x)/(npaths-1)
  10. local y1step = (chemin2[1].y-chemin1[1].y)/(npaths-1)
  11. local y2step = (chemin2[2].y-chemin1[2].y)/(npaths-1)
  12. for n = 1, npaths do
  13.     table.insert(chemins, path({
  14.             point(chemin1[1].x+(n-1)*x1step,
  15.                   chemin1[1].y+(n-1)*y1step),
  16.             point(chemin1[2].x+(n-1)*x2step,
  17.                   chemin1[2].y+(n-1)*y2step)}
  18.         ):subdivide())
  19. end
  20. local canvas = gd.createFromPng("frms/frm_1.png")
  21. local red = canvas:colorResolve(255, 0, 0)
  22. local f = io.open("fronts.txt", "w")
  23. local l1 = 30
  24. local l2 = 30
  25. local cnt = 0
  26. for frm = 10, 180, 5 do --2, 46, 3 do
  27.     cnt = cnt + 1
  28.     local inputpng = gd.createFromPng("diffs/diff_"..frm..".png")
  29.     local pf0 = nil
  30.     for nligne, chemin in ipairs(chemins) do
  31.         local points = {}
  32.         local ppv = {}
  33.         local dppv = {}
  34.         for _, p in chemin:points() do
  35.             local c = inputpng:getPixel(p.x, p.y)
  36.             local r = inputpng:red(c)
  37.             local g = inputpng:green(c)
  38.             local b = inputpng:blue(c)
  39.             local i = (g/3-10)*3
  40.             -- on voit dans cristal_diff.gif que l'intensite
  41.             -- dominante est dans le vert
  42.             table.insert(points, i)
  43.         end
  44.         for i = l1+1, #points-l1 do
  45.             local c = 0
  46.             for j = i-l1, i+l1 do
  47.                 c = c+points[j]
  48.             end
  49.             c = c/(2*l1+1)
  50.             if ppv then
  51.                 table.insert(ppv, {i, c})
  52.             end
  53.         end
  54.         for i = l2+1, #points-l2 do
  55.             local c1, c2 = 0, 0
  56.             for j = 1, l2 do
  57.                 c1 = c1+points[i-j]
  58.                 c2 = c2+points[i+j]
  59.             end
  60.             c1, c2 = c1/l2, c2/l2
  61.             table.insert(dppv, {i, c2-c1})
  62.         end
  63.         local avg = 0
  64.         for _, p in ipairs(ppv) do
  65.             avg = avg + p[2]
  66.         end
  67.         avg = avg / #ppv
  68.         local ph = 0
  69.         --nb de points qui sont au dessus de la moyenne
  70.         for _, p in ipairs(ppv) do
  71.             if p[2] < avg then
  72.                 ph = ph + 1
  73.             end
  74.         end
  75.         local mind = 1
  76.         for i, p in ipairs(dppv) do
  77.             if p[2] < dppv[mind][2] then
  78.                 mind = i
  79.             end
  80.         end
  81.         local pfront = dppv[mind][1]
  82.         local pf = chemin[pfront]
  83.         if pf then
  84.             f:write(cnt.."\t"
  85.                 ..math.floor(pf.x).."\t"
  86.                 ..math.floor(pf.y).."\n")
  87.             if pf0 then
  88.                 canvas:line(pf0.x, pf0.y, pf.x, pf.y,
  89.                     red)
  90.             end
  91.         end
  92.         pf0 = pf
  93.     end
  94.     io.write(frm.."\t")
  95.     if cnt % 8 == 0 then
  96.         io.write("\n")
  97.     end
  98. end
  99. f:close()
  100. canvas:png("fronts.png")
  101. io.write("\n")
Advertisement
Add Comment
Please, Sign In to add comment