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

以前做视频的网站吗百度网盟官网

以前做视频的网站吗,百度网盟官网,武汉网上商城网站建设,网站功能模块有哪些文章目录 0. 实验环境1. 理论基础1.1 滤波器(卷积核)1.2 PyTorch:卷积操作 2. 图像处理2.1 图像读取2.2 查看通道2.3 图像处理 3. 图像去模糊4. 图像去噪4.1 添加随机噪点4.2 图像去噪 0. 实验环境 本实验使用了PyTorch深度学习框架,相关操作…

文章目录

  • 0. 实验环境
  • 1. 理论基础
    • 1.1 滤波器(卷积核)
    • 1.2 PyTorch:卷积操作
  • 2. 图像处理
    • 2.1 图像读取
    • 2.2 查看通道
    • 2.3 图像处理
  • 3. 图像去模糊
  • 4. 图像去噪
    • 4.1 添加随机噪点
    • 4.2 图像去噪

在这里插入图片描述

0. 实验环境

  本实验使用了PyTorch深度学习框架,相关操作如下:

conda create -n DL python==3.11
conda activate DL
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
conda install matplotlib
conda install pillow numpy
软件包本实验版本
matplotlib3.8.0
numpy1.26.3
pillow10.0.1
python3.11.0
torch2.1.2
torchaudio2.1.2
torchvision0.16.2

1. 理论基础

  二维卷积运算是信号处理和图像处理中常用的一种运算方式,当给定两个二维离散信号或图像 f ( x , y ) f(x, y) f(x,y) g ( x , y ) g(x, y) g(x,y),其中 f ( x , y ) f(x, y) f(x,y) 表示输入信号或图像, g ( x , y ) g(x, y) g(x,y) 表示卷积核。二维卷积运算可以表示为: h ( x , y ) = ∑ m ∑ n f ( m , n ) ⋅ g ( x − m , y − n ) h(x, y) = \sum_{m}\sum_{n} f(m, n) \cdot g(x-m, y-n) h(x,y)=mnf(m,n)g(xm,yn)其中 ∑ m ∑ n \sum_{m}\sum_{n} mn 表示对所有 m , n m, n m,n 的求和, h ( x , y ) h(x, y) h(x,y) 表示卷积后的输出信号或图像。
在这里插入图片描述
  在数学上,二维卷积运算可以理解为将输入信号或图像 f ( x , y ) f(x, y) f(x,y) 和卷积核 g ( x , y ) g(x, y) g(x,y) 进行对应位置的乘法,然后将所有乘积值相加得到输出信号或图像 h ( x , y ) h(x, y) h(x,y)。这个过程可以用于实现一些信号处理和图像处理的操作,例如模糊、边缘检测、图像增强等。

详见:【深度学习】Pytorch 系列教程(七):PyTorch数据结构:2、张量的数学运算(5):二维卷积及其数学原理

1.1 滤波器(卷积核)

  在图像处理中,卷积经常作为特征提取的有效方法.一幅图像在经过卷积操作后得到结果称为特征映射(Feature Map)。图5.3给出在图像处理中几种常用的滤波器,以及其对应的特征映射.图中最上面的滤波器是常用的高斯滤波器,可以用来对图像进行平滑去噪;中间和最下面的滤波器可以用来提取边缘特征
在这里插入图片描述

# 高斯滤波~平滑去噪
conv_kernel1 = torch.tensor([[1/16, 1/8, 1/16],[1/8, 1/4, 1/8],[1/16, 1/8, 1/16]], dtype=torch.float).unsqueeze(0).unsqueeze(0)
# 提取边缘特征
conv_kernel2 = torch.tensor([[0, 1, 0],[1, -4, 1],[0, 1, 0]], dtype=torch.float).unsqueeze(0).unsqueeze(0)
conv_kernel3 = torch.tensor([[0, 1, 1],[-1, 0, 1],[-1, -1, 0]], dtype=torch.float).unsqueeze(0).unsqueeze(0)
print(conv_kernel1.size())
  • 上述均为3x3的单通道卷积核,需要拓展为四维张量(PyTorch就是这么设计的~)

1.2 PyTorch:卷积操作

def conv2d(img_tensor, conv_kernel):convolved_channels = []for i in range(3):channel_input = img_tensor[:, i, :, :]  # 取出每个通道的输入convolved = F.conv2d(channel_input, conv_kernel, padding=1)  convolved_channels.append(convolved)# 合并各通道卷积后的结果output = torch.cat(convolved_channels, dim=1)# 将张量转换为NumPy数组,进而转换为图像output_img = output.squeeze().permute(1, 2, 0).numpy().astype(np.uint8)output_img = Image.fromarray(output_img)return output_img

2. 图像处理

2.1 图像读取

img = Image.open('1.jpg')  
# img = img.resize((128, 128))  # 调整图像大小img_tensor = torch.tensor(np.array(img), dtype=torch.float).permute(2, 0, 1).unsqueeze(0)print(img_tensor.shape)
  • 将图像转换为PyTorch张量:将通道顺序从HWC转换为CHW,并在第一个维度上增加一个维度~卷积操作使用四维张量

2.2 查看通道

  本部分内容纯属没事儿闲的~

img = Image.open('1.jpg')  
img_tensor = torch.tensor(np.array(img), dtype=torch.float).permute(2, 0, 1).unsqueeze(0)
channel1 = img_tensor[:, 0, :, :]  # 提取每个通道
channel2 = img_tensor[:, 1, :, :] 
channel3 = img_tensor[:, 2, :, :]  
plt.figure(figsize=(12, 12)) 
plt.subplot(1, 4, 1)
plt.imshow(img)
plt.axis('off')
plt.subplot(1, 4, 2)
channel1_img = channel1.squeeze().numpy().astype(np.uint8)
channel1_img = Image.fromarray(channel1_img)
plt.imshow(channel1_img)
plt.axis('off')
plt.subplot(1, 4, 3)
channel2_img = channel2.squeeze().numpy().astype(np.uint8)
channel2_img = Image.fromarray(channel2_img)
plt.imshow(channel2_img)
plt.axis('off')
plt.subplot(1, 4, 4)
channel3_img = channel3.squeeze().numpy().astype(np.uint8)
channel3_img = Image.fromarray(channel3_img)
plt.imshow(channel3_img)
plt.axis('off')

在这里插入图片描述

在这里插入图片描述

2.3 图像处理

def plot_img(img_tensor): output_img1 = conv2d(img_tensor, conv_kernel1)output_img2 = conv2d(img_tensor, conv_kernel2)output_img3 = conv2d(img_tensor, conv_kernel3)plt.subplot(2, 2, 1)plt.title('原始图像', fontproperties=font)plt.imshow(img)plt.axis('off')  plt.subplot(2, 2, 2)plt.title('平滑去噪', fontproperties=font)plt.imshow(output_img1)plt.axis('off')  plt.subplot(2, 2, 3)plt.imshow(output_img2)plt.title('边缘特征1', fontproperties=font)  plt.axis('off')  plt.subplot(2, 2, 4)plt.imshow(output_img3)plt.title('边缘特征2', fontproperties=font)  plt.axis('off')  plt.show()
font = FontProperties(fname='C:\Windows\Fonts\simkai.ttf', size=16)  # 使用楷体
plt.figure(figsize=(12, 12))  # 设置图大小12*12英寸
plot_img(img_tensor)  

在这里插入图片描述

在这里插入图片描述

  • 如图所示,图像提取边缘特征效果明显
  • 但图片过于高清,plt输出的(12英寸)原始图像、平滑去噪图像都很模糊~,下面会先降低像素,然后进行去模糊去噪实验
  • 原图为
    在这里插入图片描述

3. 图像去模糊

img = Image.open('2.jpg')  
img = img.resize((480, 480))  # 调小图像~先使原图变模糊
img_tensor = torch.tensor(np.array(img), dtype=torch.float).permute(2, 0, 1).unsqueeze(0)
conv_kernel4 = torch.tensor([[0, 0, 0],[0, 2, 0],[0, 0, 0]], dtype=torch.float).unsqueeze(0).unsqueeze(0)
conv_kernel5 = torch.ones(3, 3).unsqueeze(0).unsqueeze(0)/9
# print(conv_kernel4-conv_kernel5)
font = FontProperties(fname='C:\Windows\Fonts\simkai.ttf', size=32) 
plt.figure(figsize=(32, 32))  
plt.subplot(2, 2, 1)
plt.title('原始图像', fontproperties=font)
plt.imshow(img)
plt.axis('off')  
plt.subplot(2, 2, 2)
plt.title('线性滤波-2', fontproperties=font)
plt.imshow(conv2d(img_tensor, conv_kernel4))
plt.axis('off')  
plt.subplot(2, 2, 3)
plt.imshow(conv2d(img_tensor, conv_kernel5))
plt.title('均值滤波器:模糊', fontproperties=font)  
plt.axis('off')  
plt.subplot(2, 2, 4)
plt.imshow(conv2d(img_tensor, conv_kernel4-conv_kernel5))
plt.title('锐化滤波器:强调局部差异', fontproperties=font)  
plt.axis('off')  
plt.show()

在这里插入图片描述

4. 图像去噪

4.1 添加随机噪点

img = Image.open('1.jpg')  
img = img.resize((640, 640))  # 调小图像~先使原图变模糊
img_tensor = torch.tensor(np.array(img), dtype=torch.float).permute(2, 0, 1).unsqueeze(0)noise = torch.randn_like(img_tensor)    # 与图像相同大小的随机标准正态分布噪点
noisy_img_tensor = img_tensor + noise   # 将噪点叠加到图像上
noisy_img = noisy_img_tensor.squeeze(0).permute(1, 2, 0).to(dtype=torch.uint8)
noisy_img = Image.fromarray(noisy_img.numpy())

4.2 图像去噪

# conv_kernel1 = torch.tensor([[1/16, 1/8, 1/16],
#                             [1/8, 1/4, 1/8],
#                             [1/16, 1/8, 1/16]], dtype=torch.float).unsqueeze(0).unsqueeze(0)
# # 生成随机3x3高斯分布
# random_gaussian = torch.randn(3, 3).unsqueeze(0).unsqueeze(0)
# print(random_gaussian)
font = FontProperties(fname='C:\Windows\Fonts\simkai.ttf', size=32)  # 使用楷体
plt.figure(figsize=(32, 32))  
plt.subplot(1, 3, 1)
plt.title('原始图像', fontproperties=font)
plt.imshow(img)
plt.axis('off')  
plt.subplot(1, 3, 2)
plt.title('噪点图像', fontproperties=font)
plt.imshow(noisy_img)
plt.axis('off')  
plt.subplot(1, 3, 3)
plt.title('去噪图像', fontproperties=font)
plt.imshow(conv2d(noisy_img_tensor, conv_kernel1))
plt.axis('off') 
plt.show()

在这里插入图片描述

http://www.ds6.com.cn/news/82195.html

相关文章:

  • 内蒙包头网站开发东莞网络营销推广软件
  • 太原模板建站定制seo和sem的概念
  • wordpress excerpt网站关键词百度自然排名优化
  • 深圳西乡做网站点石关键词排名优化软件
  • wordpress主题 magazine信息流优化师招聘
  • 网站备案教育审批号百度认证怎么认证
  • wordpress创建页面模板semseo
  • 网站建设 腾杭州推广平台有哪些
  • 线上网站怎么做今日国际新闻摘抄
  • dw个人网页模板seo综合优化公司
  • 四合一网站建设全网搜索
  • 企业建站做网站徐汇网站建设
  • 深圳专业网站设计公司哪家好搜外网友情链接
  • 做杀人任务的网站焊工培训技术学校
  • 地方网站名称石家庄高级seo经理
  • 房山区网站建设自助建站网站模板
  • 网站渗透案例安全又舒适的避孕方法有哪些
  • 毕节网站建设百度指数的功能
  • 购物网站建设公网址大全123
  • 厦门最快seo深圳关键词seo
  • 微擎pc网站开发免费舆情监测平台
  • 网站开发者常见问题互联网关键词优化
  • p2p网站如何做测试安卓神级系统优化工具
  • 网站优化网站优化推广竞价托管公司
  • 广东装饰公司网站建设优化设计答案大全
  • wordpress注册设置seo面试常见问题及答案
  • 章丘做网站公司百度自然排名优化
  • 中国制造网外贸网网站网络营销课程培训机构
  • 深圳优化网站关键词教育机构加盟
  • 网站有什么作用百度快速查询