我在读this page of the OpenCV docs。但是,当我在Jupyter笔记本中运行相同的代码时,图像输出会有所不同。你知道吗
你知道吗
import cv2
import numpy as np
from matplotlib import pyplot as plt
# simple averaging filter without scaling parameter
mean_filter = np.ones((3,3))
# creating a guassian filter
x = cv2.getGaussianKernel(5,10)
gaussian = x*x.T
# different edge detecting filters
# scharr in x-direction
scharr = np.array([[-3, 0, 3],
[-10,0,10],
[-3, 0, 3]])
# sobel in x direction
sobel_x= np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# sobel in y direction
sobel_y= np.array([[-1,-2,-1],
[0, 0, 0],
[1, 2, 1]])
# laplacian
laplacian=np.array([[0, 1, 0],
[1,-4, 1],
[0, 1, 0]])
filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr]
filter_name = ['mean_filter', 'gaussian','laplacian', 'sobel_x', \
'sobel_y', 'scharr_x']
fft_filters = [np.fft.fft2(x) for x in filters]
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
mag_spectrum = [np.log(np.abs(z)+1) for z in fft_shift]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(mag_spectrum[i],cmap = 'gray')
plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])
plt.show()
虽然输出是相似的,但并不精确。有人能解释为什么会这样吗?你知道吗
它与图像的显示方式有关。图像实际上是一样的。不过,由于图像是3x3像素,而显示器上的一个像素很小,因此正在调整图像的大小以供查看。文档中的图像采用双线性插值显示,这是一种平滑像素之间边界的方法。jupyter笔记本使用最近邻插值。你知道吗
使用matplotlib,可以告诉它要用于查看的插值类型。选项显示为here。你知道吗
维基百科在这个主题上也有一个article。有关它的信息也可以在OpenCV docs中找到,因为它是在调整图像大小时使用的。你知道吗
我个人认为最近邻插值更适合描述滤波器,但双线性插值更适合于观察照片。你知道吗
相关问题 更多 >
编程相关推荐