-
Notifications
You must be signed in to change notification settings - Fork 1
/
Image_process.py
235 lines (204 loc) · 6.65 KB
/
Image_process.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/24 16:39
# @Author : Deyu Tian
# @Site :
# @File : Image_process.py
# @Software: PyCharm Community Edition
import cv2
import numpy as np
import Image
def draw_hist(impath):
"""
:param impath:
:return:
"""
img = Image.img2array(impath)
#img = cv2.imread(impath)
h = np.zeros((img.shape[0], img.shape[1], 1))
bins = np.arange(256).reshape(256, 1)
color = [(255, 0, 0)]
hist_item = cv2.calcHist(img, 0, None, [256], [0, 255])
cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX)
hist = np.int32(np.around(hist_item))
pts = np.column_stack((bins, hist))
cv2.polylines(h, [pts], False, color)
h = np.flipud(h)
cv2.imshow('colorhist', h)
cv2.waitKey(0)
pass
def rgb2hsi(rgb_lwpImg):
"""
:param rgb_lwpImg: arrays of input RGB Image
:return:
"""
rows = int(rgb_lwpImg.shape[0])
cols = int(rgb_lwpImg.shape[1])
b, g, r = cv2.split(rgb_lwpImg)
# 归一化到[0,1]
b = b / 255.0
g = g / 255.0
r = r / 255.0
hsi_lwpImg = rgb_lwpImg.copy()
H, S, I = cv2.split(hsi_lwpImg)
for i in range(rows):
for j in range(cols):
num = 0.5 * ((r[i, j]-g[i, j])+(r[i, j]-b[i, j]))
den = np.sqrt((r[i, j]-g[i, j])**2+(r[i, j]-b[i, j])*(g[i, j]-b[i, j]))
theta = float(np.arccos(num/den))
if den == 0:
H = 0
elif b[i, j] <= g[i, j]:
H = theta
else:
H = 2*3.14169265 - theta
min_RGB = min(min(b[i, j], g[i, j]), r[i, j])
sum = b[i, j]+g[i, j]+r[i, j]
if sum == 0:
S = 0
else:
S = 1 - 3*min_RGB/sum
H = H/(2*3.14159265)
I = sum/3.0
# 输出HSI图像,扩充到255以方便显示,一般H分量在[0,2pi]之间,S和I在[0,1]之间
hsi_lwpImg[i, j, 0] = H*255
hsi_lwpImg[i, j, 1] = S*255
hsi_lwpImg[i, j, 2] = I*255
return hsi_lwpImg
def band_9_neibour_layers_ignoreedge(bandarr):
"""
drop edge pixels for faster diff map generation
:param bandarr:
:return:
"""
pass
ul = np.zeros((bandarr.shape[0], bandarr.shape[1]), dtype='f')
up = np.zeros_like(ul)
ur = np.zeros_like(ul)
left = np.zeros_like(ul)
right = np.zeros_like(ul)
dl = np.zeros_like(ul)
down = np.zeros_like(ul)
dr = np.zeros_like(ul)
for i in range(1, bandarr.shape[0]-1):
for j in range(1, bandarr.shape[1]-1):
ul[i, j] = bandarr[i - 1, j - 1]
up[i, j] = bandarr[i - 1, j]
left[i, j] = bandarr[i, j - 1]
dl[i, j] = bandarr[i + 1, j - 1]
ur[i, j] = bandarr[i - 1, j + 1]
right[i, j] = bandarr[i, j+1]
down[i, j] = bandarr[i + 1, j]
dr[i, j] = bandarr[i + 1, j + 1]
#print(np.stack((bandarr, ul, up, ur, left, right, dl, down, dr)).shape)
returnarr = np.moveaxis(np.stack((bandarr, ul, up, ur, left, right, dl, down, dr)), 0, 2)
print(returnarr.shape)
return returnarr
def band_9_neibour_layers(bandarr):
"""
input band, return stacked layers of pixels' 9-neighbourhood
:param bandarr:
:return:
"""
#print(bandarr.shape[0], bandarr.shape[1])
if isinstance(bandarr, float):
print("band_9_neibour_layers:band datatype changed to int!")
bandarr = bandarr.astype('i')
ul = np.zeros((bandarr.shape[0], bandarr.shape[1]), dtype='i')
up = np.zeros_like(ul)
ur = np.zeros_like(ul)
left = np.zeros_like(ul)
right = np.zeros_like(ul)
dl = np.zeros_like(ul)
down = np.zeros_like(ul)
dr = np.zeros_like(ul)
for i in range(bandarr.shape[0]):
for j in range(bandarr.shape[1]):
if i < 1 or j < 1:
ul[i, j] = -1
else:
ul[i, j] = bandarr[i - 1, j - 1]
if i < 1:
up[i, j] = -1
else:
up[i, j] = bandarr[i - 1, j]
if j < 1:
left[i, j] = -1
else:
left[i, j] = bandarr[i, j - 1]
if j < 1 or i == bandarr.shape[0]-1:
dl[i, j] = -1
else:
dl[i, j] = bandarr[i + 1, j - 1]
if i < 1 or j == bandarr.shape[1]-1:
ur[i, j] = -1
else:
ur[i, j] = bandarr[i - 1, j + 1]
if j == bandarr.shape[1]-1:
right[i, j] = -1
else:
right[i, j] = bandarr[i, j+1]
if i == bandarr.shape[0]-1:
down[i, j] = -1
else:
down[i, j] = bandarr[i + 1, j]
if i == bandarr.shape[0]-1 or j == bandarr.shape[1]-1:
dr[i, j] = -1
else:
dr[i, j] = bandarr[i + 1, j + 1]
return np.moveaxis(np.stack((bandarr, ul, up, ur, left, right, dl, down, dr)), 0, 2)
def neibour_4_grab():
pass
def rescaler():
pass
def unimodal_thre_rosin():
"""
rosin's methods
:return:
"""
pass
def unimodal_thre_T_point():
"""
T-point algrithom
:return:
"""
pass
def bimodal_threshood():
pass
def sobel(src, kernel_size):
"""
:param: src: input lumination grayscale image
:param:kernel_size: 1 3 5 7
:return: output gradients
"""
if kernel_size != 1 and kernel_size != 3 and kernel_size != 5 and kernel_size != 7:
print("INPUT Params ERROR: kernel_size must be 1 or 3 or 5 or 7!")
grad_x = cv2.Sobel(src, cv2.CV_64F, 1, 0, ksize=kernel_size)
grad_y = cv2.Sobel(src, cv2.CV_64F, 0, 1, ksize=kernel_size)
grad = cv2.Sobel(src, cv2.CV_64F, 1, 1, ksize=kernel_size)
grad_abs = np.absolute(grad)
grad_8u = np.uint8(grad_abs)
grad_bi = np.sqrt(np.power(grad_x, 2) + np.power(grad_y, 2))
if grad_8u.all() == grad_bi.all():
print("got gradiation without seperate by x and y!")
print(np.max(grad), np.max(grad_8u))
return grad_8u
else:
print("need seperately x and y grad to generate total gradiation!")
return grad_bi
def conn_4_neibour(img):
"""
:param img:
:return:
"""
ret, labels = cv2.connectedComponents(img, connectivity=4)
# Map component labels to hue val
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue == 0] = 0
cv2.imshow('labeled.png', labeled_img)
cv2.waitKey()