当前位置: 首页 > news >正文

CNN--不同的filter对图片进行卷积

import os
import numpy as np
from PIL import Image, ImageDraw
import matplotlib.pyplot as plt# ============== 1) 使用你的图片 ==============
CUSTOM_PATH = "apple.png"def load_or_make_image():if CUSTOM_PATH and os.path.exists(CUSTOM_PATH):img = Image.open(CUSTOM_PATH).convert("L")title = f"Original ({os.path.basename(CUSTOM_PATH)})"else:# 备用: 生成一张灰度示例图H, W = 128, 128img = Image.new("L", (W, H), color=220)draw = ImageDraw.Draw(img)draw.rectangle([20, 20, 100, 60], fill=40)draw.ellipse([60, 70, 110, 120], fill=255)draw.line([0, 0, 127, 127], fill=90, width=3)title = "Original (synthetic)"return img, titleimg, orig_title = load_or_make_image()
img_np = np.array(img, dtype=np.float32)# ============== 2) 多个 3×3 卷积核 ==============
kernels = {"Sobel X": np.array([[1, 0, -1],[1, 0, -1],[1, 0, -1]], dtype=np.float32),"Sobel Y": np.array([[ 1,  1,  1],[ 0,  0,  0],[-1, -1, -1]], dtype=np.float32),"Laplacian": np.array([[0, -1,  0],[-1, 4, -1],[0, -1,  0]], dtype=np.float32),"Sharpen": np.array([[0, -1,  0],[-1, 5, -1],[0, -1,  0]], dtype=np.float32),"Box Blur": (1/9.0) * np.ones((3,3), dtype=np.float32),"Emboss": np.array([[-2, -1, 0],[-1,  1, 1],[ 0,  1, 2]], dtype=np.float32),"Outline": np.array([[-1, -1, -1],[-1,  8, -1],[-1, -1, -1]], dtype=np.float32),
}# ============== 3) 卷积函数 ==============
def conv2d_same(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:kh, kw = kernel.shapepad_h, pad_w = kh//2, kw//2padded = np.pad(image, ((pad_h, pad_h), (pad_w, pad_w)), mode='constant')out = np.zeros_like(image, dtype=np.float32)for i in range(out.shape[0]):for j in range(out.shape[1]):region = padded[i:i+kh, j:j+kw]out[i, j] = float(np.sum(region * kernel))return outdef to_uint8_for_display(arr: np.ndarray) -> np.ndarray:a_min, a_max = float(arr.min()), float(arr.max())if a_max - a_min < 1e-6:return np.zeros_like(arr, dtype=np.uint8)norm = (arr - a_min) / (a_max - a_min) * 255.0return norm.astype(np.uint8)# ============== 4) 卷积 + 展示 ==============
results = {"Original": img_np}
for name, k in kernels.items():conv = conv2d_same(img_np, k)results[name] = to_uint8_for_display(conv)# 展示:所有图像在一行或两行
n = len(results)
cols = 4  # 每行展示 4 张图,可以改成 3 或 5
rows = int(np.ceil(n / cols))plt.figure(figsize=(4*cols, 4*rows))
for idx, (name, arr) in enumerate(results.items(), 1):plt.subplot(rows, cols, idx)plt.imshow(arr, cmap="gray")plt.title(name)plt.axis("off")plt.tight_layout()
plt.show()

苹果原图
image
不同的filter卷积之后展示的图片效果

image

http://www.jsqmd.com/news/19199/

相关文章:

  • 2025年10月北京口腔医院口碑榜:十家院区横向对比排行
  • 拓展博客内容学习
  • 2025年10月AI搜索优化推荐:口碑榜单与全流程避坑指南
  • SqlServer AlwaysOn高可用群集搭建 - 教程
  • 2025年口碑最好的电线电缆品牌推荐:鑫佰亿线缆领跑行业
  • 2025 年最新推荐树脂瓦厂家排行榜:聚焦品质企业,助力高效选购优质合成 / 屋面 / ASA 树脂瓦
  • 今日开始学习自动化发布博客内容
  • 基于C语言实现Modbus转IEC 60870-5-103协议转换器
  • 2025年冷风机厂家权威推荐榜:水冷环保空调/节能工业冷风机/车间降温设备源头厂家综合评测与选购指南
  • 10 22
  • 2025 年防火母线槽厂家最新推荐榜:聚焦企业专利技术、品质管控及知名客户合作案例的权威解析
  • 2025年CNC加工厂家权威推荐榜单:CNC精密加工/加工中心CNC/cnc电脑锣加工/精密CNC加工/铝板cnc加工技术实力与市场口碑深度解析
  • 2025年国内4大主流CRM AI能力选型对比分析 - SaaS软件
  • 2025年地铺石材品牌口碑排行榜TOP10:选购指南与行业趋势解析
  • 2025年市面上地铺石材品牌排名前十揭秘:行业趋势与选择指南
  • 2025年陶瓷过滤板厂家权威推荐榜:专业生产微孔陶瓷过滤板、耐高温陶瓷过滤板、工业用陶瓷过滤板源头厂家实力解析
  • 六边形、洋葱、策略、适配器架构设计
  • 2025年市面上太空舱品牌排行榜前十强推荐
  • 2025年市面上太空舱品牌排名前十推荐
  • 实用指南:深入浅出 Redis:从核心原理到运维实战指南二
  • 详细介绍:《UE5_C++多人TPS完整教程》学习笔记60 ——《P61 开火蒙太奇(Fire Montage)》
  • 为什么后悔在创业中用RUST这个妖魔化宣传的语言
  • 2025年西安买房推荐Top 10:学区新房性价比终极指南
  • 全国医院排名前十
  • 2025 年最新推荐!五莲花 / 五莲红 / 五莲灰 / 芝麻灰等路沿石优质厂家榜单:深度聚焦实力企业资源、加工与服务核心优势
  • 2025 年丁基胶厂家最新推荐排行榜:涵盖耐高温 / 光伏用 / 车用等多领域产品,助力企业精准挑选优质合作伙伴
  • 1114. 按序打印
  • linux 中sed命令 d与g选项的区别
  • C# Avalonia 16- Animation- SampleViewer - ReusableFollowExample
  • 2025年五金零件加工厂家权威推荐榜:精密零件/小五金加工/五金精密零件加工专业实力与品质保障精选指南