-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabo2.py
More file actions
136 lines (111 loc) · 3.94 KB
/
Copy pathlabo2.py
File metadata and controls
136 lines (111 loc) · 3.94 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
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
import sys
import cv2
import numpy as np
from skimage.exposure import rescale_intensity
import convolutions
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(str(sys.argv[1]))
ret, frame = cap.read()
def kernel(n):
factor = 1/pow(n, 2)
kernelArray = np.full((n, n), factor)
# print(kernelArray)
return kernelArray
def imageConvolution(frame,kernel):
"""
1/ place the frame in separate matrices
2/ perform the convolution on a frame
3/ resize the frame
:param frame: an image of a video
:param kernel: is varying
:return: return the convolution image
"""
widthReturnArray = int(len(frame[0])-len(kernel)+1)
heightReturnArray = int(len(frame)-len(kernel)+1)
widthReturnArray = len(frame[0])
heightReturnArray = len(frame)
returnArray = np.zeros((heightReturnArray, widthReturnArray))
x = 0
y = 0
kernelWidth = len(kernel[0])
while y <= (len(frame)-len(kernel)):
while x <= (len(frame[0]) - len(kernel[0])):
# imaginary array from x to x+kernelwidth en from y to y+kernelheight
currentSum = 0
kernelX = 0
kernelY = 0
for b in range(0, kernelWidth):
for a in range(0, kernelWidth):
currentSum = currentSum + frame[b+y][a+x]*kernel[b][a]
returnArray[y][x] = currentSum
# print(currentSum)
x = x+1
x = 0
y = y+1
# print(returnArray)
# get the size of kernel and take a matrice from the frame
"""
size for width & height:
a = frameSize%kernelSize
=> framSize - a / kernelSize
"""
#min = np.min(returnArray)
#np.sum(returnArray-min)
# rescale
maxValue = np.max(returnArray)
factor = 255/maxValue
returnArray = np.multiply(returnArray, factor)
# returnArray = rescale_intensity(returnArray, in_range=(0, 255), np.max(returnArray))
return returnArray
def showPlot(frame):
plt.hist(frame.ravel(), 12, [0, 120]); plt.show()
while cap.isOpened():
orgValuePixels = []
ret, frame = cap.read()
frame = cv2.resize(frame, (0, 0), fx=0.3, fy=0.3)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('origineel', frame)
cv2.waitKey(1)
imageFourier = np.fft.fft(frame)
# fourrierHist = np.histogram(imageFourier.ravel(), 256, [0, 256])
showPlot(imageFourier)
# we now have the spectrum analyse
# wich needs to be logaritmic:
logSpectrum = np.log(imageFourier)
showPlot(logSpectrum)
"""
kernl = kernel(9)
# kernl = np.uint8(kernl)
#imageConvolve = convolutions.convolve(imageFourier.real, kernl)
laplacian = np.array((
[1/9, 1/9, 1/9],
[1/9, 1/9, 1/9],
[1/9, 1/9, 1/9]), dtype="int")
sharpen = np.array((
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]), dtype="int")
"""
imageConvolve = convolutions.convolve(frame, np.real(kernel(9)))
showPlot(imageConvolve)
# imageConvolve = np.convolve(frame, np.real(kernel(9)))
# imageConvolve = imageConvolution(frame, laplacian)
averagedSpectrum = np.multiply(imageConvolve, logSpectrum)
showPlot(averagedSpectrum)
spectralResidual = np.subtract(logSpectrum, averagedSpectrum)
showPlot(spectralResidual)
# imageSubtract = np.zeros(len(imageConvolve[0], len(imageConvolve)))
# for y in range(0, len(imageSubtract)):
# for x in range(0, len(imageSubtract[0])):
# imageSubtract[y][x] = imageFourier[y][x]-imageConvolve[y][x]
saliencyImage = np.fft.ifft2(spectralResidual)
# saliencyImage = rescale_intensity(np.real(saliencyImage), in_range=(0, 255))
cv2.imshow('saliency', np.real(saliencyImage))
#
# convolve v d foto & dan imageFourier - die convolve
# daarna een ifft2 ervan doen
#
# # saliencyImage = np.fft.ifft2(frame)
# # saliencyImage = np.real(saliencyImage)
# # saliencyImage = np.scal
# cv2.imshow('saliency', np.real(saliencyImage))