-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicsOfImageProcessing.py
172 lines (145 loc) · 4.18 KB
/
BasicsOfImageProcessing.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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 30 09:25:47 2022
@author: S. M. Hossein Mousavi
"""
import numpy as np
import cv2 as cv
import glob
from skimage.feature import hog
import warnings
import sklearn.model_selection as ms
import sklearn.neighbors as ne
import sklearn.naive_bayes as nb
import sklearn.tree as tr
import sklearn.linear_model as lm
from sklearn.metrics import confusion_matrix
from sklearn.metrics import ConfusionMatrixDisplay
# Suppressing All Warnings
warnings.filterwarnings("ignore")
# Three DIM loading image
ColorImg = []
for img2 in glob.glob("data/*.jpg"):
n= cv.imread(img2)
ColorImg.append(n)
# Two DIM Loading Images
img=cv.imread('tst.jpg')
imggray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
images = [cv.imread(file) for file in glob.glob("data/*.jpg")]
# Dataset Size
DataSize=len(images)
# Converting to Gray
Gray=images
for i in range(DataSize):
Gray[i]=cv.cvtColor(images[i],cv.COLOR_BGR2GRAY)
print('Convert to Gray Image',i)
# Resize Images
Resized=Gray
width = 512
height = 512
dim = (width, height)
for i in range(DataSize):
Resized[i] = cv.resize(Gray[i], dim, interpolation = cv.INTER_AREA)
print('Resize Image',i)
# Image Sizes
SampSize=Resized[1].shape
# Hist EQ
HistEQ=Resized
for i in range(DataSize):
HistEQ[i]=cv.equalizeHist(Resized[i])
print('HistEQ Image',i)
# Edge Detection
CannyEdge=HistEQ
for i in range(DataSize):
CannyEdge[i]=cv.Canny(HistEQ[i],100,200)
print('Canny Edges for Image',i)
# Extracting SIFT Features
sift = cv.SIFT_create()
SiftF=HistEQ
des=HistEQ
kp=HistEQ
DesSift=HistEQ
for i in range(DataSize):
kp[i], des[i] = sift.detectAndCompute(HistEQ[i],None)
DesSift[i]=sum(des[i])
print('SIFT Features for Image',i)
#pts = cv.KeyPoint_convert(kp)
#pts2 = [p.pt for p in kp]
# Extracting HOG Features
HogF=ColorImg
for i in range(DataSize):
HogF[i], hog_image = hog(ColorImg[i], orientations=8, pixels_per_cell=(64, 64),
cells_per_block=(1, 1), visualize=True, channel_axis=-1)
print('HOF Features for Image',i)
# List to matrix conversion
F1 = np.array(DesSift)
F2 = np.array(HogF)
F2 = np.float32(F2)
# Feature Fusion
FinalF = np.hstack((F1, F2))
# Labeling for Classification
ClassLabel = np.arange(DataSize)
ClassLabel[:20]=0
ClassLabel[20:40]=1
ClassLabel[40:60]=2
# Data Assign to X and Y
X=FinalF
Y=ClassLabel
# Data Split
Xtr, Xte, Ytr, Yte = ms.train_test_split(X, Y, train_size = 0.8)
# KNN Classifier
trAcc=[]
teAcc=[]
Ks=[]
for i in range(1,5):
KNN = ne.KNeighborsClassifier(n_neighbors = i)
KNN.fit(Xtr, Ytr)
trAcc.append(KNN.score(Xtr, Ytr))
teAcc.append(KNN.score(Xte, Yte))
Ks.append(i)
# Logistic Regression Classifier
LR = lm.LogisticRegression(max_iter = 100)
LR.fit(Xtr, Ytr)
PredTrainLR= LR.predict(Xtr) # for train confusion matrix
PredTestLR= LR.predict(Xte) # for test confusion matrix
LRtrAcc = LR.score(Xtr, Ytr)
LRteAcc = LR.score(Xte, Yte)
# Naive Bayes Classifier
NB = nb.GaussianNB()
NB.fit(Xtr, Ytr)
NBtrAcc = NB.score(Xtr, Ytr)
NBteAcc = NB.score(Xte, Yte)
# Decision Tree Classifier
DTtrAcc = []
DTteAcc = []
MD = []
for i in range(2, 12):
DT = tr.DecisionTreeClassifier(max_depth = i)
DT.fit(Xtr, Ytr)
DTtrAcc.append(DT.score(Xtr, Ytr))
DTteAcc.append(DT.score(Xte, Yte))
MD.append(i)
# Train and Test Results
print ('KNN Train Accuracy is :')
print (trAcc[-1])
print ('KNN Test Accuracy is :')
print (teAcc[-1])
print('Logestic Regression Train Accuracy is : ')
print (LRtrAcc)
print('Logestic Regression Test Accuracy is :')
print (LRteAcc)
print('Naive Bayes Train Accuracy is :')
print (NBtrAcc)
print('Naive Bayes Test Accuracy is :')
print (NBteAcc)
print('Decision Tree Train Accuracy is :')
print (DTtrAcc[-1])
print('Decision Tree Test Accuracy is :')
print (DTteAcc[-1])
# Plot Confusion Matrix for Logistic Regression
# Train LR
cm = confusion_matrix(Ytr, PredTrainLR)
cm_display = ConfusionMatrixDisplay(cm).plot()
# Test LR
cm2 = confusion_matrix(Yte, PredTestLR)
cm_display2 = ConfusionMatrixDisplay(cm2).plot()