Jupyter笔记本和OpenCV文档中的图像输出

2024-05-19 07:07:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我在读this page of the OpenCV docs。但是,当我在Jupyter笔记本中运行相同的代码时,图像输出会有所不同

文档中的图像

Image in the docs

以Jupyter

Image output in 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()

虽然输出类似,但并不精确。有人能解释为什么会这样吗


Tags: inimportfftfornppltgaussianfilter
1条回答
网友
1楼 · 发布于 2024-05-19 07:07:15

这与图像的显示方式有关。这些图像实际上是相同的。但是,由于图像是3x3像素,并且在显示器上一个像素很小,因此正在调整图像的大小以供查看。文档中的图像通过双线性插值显示,这是一种平滑像素之间边界的方法。jupyter笔记本使用最近邻插值

使用matplotlib,可以告诉它要用于查看的插值类型。选项显示为here

维基百科在这个主题上也有一个article。有关它的信息也可以在OpenCV docs中找到,因为它是在调整图像大小时使用的

我个人认为最近邻插值更适合描述滤波器,但双线性插值更适合于观看照片

相关问题 更多 >

    热门问题