-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.py
More file actions
45 lines (35 loc) · 1.63 KB
/
Block.py
File metadata and controls
45 lines (35 loc) · 1.63 KB
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
import numpy as np
from sklearn.decomposition import PCA
class Block(object):
def __init__(self, imageBlock, x, y, blockSize):
"""
Starts an block object used to store cropped parts of the image.
'imageBlock': cropped part of the image
'x': x coordenate of the block
'y': y coordenate of the block
'blockSize': size of the block
"""
self.image = imageBlock
self.imagePixels = self.image.load()
self.coordinate = (x, y)
self.blockSize = blockSize
def appendBlock(self):
"""
Compute the PCA and append it - and the coordinates - in the data list of the block; returns data list of the block.
"""
blockData = []
blockData.append(self.coordinate)
blockData.append(self.principalComponentAnalysis(precision = 5)) # Is relative to the numbers after the comma of the component in question
return blockData
def principalComponentAnalysis(self, precision):
"""
Compute the average PCA value of the block based in it components; returns the PCA.
'Precision': number of numbers after the comma in the elements made by the PCA transform
"""
pca = PCA(n_components=1)
imageArray = np.array(self.image)
pca.fit_transform(imageArray)
principalComponents = pca.components_
result = [round(element, precision)
for element in list(principalComponents.flatten())] # Approximates the block to a single value based on its pixels via linearized PCA
return result