Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. from PIL import Image, ImageDraw
  2. import numpy as np
  3. from math import *
  4. import matplotlib.pyplot as plt
  5. import matplotlib.patches as patches
  6.  
  7. im1 = np.array(Image.open("src_0.png").convert("RGB"))  # nie ma deadpoola
  8. im2 = np.array(Image.open("src_1.png").convert("RGB"))  # jest deadpool
  9.  
  10. mask = np.array(im2)
  11. mask[np.abs(im1-im2) < 1] = 0
  12.  
  13. box = Image.fromarray(mask).getbbox()
  14. hero = mask[box[1]:box[3], box[0]:box[2]]
  15. herosizex, herosizey = (box[3]-box[1], box[2]-box[0])
  16. heroR, heroG, heroB = Image.fromarray(hero).split()
  17. print('herosize:')
  18. print((herosizex, herosizey))
  19. plt.show(hero)
  20. mhist = Image.fromarray(hero).histogram(mask=heroR)
  21. mhistA, mhistAbins = np.histogram(hero, bins=255*3)
  22.  
  23. ##########
  24. ##########
  25. im = Image.open('img5.png')
  26. print("rozmiar:")
  27. print(im.size)
  28. sizey, sizex = im.size
  29. xjump, yjump = int(herosizex/2), int(herosizey/2)
  30. imA = np.array(im)
  31. value = np.zeros((ceil(sizex/xjump), ceil(sizey/yjump)))
  32. for x in range(0, sizex, xjump):
  33.     for y in range(0, sizey, yjump):
  34.         bmp = imA[x:x+herosizex, y:y+herosizey]
  35.         h, hbins = np.histogram(bmp, bins=255*3)
  36.         r = np.array(h) - np.array(mhistA)
  37.         p = np.dot(r, r)
  38.         value[int(x/xjump), int(y/yjump)] = p
  39.  
  40. print("Wartosc minimalna bledu: %d", value.min())
  41. posx, posy = np.unravel_index(value.argmin(), value.shape)
  42. print("pozycja:")
  43. print((posx, posy))
  44. draw = ImageDraw.Draw(im)
  45.  
  46. # Create figure and axes
  47. fig1, ((ax1, ax2), (ax3, ax4), (ax5, ax6)) = plt.subplots(3, 2, sharex='col', sharey='row')
  48.  
  49. # Display the image
  50. ax1.imshow(im)
  51.  
  52. # Add a Rectangle patch
  53. x1, y1, x2, y2 = (posy*yjump, posx*xjump, posy*yjump+herosizey, posx*xjump+herosizex)
  54. width = x2 - x1
  55. height = y2 - y1
  56. ax1.add_patch(patches.Rectangle((x1, y1), width, height, linewidth=2, edgecolor='green', facecolor='none'))
  57.  
  58. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement