Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Extraction using DCT and LDA added #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Feature Extraction/DCT/dct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import math
import cv2
import matplotlib.pyplot as plt
import numpy as np


# Constructing DCT using Quantizatoin matrix
############# Failure attempt !! ################

quantization_matrix = [
[16, 11, 10, 16, 24, 40, 51, 61],
[12, 12, 14, 19, 26, 58, 60, 55],
[14, 13, 16, 24, 40, 57, 69, 56],
[14, 17, 22, 29, 51, 87, 80, 62],
[18, 22, 37, 56, 68, 109, 103, 77],
[24, 35, 55, 64, 81, 104, 113, 92],
[49, 64, 78, 87, 103, 121, 120, 101],
[72, 92, 95, 98, 112, 100, 103, 99]
]

cosine_table = [
[math.cos((2*i+1)*j * math.pi/16) for j in range(8)] for i in range(8)
]

partition_list = [(i,j) for i in range(8) for j in range(8)]


def DCT (a,u,v):
value = 0
for i,j in partition_list:
value += a[i][j] * cosine_table[i][u] * cosine_table[j][v]
if u == 0: value *= (1/math.sqrt(2))
if v == 0: value *= (1/math.sqrt(2))
value *= 0.25
return value




def reduce_128 (pixel):
pixel -= 128
return pixel


def Encode (img):
img = [[reduce_128(pixel) for pixel in row] for row in img]
img = [[DCT(img,u,v) for v in range(8)] for u in range(8) ]
img = [[round(a/q) for a,q in zip(a,q)] for a,q in zip (img,quantization_matrix)]
return img

############################################################################################################
############################################################################################################
############################################################################################################
############################### Constructing DCT using O(n^4) loops ########################################
############################################################################################################
############################################################################################################
############################################################################################################

############# Second Failure attempt !! ################

def DCT_cosine (img_index, dct_index, img_row_or_col_size):
return np.cos(math.radians((2*img_index+1)*dct_index*np.pi/2*img_row_or_col_size))


def constant_value (dct_index, img_row_or_col_size):
if dct_index == 0:
return np.sqrt(1/img_row_or_col_size)
else:
return np.sqrt(2/img_row_or_col_size)

def DCT_for_one_pixel (img, dct_row_index, dct_col_index, img_row_size, img_col_size):
value = 0;
for img_row_index in range(img_row_size):
for img_col_index in range(img_col_size):
value += img[img_row_index,img_col_index] * DCT_cosine(img_row_index,dct_row_index,img_row_size)* DCT_cosine(img_col_index,dct_col_index,img_col_size)
return value


def DCT_Matrix (D, img):
for u in range(D.shape[0]):
for v in range(D.shape[1]):
D[u,v] = constant_value(u, D.shape[0]) * constant_value(v, D.shape[1]) * DCT_for_one_pixel(img,u,v,img.shape[0],img.shape[1])
D[u,v] = DCT_for_one_pixel(img,u,v,img.shape[0],img.shape[1])

return D


############################################################################################################
############################################################################################################
############################################################################################################
############################### Constructing DCT using basic matrix ########################################
############################################################################################################
############################################################################################################
############################################################################################################


def basic_matrix(N):
C = np.zeros((N,N))
C[0,] = np.sqrt(1/N)

for u in range(1,N):
for v in range(N):
C[u,v] = np.sqrt(2/N) *math.cos(((2*v+1)*np.pi*u)/(2*N))
return C

image = cv2.imread('/home/abdelgawad/Folders/Graduation-Project/Feature Extraction/DCT/panda.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = np.float32(image)/255.0


C = basic_matrix(image.shape[0])
temp = C.dot(image)
D = temp.dot(np.transpose(C))
print(D)

print("-------------------------------------------------------")
#test against openCv DCT function
dct = cv2.dct(image)
print(dct)


print(np.allclose(D,dct,atol=1e-05)) # hand made DCT gets the same output as the ready made one with tolreance of 10^-5

Binary file added Feature Extraction/DCT/panda.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
44 changes: 44 additions & 0 deletions Feature Extraction/LDA/lda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

from __future__ import print_function, division
import numpy as np
from utils import calculate_covariance_matrix, normalize, standardize

class LDA():
"""The Linear Discriminant Analysis classifier, also known as Fisher's linear discriminant.
Can besides from classification also be used to reduce the dimensionaly of the dataset.
"""
def __init__(self):
self.w = None

def transform(self, X, y):
self.fit(X, y)
# Project data onto vector
X_transform = X.dot(self.w)
return X_transform

def fit(self, X, y):
# Separate data by class
X1 = X[y == 0]
X2 = X[y == 1]

# Calculate the covariance matrices of the two datasets
cov1 = calculate_covariance_matrix(X1)
cov2 = calculate_covariance_matrix(X2)
cov_tot = cov1 + cov2

# Calculate the mean of the two datasets
mean1 = X1.mean(0)
mean2 = X2.mean(0)
mean_diff = np.atleast_1d(mean1 - mean2)

# Determine the vector which when X is projected onto it best separates the
# data by class. w = (mean1 - mean2) / (cov1 + cov2)
self.w = np.linalg.pinv(cov_tot).dot(mean_diff)

def predict(self, X):
y_pred = []
for sample in X:
h = sample.dot(self.w)
y = 1 * (h < 0)
y_pred.append(y)
return y_pred
2 changes: 2 additions & 0 deletions Feature Extraction/LDA/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .data_manipulation import *
from .data_operation import *
20 changes: 20 additions & 0 deletions Feature Extraction/LDA/utils/data_manipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import math
import sys

def normalize(X, axis=-1, order=2):
""" Normalize the dataset X """
l2 = np.atleast_1d(np.linalg.norm(X, order, axis))
l2[l2 == 0] = 1
return X / np.expand_dims(l2, axis)


def standardize(X):
""" Standardize the dataset X """
X_std = X
mean = X.mean(axis=0)
std = X.std(axis=0)
for col in range(np.shape(X)[1]):
if std[col]:
X_std[:, col] = (X_std[:, col] - mean[col]) / std[col]
return X_std
12 changes: 12 additions & 0 deletions Feature Extraction/LDA/utils/data_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np
import math
import sys

def calculate_covariance_matrix(X, Y=None):
""" Calculate the covariance matrix for the dataset X """
if Y is None:
Y = X
n_samples = np.shape(X)[0]
covariance_matrix = (1 / (n_samples-1)) * (X - X.mean(axis=0)).T.dot(Y - Y.mean(axis=0))

return np.array(covariance_matrix, dtype=float)