Fast IFS using RPN notation
python
c
x86-64
nasm
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ifs4.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import random
  2. import time
  3. import copy
  4. import numpy as np
  5. from skimage.restoration import estimate_sigma
  6. from .expr import *
  7. from .fractdim import *
  8. class IFS4(object):
  9. """ @brief 1 Variable IFS with R,G,B,A X,Y decomposition """
  10. #height=width=1024
  11. height=width=768
  12. #height=width=512
  13. def __init__(self, nexpr=4, init_sz=1, world=None):
  14. self._nexpr = nexpr
  15. self._expr_pos = [[RpnExpr(sz=init_sz, nvar=6)
  16. for _ in range(nexpr)]
  17. for _ in range(2)]
  18. self._expr_col = [[RpnExpr(sz=init_sz, nvar=6)
  19. for _ in range(nexpr)]
  20. for _ in range(4)]
  21. self._world = world
  22. if self._world is None:
  23. self._world = self.get_world()
  24. self._position = [random.randint(0, self.height-1),
  25. random.randint(0, self.width-1)]
  26. self._col = [random.randint(0, 255) for _ in range(4)]
  27. @classmethod
  28. def get_world(cls):
  29. return np.zeros((cls.height,cls.width,4), dtype=np.uint8)
  30. def raz_world(self):
  31. for i in range(self.height):
  32. for j in range(self.width):
  33. self._world[i][j] = [0,0,0,0]
  34. def __str__(self):
  35. ret = 'X<%s>' % (';'.join([str(e) for e in self._expr_pos[0]]))
  36. ret += 'Y<%s>' % (';'.join([str(e) for e in self._expr_pos[1]]))
  37. ret += 'R<%s>' % (';'.join([str(e) for e in self._expr_col[0]]))
  38. ret += 'G<%s>' % (';'.join([str(e) for e in self._expr_col[1]]))
  39. ret += 'B<%s>' % (';'.join([str(e) for e in self._expr_col[2]]))
  40. ret += 'A<%s>' % (';'.join([str(e) for e in self._expr_col[3]]))
  41. return ret
  42. def __copy__(self):
  43. ret = IFS4(nexpr=self._nexpr, world=self._world)
  44. ret._expr_pos = [[copy.copy(e) for e in el]
  45. for el in self._expr_pos]
  46. ret._expr_col = [[copy.copy(e) for e in el]
  47. for el in self._expr_col]
  48. #ret._world = copy.deepcopy(self._world)
  49. return ret
  50. def get_image(self):
  51. return self._world
  52. def mutation(self, n=1, rand=True):
  53. n = 1 if n <= 1 else random.randint(1,n)
  54. for _ in range(n):
  55. random.choice(self._expr_pos + self._expr_col).mutation()
  56. def step(self):
  57. dr,dg,db,da = [int(e)
  58. for e in self._world[self._position[0]][self._position[1]]]
  59. cx = int((self._position[0] / self.width) * ((1<<64)-1))
  60. cy = int((self._position[0] / self.height) * ((1<<64)-1))
  61. args = (cx,cy,dr,dg,db,da)
  62. rx,ry = [int(random.choice(expr).eval(*args))
  63. for expr in self._expr_pos]
  64. rx = (rx * self.width) // ((1<<64)-1)
  65. ry = (ry * self.height) // ((1<<64)-1)
  66. r,g,b,a = [int(random.choice(expr).eval(*args) % 256)
  67. for expr in self._expr_col]
  68. sa = a/255
  69. outa = (a + da*(1-sa))/255
  70. if outa == 0:
  71. ro,go,bo = 0,0,0
  72. else:
  73. ro, go, bo = [(c*(a/255)+dc*da*(1-sa))/outa
  74. for c, dc in ((r,dr), (g, dg), (b, db))]
  75. r,g,b,a = [int(e) for e in (ro,go,bo,outa*255)]
  76. self._world[self._position[0]][self._position[1]] = [r,g,b,a]
  77. def score(self):
  78. start = time.time()
  79. colcount = len(np.unique(np.reshape(self._world[:,:,:3], (-1,3)),
  80. axis=0))
  81. #sigma = estimate_sigma(self._world, multichannel=True, average_sigmas=True)
  82. sigmas = estimate_sigma(self._world[:,:,:3], multichannel=True,
  83. average_sigmas=False)
  84. scores = [fractal_dimension(self._world[:,:,i]*self._world[:,:,3]/255)
  85. for i in range(3)]
  86. #alpha score
  87. #scores += [fractal_dimension(self._world[:,:,3])]
  88. gray = rgb2gray(self._world)
  89. graysigma = estimate_sigma(gray)
  90. grayscore = fractal_dimension(gray)
  91. del(gray)
  92. sigmas += [graysigma]*3
  93. sigmas = [0 if np.isnan(sigma) else sigma for sigma in sigmas]
  94. scores += [grayscore]*3
  95. sigma = sum(sigmas)/len(sigmas)
  96. mod = abs(scores[0]-scores[1])
  97. mod += abs(scores[0]-scores[2])
  98. mod += abs(scores[0]-scores[3])
  99. mod += abs(scores[1]-scores[2])
  100. mod += abs(scores[1]-scores[3])
  101. mod /= 5
  102. score = sum(scores)/len(scores)
  103. score += mod
  104. if sigma and sigma > 0:
  105. score -= sigma/100
  106. colscore = abs(colcount-1024) / 1e5
  107. score -= colscore
  108. printscore = lambda arr: '['+(', '.join(['%1.3f' % e for e in arr]))+']'
  109. print("colscore %3.3f (%4d colors) scores time %5.2fs" % (colscore,
  110. colcount,
  111. time.time() - start))
  112. print("SIGMAS : %s SIGMA : %f " % (printscore(sigmas), sigma))
  113. print("SCORES : %s SCORE : %r" % (printscore(scores), score))
  114. return score