[cf6a9e]: / features / FeatureExtract.py

Download this file

141 lines (113 with data), 3.1 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
 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
136
137
138
139
140
'''
Created by Wang Qiuli
2020/5/23
'''
import cv2
import numpy as np
import matplotlib.pyplot as plt
import features.slic
import features.rgb_gr
from PIL import Image
from skimage import io, color
from skimage import data, filters
def truncate_hu(image_array, max = 400, min = -900):
image = image_array.copy()
image[image > max] = max
image[image < min] = min
return image
def hist(img):
'''
return histgram values
1 * 128
'''
img = truncate_hu(img)
hist = cv2.calcHist([img],[0],None,[128],[-900,400])
# print(hist.shape)
# plt.subplot(121)
# plt.imshow(img,'gray')
# plt.xticks([])
# plt.yticks([])
# plt.title("Original")
# plt.subplot(122)
# plt.hist(img.ravel(),128,[-900,400])
# plt.show()
return hist
def gray2rgb(rgb,imggray):
R = rgb[:,:,0]
G = rgb[:,:,1]
B = ((imggray) - 0.299 * R - 0.587 * G) / 0.114
grayRgb = np.zeros((rgb.shape))
grayRgb[:, :, 2] = B
grayRgb[:, :, 0] = R
grayRgb[:, :, 1] = G
return grayRgb
def super_pixel(img):
'''
return super_pixel images
img w * h
'''
img = truncate_hu(img)
# io.imsave('ori.png', img)
img = np.expand_dims(img, 2)
# # print(img.shape)
rgb = np.concatenate((img, img, img), 2)
# io.imsave('ori2.png', rgb)
obj = slic.SLICProcessor(rgb, 4096, 5)
res = obj.iterate_10times()
return res
def standard_deviation(img):
hist_value = hist(img)
std = np.std(hist_value)
# print(std)
return std
def edge_detection(img):
'''
edge detection
'''
img = truncate_hu(img)
# io.imsave('ori.png', img)
# img = np.expand_dims(img, 2)
# # # print(img.shape)
# rgb = np.concatenate((img, img, img), 2)
# gray= cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
z = cv2.Sobel(img, cv2.CV_16S, 1, 1)
return x
# io.imsave('canny1.png', x)
# io.imsave('canny2.png', y)
# io.imsave('canny3.png', z)
def gabor(img):
filt_real, filt_imag = filters.gabor(img,frequency=0.6)
# io.imsave('filt_imag.png', filt_imag)
return filt_imag
def threshold_void(img):
void = truncate_hu(img, -600, -900)
# io.imsave('void.png', void)
return void
def normalization(image_array):
image_array = image_array + 900
max = 1300
min = 0
image_array = (image_array-min)/(max-min) # float cannot apply the compute,or array error will occur
# avg = image_array.mean()
# image_array = image_array-avg
image_array = image_array * 255
return image_array # a bug here, a array must be returned,directly appling function did't work
def toGrey(img):
'''
get grey-level images
0-256
'''
img = truncate_hu(img)
img_nor = normalization(img)
# io.imsave('img_nor.png', img_nor)
return img_nor
def OTSU(img):
gray = toGrey(img)
print(gray)
gray.dtype="int16"
print('int16')
print(gray)
retval, dst = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)
io.imsave('OTSU.png', dst)