
python实现线性滤波和非线性滤波
数字图像处理 | python线性滤波和非线性滤波
- 数字图像处理 | python实现线性滤波和非线性滤波
- 前言
- 一、线性滤波是什么?
- 二、非线性滤波是什么?
- 三、python代码实现
- 1.python实现
- 运行结果
- 总结
前言
这是数字图像处理课程上的作业,内容是使用非线性滤波和线性滤波处理一张图片。里面内容包含理论和代码,近期太忙,关于理论的讲解以后再补上。
一、线性滤波是什么? 二、非线性滤波是什么? 三、python代码实现 1.python实现
代码如下(示例):
import cv2 as cv
import numpy as np
convolution_kernel = np.array([
[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]
])
def linear_conv(input_image, input_conv):
height = input_image.shape[0]
width = input_image.shape[1]
channels = input_image.shape[2]
conv_height = input_conv.shape[0]
matrix = np.zeros([height + 2, width + 2, channels], np.uint8)
matrix[1:height+1, 1:width+1, :] = input_image
new_image = []
for cow in range(height):
for col in range(width):
for channel in range(channels):
new_image.append(
np.sum(np.multiply(input_conv, matrix[cow:cow + conv_height, col:col + conv_height, channel])))
new_image = np.array(new_image).reshape([height, width, channels])/255.0
cv.imshow("linear_conv_result", new_image)
def nonlinear_conv(input_image, shape):
height = input_image.shape[0]
width = input_image.shape[1]
channels = input_image.shape[2]
matrix = np.zeros([height + 2, width + 2, channels], np.uint8)
matrix[1:height+1, 1:width+1, :] = input_image
new_image = []
for cow in range(height):
for col in range(width):
for channel in range(channels):
new_image.append(np.max(matrix[cow:cow + shape, col:col + shape, channel]))
new_image = np.array(new_image).reshape([height, width, channels])
cv.imshow("nonlinear_conv_result", new_image)
# 取图片
src = cv.imread("1.jfif") # blue,green,red
print(type(src))
# 窗口
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
# 展示
cv.imshow("input image", src)
# 线性
linear_conv(src, convolution_kernel)
# 非线性
nonlinear_conv(src, 3)
# 暂停
cv.waitKey(0)
cv.destroyAllWindows()
运行结果
总结
如果有帮助的话,欢迎关注本人公众号。会更新自然语言处理的内容哦
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)