Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image, ImageDraw
- import numpy as np
- from math import *
- import time
- im1 = np.array(Image.open("src_0.png").convert("RGB"))
- im2 = np.array(Image.open("src_1.png").convert("RGB"))
- im4 = Image.open("src_1.png").convert("RGB")
- mask = np.array(im2)
- mask[np.abs(im1-im2) < 1] = 0
- box = Image.fromarray(mask).getbbox()
- # box[0] += 30
- # box[1] += 10
- # box[2] -= 60
- # box[3] -= 63
- hero = mask[box[1]+10:box[3]-23, box[0]+10:box[2]-50]
- herosizex, herosizey = (box[3]-23-box[1]+10, box[2]-50-box[0]+10)
- heroR, heroG, heroB = Image.fromarray(hero).split()
- pattern = im4.crop((box[0] + 10, box[1] + 10, box[2] - 50, box[3] - 23))
- pattern.save("patternRGB.png")
- print(herosizex)
- print(herosizey)
- mhist = Image.fromarray(hero).histogram(mask=heroR)
- mhistA, mhistAbins = np.histogram(hero, bins=255*3)
- im = Image.open('img5.png')
- sizey, sizex = im.size
- xjump, yjump = int(herosizex/6), int(herosizey/6)
- imA = np.array(im)
- value = np.zeros((ceil(sizex/xjump), ceil(sizey/yjump)))
- start = time.time()
- for x in range(0, sizex, xjump):
- for y in range(0, sizey, yjump):
- bmp = imA[x:x+herosizex, y:y+herosizey]
- h, hbins = np.histogram(bmp, bins=255*3)
- r = np.array(h) - np.array(mhistA)
- #p = np.mat(r)*np.mat(r).transpose()
- p = np.dot(r, r)
- value[int(x/xjump), int(y/yjump)] = p
- posx, posy = np.unravel_index(value.argmin(), value.shape)
- end = time.time()
- print('Czas:')
- print("{0:.2f}".format(end-start))
- print("pozycja:")
- print((posx, posy))
- draw = ImageDraw.Draw(im)
- pos = (posy*yjump, posx*xjump, posy*yjump+herosizey, posx*xjump+herosizex)
- print(pos)
- # rysowanie
- width = 10
- for i in range(width):
- draw.rectangle(pos, outline="green")
- pos = (pos[0]+1,pos[1]+1, pos[2]+1,pos[3]+1)
- im.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement