-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgabor_data.py
More file actions
89 lines (76 loc) · 2.47 KB
/
gabor_data.py
File metadata and controls
89 lines (76 loc) · 2.47 KB
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
import PIL
#import zipfile
import numpy as np
#import pandas as pd
import matplotlib.pyplot as plt
import pickle
import h5py
import cv2
def gabor(img):
# cv2.getGaborKernel(ksize, sigma, theta, lambda, gamma, psi, ktype)
# ksize - size of gabor filter (n, n)
# sigma - standard deviation of the gaussian function
# theta - orientation of the normal to the parallel stripes
# lambda - wavelength of the sunusoidal factor
# gamma - spatial aspect ratio
# psi - phase offset
# ktype - type and range of values that each pixel in the gabor kernel can hold
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ksize =( 5,5)
sigma = 1
theta = 6*np.pi/4
lamda = np.pi/4
g_kernel = cv2.getGaborKernel(ksize, sigma, theta, lamda, 0.5, 0, ktype=cv2.CV_32F)
#g_kernel /= 1.5*g_kernel.sum()
#img = cv2.imread("C:/Users/HP/desktop/pytry/new_dataset/bt_images/5.jpg")
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
filtered_img = cv2.filter2D(img, cv2.CV_8UC3, g_kernel)
return filtered_img
labels = []
images = []
gabor_data = []
#Save images of brain tumor, masks and store labels and borders in their respective lists iteratively.
filename = None
for filename in range(1, 3065):
with h5py.File('dataset/{}.mat'.format(filename), 'r') as f:
label = f['cjdata']['label'][0][0]
labels.append(int(label))
img = f['cjdata']['image']
img = np.array(img, dtype=np.float32)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
img = cv2.resize(img, (512, 512))
gabor_img = gabor(img)
images.append(gabor_img)
print(gabor_img.shape)
"""
plt.axis('off')
plt.imsave("new_dataset/gabor_images/{}.jpg".format(filename), gabor_img, cmap='gray')
"""
#Convert the Python lists to a Numpy arrays
labels = np.array(labels, dtype=np.int64)
print("labels sorted...",len(labels))
print(labels[0]," to ",labels[3063])
"""
for filename in range(1,3065):
#img = cv2.imread("new_dataset/bt_images/{}.jpg".format(filename))
img = cv2.resize(img, (512, 512))
gabor_img = gabor(img)
images.append(gabor_img)
print(gabor_img.shape)
"""
print("images sorted...",len(images))
#plot random image to test
plt.subplot(121),plt.imshow(images[654]),plt.title('Original')
plt.show()
for i in range(0, 3064):
label = labels[i]
img = images[i]
gabor_data.append([img, label])
print("data matrix ready...",len(gabor_data))
images = None
labels = None
pickle_out = open("new_dataset/gabor_data.pickle","wb")
print("gabor_pickle opened")
pickle.dump(gabor_data, pickle_out)
pickle_out.close()
print("done")