Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. from scipy import misc
  2.  
  3. IMAGE = '1.jpeg'
  4.  
  5. T = [194, 0, 11]
  6. S = [64, 49, 46]
  7.  
  8. def dist(x, y):
  9. res = 0
  10. for i in xrange(0,2):
  11. res += (int(x[i]) - int(y[i]))**2
  12. return res
  13.  
  14. def get_neighbours(i, j, max_i, max_j):
  15. result = []
  16. if i > 0:
  17. result.append((i - 1, j))
  18. if j > 0:
  19. result.append((i, j - 1))
  20. if (i + 1) < max_i:
  21. result.append((i + 1, j))
  22. if (j + 1) < max_j:
  23. result.append((i, j + 1))
  24.  
  25. return result
  26.  
  27. def flip(i, j, A, B):
  28. if (i, j) in A:
  29. A.remove((i, j))
  30. B.add((i, j))
  31. else:
  32. B.remove((i, j))
  33. A.add((i, j))
  34.  
  35. def cut(image):
  36. dim_x, dim_y, colors = image.shape
  37. A = set()
  38. B = {(i, j) for i in range(dim_x) for j in range(dim_y)}
  39. for i in xrange(0, dim_x):
  40. for j in xrange(0, dim_y):
  41. in_A = dist(image[i, j], S)
  42. in_B = dist(image[i, j], T)
  43. if in_A > in_B:
  44. if (i, j) in A:
  45. flip(i, j, A, B)
  46. else:
  47. if (i, j) in B:
  48. flip(i, j, A, B)
  49.  
  50.  
  51. return A, B
  52.  
  53.  
  54. if __name__ == "__main__":
  55. image = misc.imread(IMAGE)
  56. dim_x, dim_y, colors = image.shape
  57. print "Loaded image of shape {x}, {y}".format(x=dim_x, y=dim_y)
  58.  
  59. A, B = cut(image)
  60.  
  61. for i in range(dim_x):
  62. for j in range(dim_y):
  63. if (i, j) in A:
  64. image[i, j, 0] = min(255, image[i, j, 0] * 2 + 100)
  65. image[i, j, 1] = image[i, j, 1] / 3
  66. image[i, j, 2] = image[i, j, 2] / 3
  67. elif (i,j) in B:
  68. image[i, j, 0] = image[i, j, 0] / 3
  69. image[i, j, 1] = min(255, image[i, j, 1] * 2 + 100)
  70. image[i, j, 2] = image[i, j, 2] / 3
  71.  
  72. misc.imsave('out.png', image)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement