-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeature2.py
executable file
·247 lines (206 loc) · 6.89 KB
/
feature2.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python
# -*- coding: utf-8 -*-
######
#
# Mail npuxpli@mail.nwpu.edu.cn
# Author LiXiping
# Date 2019/09/20 16:19:34
#
######
import cv2
import math
import numpy as np
from numpy import linalg as la
from skimage import measure
from PIL import Image
def get_svd(img_path):
img = cv2.imread(img_path, 0)
U,sigma,VT=la.svd(img)
# print(sigma)
#print("svd sigma")
#print(type(sigma))
# sigma[::-1].sort()
sigma = sigma[:10]
return sigma
def get_hu(img_path):
img = cv2.imread(img_path, 0)
moments = cv2.moments(img)
humoments = cv2.HuMoments(moments)
# humoments = np.log(np.abs(humoments))
humoments = humoments.transpose()[0]
hu1 = np.log(np.abs(humoments[0]))
hu2 = np.log(np.abs(humoments[1]))
hu = '{},{}'.format(hu1, hu2)
hu = hu.strip(',').split(',')
#print(hu)
return hu
def get_mr(img_path):
def im2double(im):
min_val = np.min(im)
max_val = np.max(im)
#out = (im.astype(np.float32) - min_val) / (max_val - min_val)
out = im.astype(np.float32)
return out
image_in = cv2.imread(img_path, 0)
# Otsu滤波
threshold, image_BW = cv2.threshold(image_in ,0,255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
orign_image=im2double(np.array(image_in))
binary_image=im2double(np.array(image_BW))
[m, n] = image_in.shape
sum_foreground=0
sum_background=0
count_foreground=0
count_background=0
for row in range(m):
for col in range(n):
if binary_image[row,col] != 0:
sum_foreground = sum_foreground + orign_image[row,col]
count_foreground = count_foreground + 1
else:
sum_background = sum_background + orign_image[row,col]
count_background = count_background + 1
m1 = sum_foreground / count_foreground
m2 = sum_background / count_background
if m2 != 0:
MR = m1 / m2
else:
MR = 0
return np.array([MR])
def get_pzor(img_path):
def im2double(im):
min_val = np.min(im)
max_val = np.max(im)
out = (im.astype(np.float32) - min_val) / (max_val - min_val)
return out
image_in = cv2.imread(img_path, 0)
threshold, image_BW = cv2.threshold(image_in ,0,255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
orign_image=im2double(np.array(image_in))
binary_image=im2double(np.array(image_BW))
[m,n] = image_in.shape
count = 0
light = 0
B = np.ones((m,n))*0
for row in range(m):
for col in range(n):
if binary_image[row , col] != 0:
B[row, col] = orign_image[row,col]
count = count + 1
thresh =(np.max(B))*0.9
for row in range(m):
for col in range(n):
if orign_image[row , col] > thresh:
light = light + 1
light = float(light)
count = float(count)
# print(light)
# print(count)
Pzor = float(light/count)
return np.array([Pzor])
def get_minrectangle(img_path):
image_in = cv2.imread(img_path, 0)
threshold, image_BW = cv2.threshold(image_in ,0,255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
labels = measure.label(image_BW, connectivity=2)
regions = measure.regionprops(labels)
area_max = 0
for r in regions:
if r.area > area_max:
area_max = r.area
x = float(r.bbox[0])
y = float(r.bbox[1])
width = float(r.bbox[3] - r.bbox[1])
height = float(r.bbox[2] - r.bbox[0])
ration = float(height /width)
extent = r.extent
minrectangle = '{},{},{},{},{},{},{}'.format(x, y, width, height, area_max, ration, extent)
minrectangle = minrectangle.strip(',').split(',')
#print(minrectangle)
return minrectangle
def get_sift(img_path):
img = cv2.imread(img_path , 0)
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(img , None)
num = len(kp)
return np.array([num])
def get_harris(img_path):
img = cv2.imread(img_path , 0)
img = np.float32(img)
dst = cv2.cornerHarris(img ,2 ,3,0.04)
dst = cv2.dilate(dst , None)
num = len(dst)
return np.array([num])
def truncate_descriptor(descriptors, degree):
"""this function truncates an unshifted fourier descriptor array
and returns one also unshifted"""
descriptors = np.fft.fftshift(descriptors)
center_index = len(descriptors) / 2
descriptors = descriptors[
center_index - degree / 2:center_index + degree / 2]
descriptors = np.fft.ifftshift(descriptors)
return descriptors
def reconstruct(descriptors, degree):
""" reconstruct(descriptors, degree) attempts to reconstruct the image
using the first [degree] descriptors of descriptors"""
# truncate the long list of descriptors to certain length
descriptor_in_use = truncate_descriptor(descriptors, degree)
contour_reconstruct = np.fft.ifft(descriptor_in_use)
contour_reconstruct = np.array(
[contour_reconstruct.real, contour_reconstruct.imag])
contour_reconstruct = np.transpose(contour_reconstruct)
contour_reconstruct = np.expand_dims(contour_reconstruct, axis=1)
# make positive
if contour_reconstruct.min() < 0:
contour_reconstruct -= contour_reconstruct.min()
# normalization
contour_reconstruct *= 800 / contour_reconstruct.max()
# type cast to int32
contour_reconstruct = contour_reconstruct.astype(np.int32, copy=False)
black = np.zeros((800, 800), np.uint8)
# draw and visualize
# cv2.drawContours(black, contour_reconstruct, -1, 255, thickness=-1)
# cv2.imshow("black", black)
# cv2.waitKey(1000)
# cv2.imwrite("reconstruct_result.jpg", black)
# cv2.destroyAllWindows()
return descriptor_in_use
# def addNoise(descriptors):
# """this function adds gaussian noise to descriptors
# descriptors should be a [N,2] numpy array"""
# scale = descriptors.max() / 10
# noise = np.random.normal(0, scale, descriptors.shape[0])
# noise = noise + 1j * noise
# descriptors += noise
def get_fourier(img_path):
img = cv2.imread(img_path , 0)
retval, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
contour = []
_, contour, hierarchy = cv2.findContours(
img,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE,
contour
)
contour_array = contour[0][:, 0, :]
contour_complex = np.empty(contour_array.shape[:-1], dtype=complex)
contour_complex.real = contour_array[: , 0]
contour_complex.imag = contour_array[: , 1]
fourier_result = np.fft.fft(contour_complex)
contour = reconstruct(fourier_result, 2)
#addNoise(contour)
contour = np.absolute(contour)
return contour
# path = '063.bmp'
# # a = Image.open(path)
# # a = np.array(a)
# s = get_svd(path)
# print(s)
# a,b,c = get_svd(path)
# a.shape = (328,1)
# # a = np.transpose(a)
# print(a.shape)
# d = b[:,:]*a[50:]*c[:50]
# print(a)
# print(type(contour))
# print(contour)