Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. from PIL import Image
  2. from PIL import ImageDraw
  3. from PIL import ImageChops
  4. import time
  5. import numpy
  6.  
  7.  
  8.  
  9. def znajdzKrasnala(bez,z):
  10. sourceImage = Image.open(bez)
  11. # sourceImage.show()
  12.  
  13. sourceImageKrasnoludek = Image.open(z)
  14. # sourceImageKrasnoludek.show()
  15.  
  16.  
  17. zKrasnalem=sourceImageKrasnoludek.load()
  18. bezKrasnala=sourceImage.load()
  19.  
  20. len_x=[]
  21. len_y=[]
  22.  
  23. for x in range(0, sourceImage.size[0]):
  24. for y in range(0, sourceImage.size[1]):
  25.  
  26. r1, g1, b1,alfa = sourceImageKrasnoludek.getpixel((x, y))
  27. r, g, b= sourceImage.getpixel((x, y))
  28. zKrasnalem[x, y] = (r1, g1, b1)
  29. bezKrasnala[x, y] = (r, g, b)
  30.  
  31. if (abs(r - r1) > 3 or abs(g - g1) > 3 or abs(b - b1) > 3):
  32. len_x.append(x)
  33. len_y.append(y)
  34. a = r1
  35. b = g1
  36. c = b1
  37. else:
  38. a = 0
  39. b = 0
  40. c = 0
  41. zKrasnalem[x,y] = (a,b,c)
  42.  
  43. wycietyKrasnal = sourceImageKrasnoludek.crop(( min(len_x),min(len_y),max(len_x),max(len_y) ))
  44. wycietyKrasnal.show()
  45. wycietyKrasnal.save('wyciety.png')
  46. # print ("Mam krasnala !")
  47.  
  48. # wycietyKrasnal=open('wyciety.png')
  49. # wycietyKrasnalPix=wycietyKrasnal.load()
  50. return wycietyKrasnal
  51.  
  52.  
  53.  
  54. def chi2_distance(histA, histB, eps = 1e-10):
  55. # compute the chi-squared distance
  56. d = 0.5 * numpy.sum([((a - b) ** 2) / (a + b + eps)
  57. for (a, b) in zip(histA, histB)])
  58.  
  59. # return the chi-squared distance
  60. return d
  61.  
  62.  
  63.  
  64.  
  65. def porownywanie(img, pattern):
  66.  
  67. image=Image.open(img)
  68. # image=image.load()
  69.  
  70. imageX = image.size[0]
  71. imageY = image.size[1]
  72.  
  73. patternX = pattern.size[0]
  74. patternY = pattern.size[1]
  75.  
  76. distances = []
  77. indicies = []
  78. patternHistogram = pattern.histogram()
  79.  
  80. for i in range(0,int(imageX-patternX), int(patternX*0.5)):
  81. for j in range(0, int( imageY-patternY),int(patternY*0.5)):
  82. bufor = image.crop((i, j, i + patternX, j + patternY))
  83. buforHistogram=bufor.histogram()
  84. distance = chi2_distance(buforHistogram, patternHistogram, 1e-10)
  85. distances.append(distance)
  86. indicies.append([i, j])
  87.  
  88. minIndex = distances.index(min(distances))
  89. x = indicies[minIndex][0]
  90. y = indicies[minIndex][1]
  91. draw = ImageDraw.Draw(image)
  92. draw.rectangle((x, y, x + patternX, y + patternY), outline="red")
  93. image.show()
  94. return [x, y]
  95.  
  96.  
  97. wycietyKrasnal = znajdzKrasnala('src_0.png','src_1.png')
  98.  
  99.  
  100. wycietyKrasnalPix=wycietyKrasnal.load()
  101.  
  102. print(porownywanie('img1.png',wycietyKrasnal))
  103. print(porownywanie('img2.png',wycietyKrasnal))
  104. print(porownywanie('img3.png',wycietyKrasnal))
  105. print(porownywanie('img4.png',wycietyKrasnal))
  106. print(porownywanie('img5.png',wycietyKrasnal))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement