Skip to content

Commit c0cf9fa

Browse files
committed
update ch23
1 parent c5cb8d2 commit c0cf9fa

8 files changed

+230
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/13 上午11:12
3+
# @Author : play4fun
4+
# @File : 23.1.3 DFT 的性能优化.py
5+
# @Software: PyCharm
6+
7+
"""
8+
23.1.3 DFT 的性能优化.py:
9+
"""
10+
import numpy as np
11+
12+
# In [16]: img = cv2.imread('messi5.jpg',0)
13+
# In [17]: rows,cols = img.shape
14+
# In [18]: print rows,cols
15+
# 342 548
16+
# In [19]: nrows = cv2.getOptimalDFTSize(rows)
17+
# In [20]: ncols = cv2.getOptimalDFTSize(cols)
18+
# In [21]: print nrows, ncols
19+
# 360 57
20+
21+
nimg = np.zeros((nrows,ncols))
22+
nimg[:rows,:cols] = img
23+
24+
#或者
25+
right = ncols - cols
26+
bottom = nrows - rows
27+
#just to avoid line breakup in PDF file
28+
bordertype = cv2.BORDER_CONSTANT
29+
nimg = cv2.copyMakeBorder(img,0,bottom,0,right,bordertype, value = 0)
30+
31+
#现在我们看看 Numpy 的 现
32+
33+
In [22]: %timeit fft1 = np.fft.fft2(img)
34+
10 loops, best of 3: 40.9 ms per loop
35+
In [23]: %timeit fft2 = np.fft.fft2(img,[nrows,ncols])
36+
100 loops, best of 3: 10.4 ms per loop
37+
38+
# 度提 了 4 倍。我们再看看 OpenCV 的 现
39+
In [24]: %timeit dft1= cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
40+
100 loops, best of 3: 13.5 ms per loop
41+
42+
In [27]: %timeit dft2= cv2.dft(np.float32(nimg),flags=cv2.DFT_COMPLEX_OUTPUT)
43+
100 loops, best of 3: 3.11 ms per loop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/13 上午11:18
3+
# @Author : play4fun
4+
# @File : 23.1.4 为什么拉普拉斯算子是高通滤波器.py
5+
# @Software: PyCharm
6+
7+
"""
8+
23.1.4 为什么拉普拉斯算子是高通滤波器.py:
9+
从图像中我们就可以看出每一个算子允 些信号。从 些信息中我 们就可以知 些是 HPF 是 LPF
10+
"""
11+
12+
import cv2
13+
import numpy as np
14+
from matplotlib import pyplot as plt
15+
16+
# simple averaging filter without scaling parameter
17+
mean_filter = np.ones((3, 3))
18+
# creating a guassian filter
19+
x = cv2.getGaussianKernel(5, 10)
20+
21+
gaussian = x * x.T
22+
# different edge detecting filters
23+
# scharr in x-direction
24+
scharr = np.array([[-3, 0, 3],
25+
[-10, 0, 10],
26+
[-3, 0, 3]])
27+
# sobel in x direction
28+
sobel_x = np.array([[-1, 0, 1],
29+
[-2, 0, 2],
30+
[-1, 0, 1]])
31+
# sobel in y direction
32+
sobel_y = np.array([[-1, -2, -1],
33+
[0, 0, 0],
34+
[1, 2, 1]])
35+
# laplacian
36+
laplacian = np.array([[0, 1, 0],
37+
[1, -4, 1],
38+
[0, 1, 0]])
39+
filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr]
40+
filter_name = ['mean_filter', 'gaussian', 'laplacian', 'sobel_x', 'sobel_y', 'scharr_x']
41+
42+
fft_filters = [np.fft.fft2(x) for x in filters]
43+
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
44+
mag_spectrum = [np.log(np.abs(z) + 1) for z in fft_shift]
45+
46+
for i in range(6):
47+
plt.subplot(2, 3, i + 1), plt.imshow(mag_spectrum[i], cmap='gray')
48+
plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])
49+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/13 上午10:59
3+
# @Author : play4fun
4+
# @File : OpenCV中的傅里叶变换-DFT.py
5+
# @Software: PyCharm
6+
7+
"""
8+
OpenCV中的傅里叶变换-DFT.py:
9+
OpenCV 中相应的函数是 cv2.dft() 和 cv2.idft()。和前 出的结果 一样 但是是双通道的。
10+
第一个通道是结果的实数部 分
11+
第二个通道是结果的虚数部分。
12+
输入图像 先 换成 np.float32 格式
13+
14+
使用函数 cv2.cartToPolar() 它会同时返回幅度和相位。
15+
16+
"""
17+
18+
import numpy as np
19+
import cv2
20+
from matplotlib import pyplot as plt
21+
22+
img = cv2.imread('../data/messi5.jpg', 0)
23+
24+
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
25+
26+
dft_shift = np.fft.fftshift(dft)
27+
magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:, :, 0], dft_shift[:, :, 1]))
28+
29+
plt.subplot(121), plt.imshow(img, cmap='gray')
30+
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
31+
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
32+
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
33+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/13 上午11:03
3+
# @Author : play4fun
4+
# @File : OpenCV中的傅里叶变换-逆DFT.py
5+
# @Software: PyCharm
6+
7+
"""
8+
OpenCV中的傅里叶变换-逆DFT.py:
9+
在前 的 分我们实现了一个 HPF 高通滤波 现在我们来做 LPF 低通滤波 将高频分去除。其实就是对图像进行模糊操作。
10+
首先我们 构建一个掩模 与低 区域对应的地方 置为 1, 与 区域 对应的地方 置为 0。
11+
"""
12+
13+
import numpy as np
14+
import cv2
15+
from matplotlib import pyplot as plt
16+
17+
img = cv2.imread('../data/messi5.jpg', 0)
18+
19+
dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT)
20+
21+
dft_shift = np.fft.fftshift(dft)
22+
23+
rows, cols = img.shape
24+
crow, ccol = int(rows / 2), int(cols / 2)
25+
26+
# create a mask first, center square is 1, remaining all zeros
27+
mask = np.zeros((rows, cols, 2), np.uint8)
28+
mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1
29+
30+
# apply mask and inverse DFT
31+
fshift = dft_shift * mask
32+
f_ishift = np.fft.ifftshift(fshift)
33+
img_back = cv2.idft(f_ishift)
34+
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
35+
36+
plt.subplot(121), plt.imshow(img, cmap='gray')
37+
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
38+
plt.subplot(122), plt.imshow(img_back, cmap='gray')
39+
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
40+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:42
5+
6+
函数 np.fft.fft2() 可以对信号 率 换 出结果是一个复杂的数组。
7+
第一个参数是 入图像 求是灰 度格式。
8+
第二个参数是可 的, 决定 出数组的大小。
9+
输出数组的大小和输入图像大小一样。如果输出结果比输入图像大
10+
输入图像就需要在进行 FFT 前补0。如果输出结果比输入图像小的话 输入图像就会被切割。
11+
"""
12+
13+
import cv2
14+
import numpy as np
15+
from matplotlib import pyplot as plt
16+
17+
img = cv2.imread('../data/messi5.jpg', 0)
18+
f = np.fft.fft2(img)
19+
fshift = np.fft.fftshift(f)
20+
# 这里构建振幅图的公式没学过
21+
magnitude_spectrum = 20 * np.log(np.abs(fshift))
22+
23+
plt.subplot(121), plt.imshow(img, cmap='gray')
24+
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
25+
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
26+
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
27+
plt.show()
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*-coding:utf8-*-#
2+
__author__ = 'play4fun'
3+
"""
4+
create time:15-10-24 下午5:42
5+
6+
现在我们可以 域变换了 我们就可以在 域对图像 一些操 作了 例如 滤波和 建图像 DFT 的 变换 。比如我们可以使用一个 60x60 的矩形窗口对图像 掩模操作从而去 低 分 。然后再使用函数 np.fft.ifftshift() 平移操作 所以现在直流分 又回到左上 了 左 后使用函数 np.ifft2() FFT 变换。同样又得到一堆复杂的数字 我们 可以对他们取绝对值
7+
8+
"""
9+
10+
import cv2
11+
import numpy as np
12+
from matplotlib import pyplot as plt
13+
14+
img = cv2.imread('../data/messi5.jpg', 0)
15+
f = np.fft.fft2(img)
16+
fshift = np.fft.fftshift(f)
17+
18+
rows, cols = img.shape
19+
crow, ccol = int(rows / 2), int(cols / 2)
20+
fshift[crow - 30:crow + 30, ccol - 30:ccol + 30] = 0
21+
f_ishift = np.fft.ifftshift(fshift)
22+
img_back = np.fft.ifft2(f_ishift)
23+
# 取绝对值
24+
img_back = np.abs(img_back)
25+
26+
plt.subplot(131), plt.imshow(img, cmap='gray')
27+
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
28+
plt.subplot(132), plt.imshow(img_back, cmap='gray')
29+
plt.title('Image after HPF'), plt.xticks([]), plt.yticks([])
30+
plt.subplot(133), plt.imshow(img_back)
31+
plt.title('Result in JET'), plt.xticks([]), plt.yticks([])
32+
plt.show()
33+
34+
'''
35+
如果你 察仔细的 尤其是最后一章 JET 色的图像 你会看到一些不 自然的东 如我用红色箭头标出的区域 。看上图 有些条带 的结构 成为振铃效应。
36+
这是由于我们使用矩形窗口做掩模 成的。 个掩模 换 成正弦形状时就会出现 个 。所以一般我们不 用矩形窗口滤波。最好的 择是高斯窗口。
37+
38+
'''

ch23-图像变换/fftshift-abs.py

-30
This file was deleted.

ch23-图像变换/fftshift.py

-21
This file was deleted.

0 commit comments

Comments
 (0)