-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.py
114 lines (92 loc) · 3.67 KB
/
plotting.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
"""
File Summary: Handles plotting and displaying images.
"""
import os
import matplotlib.pyplot as plt
import numpy as np
import file_names
import image_reading
def plotImageHistogram(histogram, bar=False, axis=None):
"""
If bars is True, it's a bar chart, otherwise it's a line chart.
You will need to call plt.show() outside of this function.
"""
if axis is None:
fig, axis = plt.subplots()
xValues = list(range(0, 256))
if bar:
axis.bar(xValues, histogram)
else:
axis.plot(xValues, histogram)
axis.set_title('GrayScale Image Histogram')
axis.set_ylabel('Frequency')
def plotHistogramWithPeakAndPitLines(histogram, peakIndices, pitIndices, axis=None):
"""
Peaks are drawn in green and pits are drawn in red.
"""
if axis is None:
fig, axis = plt.subplots()
plotImageHistogram(axis, histogram)
for i in range(len(peakIndices)):
peakIndex = peakIndices[i]
pitIndex = pitIndices[i]
# If the peak and pit indices are at the same location, we draw one purple line.
if peakIndex == pitIndex:
axis.axvline(x=peakIndex, color='purple', linestyle='--', linewidth=0.5)
# Otherwise, we draw one line for the peak and one for the pit.
else:
axis.axvline(x=peakIndex, color='green', linestyle='--', linewidth=0.5)
axis.axvline(x=pitIndex, color='red', linestyle='--', linewidth=0.5)
plt.show()
def plotPeakIndices(arrLength, peakIndices, title, axis=None):
if axis is None:
fig, axis = plt.subplots()
x = range(arrLength)
y = np.zeros(shape=(arrLength))
y[peakIndices] = 1
axis.plot(x, y)
axis.set_ylabel('Peak Indication')
axis.set_title(title)
def showGrayScaleImage(image, title, axis=None):
if axis is None:
fig, axis = plt.subplots()
axis.imshow(image, cmap='gray')
axis.set_title(title)
axis.set_axis_off()
def showColorImage(image, title, axis=None):
if axis is None:
fig, axis = plt.subplots()
image = image_reading.scaleColorImageBetween0And1(image)
axis.imshow(image)
axis.set_title(title)
axis.set_axis_off()
def printFileHasBeenSavedMessage(filePath):
fileName = os.path.basename(filePath)
string = 'File {} has been saved'.format(fileName)
print(string)
def saveAlgorithmStepsPlot(hist, peakList, filePath):
fig, axes = plt.subplots(5, figsize=(10, 15))
plotImageHistogram(hist, False, axes[0])
titleList = ['All Peaks', 'Vertical Peaks', 'Horizontal Peaks', 'Final Peaks (Possibly One Added)']
axesList = [axes[1], axes[2], axes[3], axes[4]]
for i in range(len(peakList)):
plotPeakIndices(len(hist), peakList[i], titleList[i], axesList[i])
plt.savefig(filePath, dpi=300)
printFileHasBeenSavedMessage(filePath)
def saveOldNewImageComparisonPlot(originalIm, segmentedIm, filePath):
# Plot the original and new images.
fig, axes = plt.subplots(1, 2, figsize=(20, 10))
originalTitle, segmentedTitle = 'Original Image', 'Segmented Image'
if image_reading.imageIsGrayScale(originalIm):
showGrayScaleImage(originalIm, originalTitle, axes[0])
showGrayScaleImage(segmentedIm, segmentedTitle, axes[1])
else:
showColorImage(originalIm, originalTitle, axes[0])
showColorImage(segmentedIm, segmentedTitle, axes[1])
plt.savefig(filePath, dpi=300)
printFileHasBeenSavedMessage(filePath)
if __name__ == '__main__':
files = file_names.getColorImageFilePaths()
image = image_reading.readImage(files[0])
showColorImage(image, 'Image')
plt.show()