-
Notifications
You must be signed in to change notification settings - Fork 276
/
tutorial75-Extracting_features_using_Gabor_Filter.py
48 lines (37 loc) · 2.27 KB
/
tutorial75-Extracting_features_using_Gabor_Filter.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
# https://youtu.be/ywyomOyXpxg
import numpy as np
import cv2
import pandas as pd
#img = cv2.imread('BSE_Image.jpg')
img = cv2.imread('images/synthetic.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Here, if you have multichannel image then extract the right channel instead of converting the image to grey.
#For example, if DAPI contains nuclei information, extract the DAPI channel image first.
#Multiple images can be used for training. For that, you need to concatenate the data
#Save original image pixels into a data frame. This is our Feature #1.
img2 = img.reshape(-1)
df = pd.DataFrame()
df['Original Image'] = img2
#Generate Gabor features
num = 1 #To count numbers up in order to give Gabor features a lable in the data frame
kernels = [] #Create empty list to hold all kernels that we will generate in a loop
for theta in range(8): #Define number of thetas. Here only 2 theta values 0 and 1/4 . pi
theta = theta / 4. * np.pi
for sigma in (1, 3, 5, 7): #Sigma with values of 1 and 3
for lamda in np.arange(0, np.pi, np.pi / 4): #Range of wavelengths
for gamma in (0.05, 0.5): #Gamma values of 0.05 and 0.5
gabor_label = 'Gabor' + str(num) #Label Gabor columns as Gabor1, Gabor2, etc.
# print(gabor_label)
ksize=5 #Try 15 for hidden image. Or 9 for others
phi = 0 #0.8 for hidden image. Otherwise leave it to 0
kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)
kernels.append(kernel)
#Now filter the image and add values to a new column
fimg = cv2.filter2D(img2, cv2.CV_8UC3, kernel)
filtered_img = fimg.reshape(-1)
cv2.imwrite('images/gabor_filtered_images/'+gabor_label+'.jpg', filtered_img.reshape(img.shape))
df[gabor_label] = filtered_img #Labels columns as Gabor1, Gabor2, etc.
print(gabor_label, ': theta=', theta, ': sigma=', sigma, ': lamda=', lamda, ': gamma=', gamma)
num += 1 #Increment for gabor column label
print(df.head())
#df.to_csv("Gabor.csv")