-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageTool.py
182 lines (147 loc) · 4.71 KB
/
ImageTool.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
# -*- coding: utf-8 -*-
# @FileName: ImageTool.py
# @Time : 2021-03-08 19:59
# @Author : Dorad, [email protected]
# @Blog :https://blog.cuger.cn
'''
this file provide some function for image transform
'''
import numpy as np
from PyQt5.QtCore import QPointF
from PyQt5.QtGui import QImage, QPolygonF, QPixmap
from skimage import color
def QImageToGrayByChannel(qimage: QImage, channel, isNdarray=False):
if not channel in ['RGB', 'Gray', 'Red', 'Green', 'Blue']:
return False
if channel == 'RGB':
return qimage
imageArray = QImage2NArray(qimage)
imageArray = NAImage2GrayByChannel(imageArray, channel)
imageArray = np.squeeze(imageArray)
if isNdarray:
return imageArray
return NArray2QImage(imageArray)
def NAImage2GrayByChannel(image, channel='Gray'):
channel = channel.upper()
if not channel in ['GRAY', 'RED', 'GREEN', 'BLUE']:
return False
if channel == 'GRAY':
imageArray = np.mean(image, axis=2)
elif channel == 'RED':
imageArray = image[:, :, 0]
elif channel == 'GREEN':
imageArray = image[:, :, 1]
else:
imageArray = image[:, :, 2]
return np.squeeze(imageArray)
def QImage2GrayNArray(qimage: QImage):
return color.rgb2gray(QImage2NArray(qimage))
def QPolygon2Mask(width, height, polygon: QPolygonF):
'''
convert QPolygon to mask with the image width and height.
:param width: image width
:param height: image height
:param polygon: QPolygon
:return: binary image with the pixels in polygon marked as True
test pass at 2021.03.08
'''
poa = np.empty([len(polygon), 2])
for i, p in enumerate(polygon):
poa[i, :] = [p.x(), p.y()]
from skimage import draw
mask = draw.polygon2mask((width, height), poa)
return mask
def NArray2QImage(img: np.ndarray):
'''
convert ndarray to QImage
:param img:
:return:
'''
from qimage2ndarray import array2qimage
if (len(img.shape) > 2):
img = img.transpose((1, 0, 2))
else:
img = img.transpose((1, 0))
return array2qimage(img)
def NArray2QPixmap(image: np.array):
return QPixmap.fromImage(NArray2QImage(image))
def QImage2NArray(qimage: QImage):
'''
convert QImage to NArray
:param qimage: QImage
:return: image with the format of ndarray
test pass at 2021.03.08
'''
from qimage2ndarray import rgb_view
img = rgb_view(qimage=qimage)
if (len(img.shape) > 2):
img = img.transpose((1, 0, 2))
else:
img = img.transpose((1, 0))
return img
def QPixmap2NArray(qpixmap: QPixmap):
'''
convert QPixmap to NArray
:param qpixmap:
:return:
'''
return QImage2NArray(qpixmap.toImage())
def GetBinaryImageWithThresholdWithMask(image: np.array, mask: np.array, threshold: int):
masked = np.ma.masked_array(image, mask)
maskedImage = masked.filled(fill_value=0)
bw = maskedImage >= threshold
return bw
# 分析 mask == 1 的区域的 OTSU
def OtsuWithMask2bw(image: np.array, mask: np.array):
from skimage import filters
masked = np.ma.masked_array(image, mask)
otsuThreshold = filters.threshold_otsu(masked.compressed())
bw = GetBinaryImageWithThresholdWithMask(image, mask, otsuThreshold)
return bw
def QPolygonF2list(polygon: QPolygonF):
data = []
for point in polygon:
data.append([point.x(), point.y()])
return data
def list2QPolygonF(data):
polygon = QPolygonF()
for row in data:
polygon.append(QPointF(row[0], row[1]))
return polygon
def imAdjust(image, low_in=0.01, high_in=0.99, gamma=1):
assert (type(image) == np.ndarray)
image = image.astype(np.double)
imageArr = image.flatten()
imageArr = np.sort(imageArr)
lowT = imageArr[int(low_in * len(imageArr))]
highT = imageArr[int((1 - high_in) * len(imageArr))]
imageNew = (image - lowT) / (highT - lowT)
imageNew[imageNew > 1] = 1
imageNew[imageNew < 0] = 0
imageNew = np.power(imageNew, gamma)
imageNew = imageNew * 255
return imageNew.astype(np.uint8)
def medianFilter(image, size=4):
assert (type(image) == np.ndarray)
from skimage.filters.rank import median
from skimage.morphology import disk
return median(image, disk(size))
if __name__ == '__main__':
import matplotlib.pyplot as plt
# width = 500
# height = 400
# polygon = QPolygonF()
# polygon.append(QPointF(50, 0))
# polygon.append(QPointF(150, 280))
# polygon.append(QPointF(350, 20))
# mask = QPolygon2Mask(width, height, polygon)
# plt.imshow(mask)
# plt.show()
# pass
image = QImage("./aligned.jpg")
ig = QImage2NArray(image)
print(ig.shape)
# image_new = NArray2QImage(ig)
plt.imshow(ig)
plt.show()
pass