栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

数字图像处理 | python实现线性滤波和非线性滤波

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数字图像处理 | python实现线性滤波和非线性滤波

数字图像处理 | python实现线性滤波和非线性滤波

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()
运行结果


总结


如果有帮助的话,欢迎关注本人公众号。会更新自然语言处理的内容哦


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/665721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号