Python图像处理笔记——卷积
Python图像处理——卷积
- 一、什么是卷积?
- 1. 数学定义
- 2. 引入库
- 3. python实现对图像的卷积
- 二、相关与卷积
- 1. 相关的定义
- 2. Python实现
- 扩展阅读
一、什么是卷积?
1. 数学定义
函数 [公式] 的卷积 [公式] 如下:
2. 引入库
代码如下:
import matplotlib.pylab as pylab
from skimage.color import rgb2gray
from skimage.io import imread
import numpy as np
from scipy import signal, misc, ndimage
from skimage.filters import threshold_otsu
3. python实现对图像的卷积
对图像进行卷积可以实现模糊、浮雕、边缘提取等效果,是一种常用的图像处理基础算法。为了在此过程中熟悉不同的图像处理模块,这里使用了多种处理方式,它们实现的功能是相同的。 具体实现代码如下:
#(1) 对灰度图像进行卷积,模糊
def grayCon():img = rgb2gray(imread(r'..\cameraman.jpg').astype(float))# print(np.max(img)) #255# print(img.shape) #(255,255)blur_kernel = np.ones((3, 3)) / 9 # box模糊卷积核laplace_kernel = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) # 拉普拉斯边缘检测卷积核img_blured = signal.convolve2d(img, blur_kernel)img_edge = np.clip(signal.convolve2d(img, laplace_kernel), 0, 10) # 给数组限定范围 np.clip(array,min,max)img_edge[img_edge < 0] = 0im_edges = img / (np.max(img_edge) - np.min(img_edge))thresh = threshold_otsu(im_edges)im_edges_binary = img > threshprint(img_blured.shape, im_edges.shape, im_edges_binary.shape)fig, axes = pylab.subplots(ncols=4, sharex=True, sharey=True, figsize=(24, 6))axes[0].imshow(img, cmap=pylab.cm.gray)axes[0].set_title('original image', size=20)axes[1].imshow(img_blured, cmap=pylab.cm.gray)axes[1].set_title('box blured image', size=20)axes[2].imshow(img_edge, cmap=pylab.cm.gray)axes[2].set_title('laplace edge detection', size=20)axes[3].imshow(im_edges_binary, cmap=pylab.cm.gray)axes[3].set_title('binary edge detection', size=20)for ax in axes:ax.axis('off')pylab.show()# (2) 对彩色图像的每个通道进行卷积,浮雕
def RGBcon():im = np.array(imread(r'..\tajmahal.jpg')) / 255print('max pixel value: ' + str(np.max(im)))print('shape of image: ' + str(im.shape))emboss_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]])edge_scharr_kernel = np.array([[-3 - 3j, 0 - 10j, +3 - 3j], [-10 + 0j, 0 + 0j, +10 + 0j], [-3 + 3j, 0 + 10j, +3 + 3j]])im_embossed = np.ones(im.shape)im_edges = np.ones(im.shape)for i in range(3):im_embossed[:, :, i] = np.clip(signal.convolve2d(im[..., i], emboss_kernel,mode='same', boundary="symm"), 0, 1)for i in range(3):im_edges[:, :, i] = np.clip(np.real(signal.convolve2d(im[..., i], edge_scharr_kernel,mode='same', boundary="symm")), 0, 1)fig, axes = pylab.subplots(ncols=3, figsize=(20, 30))axes[0].imshow(im)axes[0].set_title('original image', size=20)axes[1].imshow(im_embossed)axes[1].set_title('embossed image', size=20)axes[2].imshow(im_edges)axes[2].set_title('scharr edge detection', size=20)for ax in axes:ax.axis('off')pylab.show()# (3) 直接对彩色图像进行卷积,锐化
def RGBconv1():im = imread(r'..\vic.png').astype(np.float)# print('max pixel value: ' + str(np.max(im))) # 255# print('shape of image: ' + str(im.shape)) # (540,720,4)sharpen_kernel = np.array([0, -1, 0, -1, 5, -1, 0, -1, 0]).reshape(3, 3, 1)emboss_kernel = np.array([[-2, -1, 0], [-1, 1, 1], [0, 1, 2]]).reshape(3, 3, 1)im_sharp = ndimage.convolve(im, sharpen_kernel, mode='nearest')im_sharp = np.clip(im_sharp, 0, 255).astype(np.uint8) # unsigned intim_emboss = ndimage.convolve(im, emboss_kernel, mode='nearest')# im_emboss = np.clip(im_emboss, 0, 255).astype(np.uint8)im_emboss = im_emboss.astype(np.uint8)pylab.figure(figsize=(10, 30))pylab.subplot(1, 3, 1), pylab.imshow(im.astype(np.uint8)), pylab.axis('off')pylab.title('original image', size=20)pylab.subplot(1, 3, 2), showimage(im_sharp, 'sharpened Image')pylab.subplot(1, 3, 3), showimage(im_emboss, 'embossed image')pylab.tight_layout()pylab.show()
上述代码封装为三个独立的函数。
最简单的是实现对灰度图像卷积,直接调用signal.convolve2d(image, kernel)即可。
对彩色图像的卷积可以用两种方式实现:
(1)对多个通道分别进行卷积操作后叠加;
(2)调用ndimage.convolve(image, sharpen_kernel)实现。
二、相关与卷积
1. 相关的定义
相关将核相对于水平和垂直轴翻转两次。
2. Python实现
def corre():face_image = misc.face(gray=True) - misc.face(gray=True).mean()template_image = np.copy(face_image[300:365, 670:750]) # 选择右眼区域template_image -= template_image.mean()face_image = face_image + np.random.randn(*face_image.shape) * 50 # random noisecorrelation = signal.correlate2d(face_image, template_image, boundary="symm", mode='same')y, x = np.unravel_index(np.argmax(correlation), correlation.shape) # find the matchfig, axes = pylab.subplots(1, 3, figsize=(15, 5))axes[0].imshow(face_image, cmap='gray')axes[0].set_title('Original Image', size=20)axes[0].plot(x, y, 'ro')axes[1].imshow(template_image, cmap='gray')axes[1].set_title('Template Image', size=20)axes[2].imshow(correlation, cmap='afmhot')axes[2].set_title('cross-correlation Image', size=20)pylab.show()
扩展阅读
关于卷积的概念可以阅读:
https://www.zhihu.com/question/22298352
Python图像处理笔记——卷积相关推荐
- python图像处理笔记-十二-图像聚类
python图像处理笔记-十二-图像聚类 学习内容 这一章主要在学习的是聚类算法以及其在图像算法中的应用,主要学习的聚类方法有: KMeans 层次聚类 谱聚类 并将使用他们对字母数据及进行聚类处理, ...
- Python图像处理笔记——傅里叶变换
文章目录 一.前言 二.傅里叶变换在图像中的应用 0. 本文用到的库 1. 图像的傅里叶变换和逆变换 2. 高斯模糊 3. 傅里叶变换频域滤波 (1)低通滤波 (2)高通滤波 (3)带通滤波 一.前言 ...
- python图像处理笔记(六):手动获取坐标标注图像
引言 之前的两篇文章有提到图片标注,但一个是用yolo算法给识别到的图像加框,一个是根据霍夫变换做直线与圆的检测,本篇想总结一下根据鼠标点击来对图像做一些填充处理的方式. opencv鼠标事件 介绍的 ...
- 数字图像处理与Python实现笔记之图像特征提取
数字图像处理与Python实现笔记 摘要 绪论 1 数字图像处理基础知识 2 彩色图像处理初步 3 空间滤波 4 频域滤波 5 图像特征提取 5.1 图像颜色特征提取 5.1.1 颜色直方图 1 一般 ...
- 数字图像处理与Python实现笔记之频域滤波
数字图像处理与Python实现笔记 摘要 绪论 1 数字图像处理基础知识 2 彩色图像处理初步 3 空间滤波 4 频域滤波 4.1 傅里叶变换 4.1.1 一维傅里叶变换 4.1.2 二维傅里叶变换 ...
- 数字图像处理与Python实现笔记之基础知识
数字图像处理与Python实现笔记之基础知识 摘要 绪论 1 数字图像处理基础知识 1.1 数字图像简介 1.1.1 数字图像处理的目的 1.1.2 数字图像处理的应用 1.1.3 数字图像处理的特点 ...
- python 图像分析自然纹理方向与粗细代码_数字图像处理与Python实现笔记之基础知识...
数字图像处理与Python实现笔记之基础知识 摘要 绪论 1 数字图像处理基础知识 1.1 数字图像简介 1.1.1 数字图像处理的目的 1.1.2 数字图像处理的应用 1.1.3 数字图像处理的特点 ...
- 数字图像处理与Python实现笔记之空间滤波
数字图像处理与Python实现笔记之空间滤波 摘要 绪论 1 数字图像处理基础知识 2 彩色图像处理初步 3 空间滤波 3.1 空间滤波基础 3.1.1 空间滤波的机理 3.1.2 空间滤波器模板 3 ...
- 数字图像处理与Python实现笔记之彩色图像处理初步
数字图像处理与Python实现笔记之彩色图像处理初步 摘要 绪论 1 数字图像处理基础知识 2 彩色图像处理初步 2.1 彩色图像的颜色空间 2.1.1 RGB颜色空间 2.1.2 HSI颜色空间 2 ...
最新文章
- Linux系统管理学习路线图
- Java学习笔记六 常用API对象二
- ajax 设置Access-Control-Allow-Origin实现跨域访问
- webclient无法获取html文件,C# WebClient获取网页源码的方法
- 卡尔曼滤波,最最容易理解的讲解.找遍网上就这篇看懂了(转载)
- TCP和UDP服务器性能测试工具
- 重温数据结构——(1)
- Java验证码生成工具类(简洁高效)
- 柔性电流传感器(柔性电流探头)的工作原理和特点是什么?
- 英语3500词(十三)society主题(2022.1.25)
- 云服务器系统设计,云服务器 用户系统设计
- 【物联网中间件平台-02】YFIOs技术白皮书(V1.1)
- ASP.NET/C# 控制器Controller的深入理解
- 网络基础知识点归纳(牛客网络专项练习题)
- prometheus-简介
- 项目管理领域关键知识点之横道图和网络图
- 3.3.1-取词干和词形还原
- 【量化交易】 python 基本语法与变量 【003】 策略 复习一下
- 自动检测文本文件编码是否为GB2312(简体中文),并转换为UTF8编码,附一个GB2312全区对应的utf8编码码表
- 微信小程序项目——校园新闻网
热门文章
- 【白板动画制作软件】万彩手影大师教程 | 手影大师用户指引
- Oracle EBS 术语解释-中文版
- 从《流浪地球》撤资?——“数据思维”害的
- Spring MVC 项目 JSP 页面显示源码
- 数据归约——主成分分析PCA
- access里的多步oledb错误_OLEDB 错误
- ubuntu 安装360
- 《COOL3D 中文金典版 COOL3D STUDIO (原亮剑版更新)》(COOL 3D Production Studio
- ethers.js Metamask和CoinbaseWallet,TronLink钱包连接方式
- SAP 新人入行太难