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 1.77 KB | None | 0 0
  1. from PIL import Image, ImageDraw
  2. import numpy as np
  3. from math import *
  4. import time
  5.  
  6.  
  7.  
  8. im1 = np.array(Image.open("src_0.png").convert("RGB"))
  9. im2 = np.array(Image.open("src_1.png").convert("RGB"))
  10. im4 = Image.open("src_1.png").convert("RGB")
  11. mask = np.array(im2)
  12. mask[np.abs(im1-im2) < 1] = 0
  13. box = Image.fromarray(mask).getbbox()
  14. # box[0] += 30
  15. # box[1] += 10
  16. # box[2] -= 60
  17. # box[3] -= 63
  18. hero = mask[box[1]+10:box[3]-23, box[0]+10:box[2]-50]
  19. herosizex, herosizey = (box[3]-23-box[1]+10, box[2]-50-box[0]+10)
  20. heroR, heroG, heroB = Image.fromarray(hero).split()
  21. pattern = im4.crop((box[0] + 10, box[1] + 10, box[2] - 50, box[3] - 23))
  22. pattern.save("patternRGB.png")
  23. print(herosizex)
  24. print(herosizey)
  25.  
  26. mhist = Image.fromarray(hero).histogram(mask=heroR)
  27. mhistA, mhistAbins = np.histogram(hero, bins=255*3)
  28.  
  29. im = Image.open('img5.png')
  30.  
  31. sizey, sizex = im.size
  32. xjump, yjump = int(herosizex/6), int(herosizey/6)
  33. imA = np.array(im)
  34. value = np.zeros((ceil(sizex/xjump), ceil(sizey/yjump)))
  35.  
  36. start = time.time()
  37. for x in range(0, sizex, xjump):
  38. for y in range(0, sizey, yjump):
  39. bmp = imA[x:x+herosizex, y:y+herosizey]
  40. h, hbins = np.histogram(bmp, bins=255*3)
  41. r = np.array(h) - np.array(mhistA)
  42. #p = np.mat(r)*np.mat(r).transpose()
  43. p = np.dot(r, r)
  44. value[int(x/xjump), int(y/yjump)] = p
  45.  
  46. posx, posy = np.unravel_index(value.argmin(), value.shape)
  47.  
  48. end = time.time()
  49. print('Czas:')
  50. print("{0:.2f}".format(end-start))
  51. print("pozycja:")
  52. print((posx, posy))
  53. draw = ImageDraw.Draw(im)
  54.  
  55. pos = (posy*yjump, posx*xjump, posy*yjump+herosizey, posx*xjump+herosizex)
  56. print(pos)
  57.  
  58. # rysowanie
  59. width = 10
  60. for i in range(width):
  61. draw.rectangle(pos, outline="green")
  62. pos = (pos[0]+1,pos[1]+1, pos[2]+1,pos[3]+1)
  63. im.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement