IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> Python图像处理笔记——卷积 -> 正文阅读

[人工智能]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] = 0
    im_edges = img / (np.max(img_edge) - np.min(img_edge))
    thresh = threshold_otsu(im_edges)
    im_edges_binary = img > thresh
    print(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')) / 255
    print('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 int
    im_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 noise
    correlation = signal.correlate2d(face_image, template_image, boundary="symm", mode='same')
    y, x = np.unravel_index(np.argmax(correlation), correlation.shape)  # find the match
    fig, 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

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-07-27 16:13:42  更:2021-07-27 16:15:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/27 6:14:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码