-
Notifications
You must be signed in to change notification settings - Fork 9
/
dataset.py
135 lines (110 loc) · 4.16 KB
/
dataset.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
import numpy as np
import cv2
class Dataset:
def __init__(self, scale=10.0, shuffle=False, use_rao=True):
self.load_images(scale, use_rao)
if shuffle:
indices = np.random.permutation(len(self.patches))
self.patches = self.patches[indices]
self.mask = self.create_gauss_mask()
def load_images(self, scale, use_rao):
images = []
if use_rao:
# Use images from the paper
dir_name = "images_rao"
else:
dir_name = "images_org"
for i in range(5):
image = cv2.imread("data/{}/image{}.png".format(dir_name, i))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype(np.float32)
images.append(image)
images = np.array(images)
self.load_sub(images, scale)
def create_gauss_mask(self, sigma=0.4):
""" Create gaussian mask. """
width = 16
mask = [0.0] * (width * width)
hw = width // 2
for i in range(width):
x = (i - hw) / float(hw)
for j in range(width):
y = (j - hw) / float(hw)
r = np.sqrt(x*x + y*y)
mask[j*width + i] = self.gauss(r, sigma=sigma)
mask = np.array(mask)
# Normalize
mask = mask / np.max(mask)
return mask
def gauss(self, x, sigma):
sigma_sq = sigma * sigma
return 1.0 / np.sqrt(2.0 * np.pi * sigma_sq) * np.exp(-x*x/(2 * sigma_sq))
def load_sub(self, images, scale):
self.images = images
filtered_images = []
for image in images:
filtered_image = self.apply_DoG_filter(image)
filtered_images.append(filtered_image)
self.filtered_images = filtered_images
w = images.shape[2]
h = images.shape[1]
size_w = w // 26
size_h = h // 16
patches = np.empty((size_h * size_w * len(images), 16, 26), dtype=np.float32)
for image_index, filtered_image in enumerate(filtered_images):
for j in range(size_h):
y = 16 * j
for i in range(size_w):
x = 26 * i
patch = filtered_image[y:y+16, x:x+26]
# (16, 26)
# print(patch.shape)
index = size_w*size_h*image_index + j*size_w + i
patches[index] = patch
patches = patches * scale
self.patches = patches
def get_images_from_patch(self, patch, use_mask=True):
images = []
for i in range(3):
x = 5 * i
# Apply gaussian mask
image = patch[:, x:x+16].reshape([-1])
if use_mask:
image = image * self.mask
images.append(image)
return images
def get_images(self, patch_index):
patch = self.patches[patch_index]
return self.get_images_from_patch(patch)
def apply_DoG_filter(self, gray, ksize=(5,5), sigma1=1.3, sigma2=2.6):
"""
Apply difference of gaussian (DoG) filter detect edge of the image.
"""
g1 = cv2.GaussianBlur(gray, ksize, sigma1)
g2 = cv2.GaussianBlur(gray, ksize, sigma2)
return g1 - g2
def get_bar_images(self, is_short):
patch = self.get_bar_patch(is_short)
return self.get_images_from_patch(patch, use_mask=True)
def get_bar_patch(self, is_short):
"""
Get bar patch image for end stopping test.
"""
bar_patch = np.ones((16,26), dtype=np.float32)
if is_short:
bar_width = 6
else:
bar_width = 24
bar_height = 2
for x in range(bar_patch.shape[1]):
for y in range(bar_patch.shape[0]):
if x >= 26/2 - bar_width/2 and \
x < 26/2 + bar_width/2 and \
y >= 16/2 - bar_height/2 and \
y < 16/2 + bar_height/2:
bar_patch[y,x] = -1.0
# Sete scale with stddev of all patch images.
scale = np.std(self.patches)
# Original scaling value for bar
bar_scale = 2.0
return bar_patch * scale * bar_scale