-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython
103 lines (82 loc) · 4.11 KB
/
Python
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
import cv2 # It is the latest version of OpenCV which is an image and video processing library
import numpy as np # It is the core library for scientific computing
from sklearn.cluster import KMeans # KMeans clustering algorithm
from sklearn.decomposition import PCA # PCA is a technique to emphasise variation and bring out strong patterns in a data set
from collections import Counter # A Counter is a container that keeps track of how many times equivalent values are added
from scipy.misc import imread, imresize, imsave # These are the libraries for importing and working on images
def find_vector_set(diff_image, new_size):
i = 0
j = 0
vector_set = np.zeros((int(new_size[0] * new_size[1] / 25), 25))
while i < vector_set.shape[0]:
while j < new_size[0]:
k = 0
while k < new_size[1]:
block = diff_image[j:j+5, k:k+5]
feature = block.ravel()
vector_set[i, :] = feature
k = k + 5
j = j + 5
i = i + 1
mean_vec = np.mean(vector_set, axis = 0)
vector_set = vector_set - mean_vec
return vector_set, mean_vec
#In this method, we take non-overlapping blocks of size 5 x 5 from the difference image and flatten them into row vectors.
#If the size of our difference image is m x n, then the number of rows in the vector set would be {m x n}/{5 x 5} .
def find_FVS(EVS, diff_image, mean_vec, new):
i = 2 # Building the feature vector space
#Function find_FVS() determines the feature vector space
feature_vector_set = []
while i < new[0] - 2:
j = 2
while j < new[1] - 2:
block = diff_image[i-2:i+3, j-2:j+3]
feature = block.flatten()
feature_vector_set.append(feature)
j = j+1
i = i+1
FVS = np.dot(feature_vector_set, EVS)
FVS = FVS - mean_vec
print "\nfeature vector space size", FVS.shape
return FVS
#The function find_FVS() is similar to find_vector_set(), but extracts overlapping blocks from the difference image.
def clustering(FVS, components, new):
kmeans = KMeans(components, verbose = 0)
kmeans.fit(FVS)
output = kmeans.predict(FVS)
count = Counter(output)
least_index = min(count, key = count.get)
change_map = np.reshape(output,(new[0] - 4, new[1] - 4))
return least_index, change_map
# Clustering of the feature vector space, and change map
#the cluster with the lowest number of pixels, and also the highest mean is the cluster belonging to the changed class.
def find_PCAKmeans(imagepath1, imagepath2):
image1 = imread(imagepath1)
image2 = imread(imagepath2)
# Difference image and the Eigen vector space
# difference image is the pixel-by-pixel subtraction of the 2 images
# difference image, constructed using the multi temporal images
# difference image (i, j) = |image1(i, j) - image2(i, j)|
new_size = np.asarray(image1.shape) / 5
new_size = new_size.astype(int) * 5
# The image can be resized to make both the dimensions a multiple of 5 by scipy.misc.imresize()
image1 = imresize(image1, (new_size)).astype(np.int16)
image2 = imresize(image2, (new_size)).astype(np.int16)
diff_image = abs(image1 - image2)
imsave('diff.jpg', diff_image)
vector_set, mean_vec = find_vector_set(diff_image, new_size)
pca = PCA()
pca.fit(vector_set)
# PCA is applied on vector set to get the Eigen vector space. The Eigen vector space will be a 25 x 25 matrix;
EVS = pca.components_
FVS = find_FVS(EVS, diff_image, mean_vec, new_size) # building the feature vector space
components = 3
least_index, change_map = clustering(FVS, components, new_size)
# PCA takes a data set and determines its co-variance matrix after performing mean normalisation on it
cleanChangeMap = cv2.erode(change_map,kernel) # building a change map (a binary image to show the output of change detection)
imsave("changemap.jpg", change_map)
imsave("cleanchangemap.jpg", cleanChangeMap)
if __name__ == "__main__":
a = 'image1.jpg'
b = 'image2.jpg'
find_PCAKmeans(a,b)