-
Notifications
You must be signed in to change notification settings - Fork 0
/
alternate_approaches.py
141 lines (118 loc) · 4.59 KB
/
alternate_approaches.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
"""
File Summary: Some alternate approaches that I implemented but ultimately decided against using.
This is all part of the development process. You can skip over this file if you like.
"""
import numpy as np
import pandas as pd
import cv2
import os
def readGrayScaleImage(fileName):
IMAGE_DIRECTORY = r'C:\Program Files\MATLAB\R2021a\toolbox\images\imdata'
path = os.path.join(IMAGE_DIRECTORY, fileName)
return cv2.imread(path, cv2.IMREAD_GRAYSCALE)
def getShiftedHistogramDf(histogram):
"""
Function prepares a dataframe for further use with finding peak and pit points.
"""
df = pd.DataFrame(data=histogram, index=range(0, 256), columns=['Histogram'])
df.index.name = 'Intensity'
df['Previous'] = df['Histogram'].shift(1)
df['Next'] = df['Histogram'].shift(-1)
df = df[['Previous', 'Histogram', 'Next']]
df.dropna(inplace=True)
return df
def findPeakPointIndices(df):
"""
A peak occurs when there is an increase and then a decrease.
"""
peakFilter = (df['Histogram'] > df['Previous']) & (df['Next'] < df['Histogram'])
return df[peakFilter].index.values
def findPitPointIndices(df):
"""
A pit occurs when there is a decrease and then an increase.
"""
pitFilter = (df['Histogram'] < df['Previous']) & (df['Next'] > df['Histogram'])
return df[pitFilter].index.values
def findPeakAndPitPointIndices(histogram):
df = getShiftedHistogramDf(histogram)
peakPoints = findPeakPointIndices(df)
pitPoints = findPitPointIndices(df)
return peakPoints, pitPoints
def alternateGetImageHistogram(image):
return np.histogram(image, range(0, 256))
def getPeakIndicesAboveAverageChange(peakIndices, pitIndices):
diffArray = np.abs(pitIndices - peakIndices)
meanDiff = diffArray.mean()
return peakIndices[diffArray > meanDiff]
def peaksAndPitsComeInCorrectOrder(peakIndices, pitIndices):
"""
The order of peak - pit - peak must be maintained. Basically, this means that subtracting one from the other
should yield either all positive or all negative values.
"""
boolArray = (peakIndices - pitIndices) > 0
return all(boolArray) or all(~boolArray)
def alternateFindPeakAndPitIndicesWithLimits(array):
"""
This version of the function enforces the peak - pit order.
"""
peakIndices = []
pitIndices = []
peakActive = True
pitActive = True
for i in range(1, len(array)):
if pitActive and array[i-1] < array[i] > array[i+1]:
peakIndices.append(i)
peakActive = True
pitActive = False
if peakActive and array[i-1] > array[i] < array[i+1]:
pitIndices.append(i)
pitActive = True
peakActive = False
return np.array(peakIndices), np.array(pitIndices)
def showImage(image):
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def modifyColorImageCentroids(centroidList):
"""
There are three channels for color images. That means we must adjust cluster centroids so each channel has
the same number.
"""
# Find the minimum length.
lengths = []
for centroid in centroidList:
lengths.append(len(centroid))
minLength = np.min(lengths)
# Create the new centroids.
newCentroidList = []
for centroid in centroidList:
newCentroidList.append(centroid[0:minLength])
return newCentroidList
def findPeakAndPitIndices(array):
"""
Passed over in favor of the enforced order function.
"""
peakIndices = []
pitIndices = []
for i in range(1, len(array) - 1):
# A peak is defined as an increase and then a decrease.
if array[i-1] < array[i] > array[i+1]:
peakIndices.append(i)
# A pit is defined as a decrease and then an increase.
if array[i-1] > array[i] < array[i+1]:
pitIndices.append(i)
return np.array(peakIndices), np.array(pitIndices)
def getShiftedArrayDifference(array):
"""
Passed over in favor of the bidirectional approach.
"""
shiftedDiffList = []
for i in range(len(array) - 1):
diff = abs(array[i] - array[i + 1])
shiftedDiffList.append(diff)
return shiftedDiffList
def filterByHorizontalDistance(verticalPeakIndices):
diffArray = getShiftedArrayDifference(verticalPeakIndices)
diffArray.append(diffArray[-1]) # Last value is appended because it is skipped when shifting.
avgHorizontalDiff = np.mean(diffArray)
return verticalPeakIndices[diffArray > avgHorizontalDiff]