|
a |
|
b/features/FeatureExtract.py |
|
|
1 |
''' |
|
|
2 |
Created by Wang Qiuli |
|
|
3 |
|
|
|
4 |
2020/5/23 |
|
|
5 |
''' |
|
|
6 |
|
|
|
7 |
import cv2 |
|
|
8 |
import numpy as np |
|
|
9 |
import matplotlib.pyplot as plt |
|
|
10 |
import features.slic |
|
|
11 |
import features.rgb_gr |
|
|
12 |
from PIL import Image |
|
|
13 |
from skimage import io, color |
|
|
14 |
from skimage import data, filters |
|
|
15 |
|
|
|
16 |
|
|
|
17 |
def truncate_hu(image_array, max = 400, min = -900): |
|
|
18 |
image = image_array.copy() |
|
|
19 |
image[image > max] = max |
|
|
20 |
image[image < min] = min |
|
|
21 |
return image |
|
|
22 |
|
|
|
23 |
def hist(img): |
|
|
24 |
''' |
|
|
25 |
return histgram values |
|
|
26 |
1 * 128 |
|
|
27 |
''' |
|
|
28 |
img = truncate_hu(img) |
|
|
29 |
hist = cv2.calcHist([img],[0],None,[128],[-900,400]) |
|
|
30 |
# print(hist.shape) |
|
|
31 |
# plt.subplot(121) |
|
|
32 |
# plt.imshow(img,'gray') |
|
|
33 |
# plt.xticks([]) |
|
|
34 |
# plt.yticks([]) |
|
|
35 |
# plt.title("Original") |
|
|
36 |
# plt.subplot(122) |
|
|
37 |
# plt.hist(img.ravel(),128,[-900,400]) |
|
|
38 |
# plt.show() |
|
|
39 |
return hist |
|
|
40 |
|
|
|
41 |
def gray2rgb(rgb,imggray): |
|
|
42 |
R = rgb[:,:,0] |
|
|
43 |
G = rgb[:,:,1] |
|
|
44 |
B = ((imggray) - 0.299 * R - 0.587 * G) / 0.114 |
|
|
45 |
|
|
|
46 |
grayRgb = np.zeros((rgb.shape)) |
|
|
47 |
grayRgb[:, :, 2] = B |
|
|
48 |
grayRgb[:, :, 0] = R |
|
|
49 |
grayRgb[:, :, 1] = G |
|
|
50 |
|
|
|
51 |
return grayRgb |
|
|
52 |
|
|
|
53 |
def super_pixel(img): |
|
|
54 |
''' |
|
|
55 |
return super_pixel images |
|
|
56 |
img w * h |
|
|
57 |
''' |
|
|
58 |
img = truncate_hu(img) |
|
|
59 |
# io.imsave('ori.png', img) |
|
|
60 |
img = np.expand_dims(img, 2) |
|
|
61 |
# # print(img.shape) |
|
|
62 |
rgb = np.concatenate((img, img, img), 2) |
|
|
63 |
# io.imsave('ori2.png', rgb) |
|
|
64 |
obj = slic.SLICProcessor(rgb, 4096, 5) |
|
|
65 |
res = obj.iterate_10times() |
|
|
66 |
return res |
|
|
67 |
|
|
|
68 |
def standard_deviation(img): |
|
|
69 |
hist_value = hist(img) |
|
|
70 |
std = np.std(hist_value) |
|
|
71 |
# print(std) |
|
|
72 |
|
|
|
73 |
return std |
|
|
74 |
|
|
|
75 |
def edge_detection(img): |
|
|
76 |
''' |
|
|
77 |
edge detection |
|
|
78 |
''' |
|
|
79 |
img = truncate_hu(img) |
|
|
80 |
# io.imsave('ori.png', img) |
|
|
81 |
# img = np.expand_dims(img, 2) |
|
|
82 |
# # # print(img.shape) |
|
|
83 |
# rgb = np.concatenate((img, img, img), 2) |
|
|
84 |
|
|
|
85 |
# gray= cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY) |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
x = cv2.Sobel(img, cv2.CV_16S, 1, 0) |
|
|
89 |
y = cv2.Sobel(img, cv2.CV_16S, 0, 1) |
|
|
90 |
z = cv2.Sobel(img, cv2.CV_16S, 1, 1) |
|
|
91 |
|
|
|
92 |
return x |
|
|
93 |
# io.imsave('canny1.png', x) |
|
|
94 |
# io.imsave('canny2.png', y) |
|
|
95 |
# io.imsave('canny3.png', z) |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
def gabor(img): |
|
|
99 |
filt_real, filt_imag = filters.gabor(img,frequency=0.6) |
|
|
100 |
# io.imsave('filt_imag.png', filt_imag) |
|
|
101 |
return filt_imag |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
def threshold_void(img): |
|
|
105 |
void = truncate_hu(img, -600, -900) |
|
|
106 |
# io.imsave('void.png', void) |
|
|
107 |
return void |
|
|
108 |
|
|
|
109 |
def normalization(image_array): |
|
|
110 |
image_array = image_array + 900 |
|
|
111 |
|
|
|
112 |
max = 1300 |
|
|
113 |
min = 0 |
|
|
114 |
image_array = (image_array-min)/(max-min) # float cannot apply the compute,or array error will occur |
|
|
115 |
# avg = image_array.mean() |
|
|
116 |
# image_array = image_array-avg |
|
|
117 |
image_array = image_array * 255 |
|
|
118 |
return image_array # a bug here, a array must be returned,directly appling function did't work |
|
|
119 |
|
|
|
120 |
def toGrey(img): |
|
|
121 |
''' |
|
|
122 |
get grey-level images |
|
|
123 |
0-256 |
|
|
124 |
''' |
|
|
125 |
img = truncate_hu(img) |
|
|
126 |
|
|
|
127 |
img_nor = normalization(img) |
|
|
128 |
# io.imsave('img_nor.png', img_nor) |
|
|
129 |
return img_nor |
|
|
130 |
|
|
|
131 |
def OTSU(img): |
|
|
132 |
gray = toGrey(img) |
|
|
133 |
print(gray) |
|
|
134 |
|
|
|
135 |
gray.dtype="int16" |
|
|
136 |
print('int16') |
|
|
137 |
print(gray) |
|
|
138 |
|
|
|
139 |
retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU) |
|
|
140 |
io.imsave('OTSU.png', dst) |