-
Notifications
You must be signed in to change notification settings - Fork 0
/
gendiffsmatstrainfull.py
116 lines (94 loc) · 3.28 KB
/
gendiffsmatstrainfull.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
from skimage.io import imread, imsave, imshow
import copy
import random
#from skimage.measure import compare_mse, compare_ssim
import cv2
import numpy as np
#from skimage.transform import resize
import itertools
import torch
import pickle
from pyemd import emd_samples
#from skimage.color import rgb2gray, gray2rgb
#from skimage import img_as_ubyte
#from threading import Thread
def concat(xss):
new = []
for xs in xss:
new.extend(xs)
return new
orb = cv2.ORB_create(10000)
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
def toMats():
images = sorted(list(os.walk("facades/train/"))[-1][2])
for (index, image) in enumerate(images):
print(index)
img = imread("facades/train/" + image)
imsave("imgread.png", img)
img = img[:,:,:3]
tot_height = img.shape[0]
tot_width = img.shape[1]
orb_val = orb.detectAndCompute(img, None)
divisions = 15
H = [int((tot_height+0.0)*i/divisions) for i in range(divisions)] + [tot_height]
W = [int((tot_width+0.0)*i/divisions) for i in range(divisions)] + [tot_width]
H.sort()
W.sort()
img_mat_loose = np.zeros((divisions,divisions,divisions,divisions))
img_mat_tight = np.zeros((divisions,divisions,divisions,divisions))
img_mat_looser = np.zeros((divisions, divisions, divisions, divisions))
img_third = np.zeros((divisions, divisions, divisions, divisions))
contrast = 64
brightness = 127
sifts = {}
pix = {}
for i in range(divisions):
for j in range(divisions):
pix[(i,j)] = np.concatenate(img[H[i]:H[i + 1], W[j]:W[j+1]])
sift_indices = []
for k in range(len(orb_val[0])):
if orb_val[0][k].pt[0] >= i*tot_height/divisions and orb_val[0][k].pt[0] < (i+1)*tot_height/divisions \
and orb_val[0][k].pt[1] >= j*tot_width/divisions and orb_val[0][k].pt[1] < (j+1)*tot_width/divisions:
sift_indices.append(k)
sifts[(i,j)] = np.asarray([orb_val[1][k] for k in sift_indices], dtype="uint8")
for i1 in range((divisions)):
for j1 in range((divisions)):
for i2 in range(i1, (divisions)):
for j2 in range((divisions)):
if not ((i1 == i2) and (j1 == j2)):
e = emd_samples(pix[(i1,j1)], pix[(i2,j2)])
a = sifts[(i1,j1)]
b = sifts[(i2,j2)]
a_pix = pix[(i1,j1)]
b_pix = pix[(i2,j2)]
if not (len(a) == 0 or len(b) == 0):
matches = bf.knnMatch(a, b, k=2)
if len(matches) > 0 and len(matches[0]) == 2:
good = []
for m,n in matches:
if m.distance < 50.0:
good.append([m])
if len(good) > 5 and e < 25.0:
img_mat_tight[i1,j1,i2,j2] = 1
elif len(good) > 5 and e < 35.0:
img_mat_loose[i1,j1,i2,j2] = 1
img_mat_looser[i1,j1,i2,j2] = 1
e = emd_samples(pix[(i1,j1)], pix[(i2,j2)])
if e < 15:
img_mat_tight[i1,j1,i2,j2] = 1
img_mat_loose[i1,j1,i2,j2] = 1
img_mat_looser[i1,j1,i2,j2] = 1
elif e < 25:
img_mat_loose[i1,j1,i2,j2] = 1
img_mat_looser[i1,j1,i2,j2] = 1
elif e < 30:
img_mat_loose[i1,j1,i2,j2] = 1
if j1 < 5 and j2 < 5:
img_third[i1,j1,i2,j2] = img_mat_loose[i1,j1,i2,j2]
else:
pass #print((j1,j2))
#np.save("mat_good.npy", img_mat_loose)
np.save("buildings_mat_train_full/mat-" + image.split(".")[0] + ".npy", img_mat_loose)
print("saved")
toMats()