-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic.py
141 lines (115 loc) · 4.68 KB
/
Basic.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
import numpy
from Application.Models.PlottingData import PlottingData
from Application.Utils.PlotterDecorators import PlotterFunction
# from Application.Utils.InputDecorators import InputDialog
# from Application.Utils.OutputDecorators import OutputDialog
@PlotterFunction(name="Plot row values", fromMainModel=["leftClickPosition"], computeOnClick=True)
def plotRowValues(image, leftClickPosition):
# TODO: take note that the function parameter must be named the same as the
# fromMainModel parameters
"""
TODO: document plotRowValues
:param image:
:param leftClickPosition:
:return:
"""
plotDataItemsList = []
if image is None or leftClickPosition is None:
return {
'plottingDataList': []
}
# Grayscale image
imageShapeLen = len(image.shape)
if imageShapeLen == 2:
plotName = 'Gray level'
plottingData = PlottingData(plotName, image[leftClickPosition.y], pen='r')
plotDataItemsList.append(plottingData)
# Color image
elif imageShapeLen == 3:
plotName = 'Red channel'
plottingData = PlottingData(plotName, image[leftClickPosition.y, :, 0], pen='r')
plotDataItemsList.append(plottingData)
plotName = 'Green channel'
plottingData = PlottingData(plotName, image[leftClickPosition.y, :, 1], pen='g')
plotDataItemsList.append(plottingData)
plotName = 'Blue channel'
plottingData = PlottingData(plotName, image[leftClickPosition.y, :, 2], pen='b')
plotDataItemsList.append(plottingData)
return {
'plottingDataList': plotDataItemsList
}
@PlotterFunction(name="Plot column values", fromMainModel=["leftClickPosition"], computeOnClick=True)
def plotColumnValues(image, leftClickPosition):
# TODO: take note that the function parameter must be named the same as the
# fromMainModel parameters
"""
TODO: document plotColumnValues
:param image:
:param leftClickPosition:
:return:
"""
plotDataItemsList = []
if image is None or leftClickPosition is None:
return {
'plottingDataList': []
}
# Grayscale image
imageShapeLen = len(image.shape)
if imageShapeLen == 2:
plotName = 'Gray level'
plottingData = PlottingData(plotName, image[:, leftClickPosition.x], pen='r')
plotDataItemsList.append(plottingData)
# Color image
elif imageShapeLen == 3:
plotName = 'Red channel'
plottingData = PlottingData(plotName, image[:, leftClickPosition.x, 0], pen='r')
plotDataItemsList.append(plottingData)
plotName = 'Green channel'
plottingData = PlottingData(plotName, image[:, leftClickPosition.x, 1], pen='g')
plotDataItemsList.append(plottingData)
plotName = 'Blue channel'
plottingData = PlottingData(plotName, image[:, leftClickPosition.x, 2], pen='b')
plotDataItemsList.append(plottingData)
return {
'plottingDataList': plotDataItemsList
}
@PlotterFunction(name="Plot histogram", computeOnImageChanged=True)
def plotHistogram(image):
"""
TODO: document plotHistogram
:param image:
:return:
"""
plotDataItemsList = []
if image is None:
return {
'plottingDataList': []
}
imageShapeLen = len(image.shape)
# Grayscale image
if imageShapeLen == 2:
# numpy.histogram returns the histogram first and the buckets second
# the last bin is shared between the last two elements, so we need one more
# range(256) gives us [0, ..., 255], so we need range(257)
# the first element in the range parameter needs to be lower than the first needed element
histogram = numpy.histogram(image, bins=range(257), range=(-1, 255))[0]
plotName = 'Gray histogram'
plottingData = PlottingData(plotName, histogram, pen='r')
plotDataItemsList.append(plottingData)
# Color image
elif imageShapeLen == 3:
histogram = numpy.histogram(image[:, :, 0], bins=range(257), range=(-1, 255))[0]
plotName = 'Red histogram'
plottingData = PlottingData(plotName, histogram, pen='r')
plotDataItemsList.append(plottingData)
histogram = numpy.histogram(image[:, :, 1], bins=range(257), range=(-1, 255))[0]
plotName = 'Green histogram'
plottingData = PlottingData(plotName, histogram, pen='g')
plotDataItemsList.append(plottingData)
histogram = numpy.histogram(image[:, :, 2], bins=range(257), range=(-1, 255))[0]
plotName = 'Blue histogram'
plottingData = PlottingData(plotName, histogram, pen='b')
plotDataItemsList.append(plottingData)
return {
'plottingDataList': plotDataItemsList
}