-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhybrid-images.py
64 lines (43 loc) · 2.16 KB
/
hybrid-images.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
import numpy
from numpy.fft import fft2, ifft2, fftshift, ifftshift
from scipy import misc
from scipy import ndimage
import math
def scaleSpectrum(A):
return numpy.real(numpy.log10(numpy.absolute(A) + numpy.ones(A.shape)))
# sample values from a spherical gaussian function from the center of the image
def makeGaussianFilter(numRows, numCols, sigma, highPass=True):
centerI = int(numRows/2) + 1 if numRows % 2 == 1 else int(numRows/2)
centerJ = int(numCols/2) + 1 if numCols % 2 == 1 else int(numCols/2)
def gaussian(i,j):
coefficient = math.exp(-1.0 * ((i - centerI)**2 + (j - centerJ)**2) / (2 * sigma**2))
return 1 - coefficient if highPass else coefficient
return numpy.array([[gaussian(i,j) for j in range(numCols)] for i in range(numRows)])
def filterDFT(imageMatrix, filterMatrix):
shiftedDFT = fftshift(fft2(imageMatrix))
misc.imsave("dft.png", scaleSpectrum(shiftedDFT))
filteredDFT = shiftedDFT * filterMatrix
misc.imsave("filtered-dft.png", scaleSpectrum(filteredDFT))
return ifft2(ifftshift(filteredDFT))
def lowPass(imageMatrix, sigma):
n,m = imageMatrix.shape
return filterDFT(imageMatrix, makeGaussianFilter(n, m, sigma, highPass=False))
def highPass(imageMatrix, sigma):
n,m = imageMatrix.shape
return filterDFT(imageMatrix, makeGaussianFilter(n, m, sigma, highPass=True))
def hybridImage(highFreqImg, lowFreqImg, sigmaHigh, sigmaLow):
highPassed = highPass(highFreqImg, sigmaHigh)
lowPassed = lowPass(lowFreqImg, sigmaLow)
return highPassed + lowPassed
def playWithFiltering():
marilyn = ndimage.imread("marilyn.png", flatten=True)
highPassedMarilyn = highPass(marilyn, 20)
lowPassedMarilyn = lowPass(marilyn, 20)
misc.imsave("low-passed-marilyn.png", numpy.real(lowPassedMarilyn))
misc.imsave("high-passed-marilyn.png", numpy.real(highPassedMarilyn))
misc.imsave("sum-of-marilyns.png", numpy.real((highPassedMarilyn + lowPassedMarilyn)/2.0))
if __name__ == "__main__":
einstein = ndimage.imread("einstein.png", flatten=True)
marilyn = ndimage.imread("marilyn.png", flatten=True)
hybrid = hybridImage(einstein, marilyn, 25, 10)
misc.imsave("marilyn-einstein.png", numpy.real(hybrid))