Python中的图像处理(第六章)Python图像量化及采样处理(1)

Python中的图像处理(第六章)Python图像量化及采样处理(1)

前言

随着人工智能研究的不断兴起,Python的应用也在不断上升,由于Python语言的简洁性、易读性以及可扩展性,特别是在开源工具和深度学习方向中各种神经网络的应用,使得Python已经成为最受欢迎的程序设计语言之一。由于完全开源,加上简单易学、易读、易维护、以及其可移植性、解释性、可扩展性、可扩充性、可嵌入性:丰富的库等等,自己在学习与工作中也时常接触到Python,这个系列文章的话主要就是介绍一些在Python中常用一些例程进行仿真演示!

本系列文章主要参考杨秀章老师分享的代码资源,杨老师博客主页是Eastmount,杨老师兴趣广泛,不愧是令人膜拜的大佬,他过成了我理想中的样子,希望以后有机会可以向他请教学习交流。

因为自己是做图像语音出身的,所以结合《Python中的图像处理》,学习一下Python,OpenCV已经在Python上进行了多个版本的维护,所以相比VS,Python的环境配置相对简单,缺什么库直接安装即可。本系列文章例程都是基于Python3.8的环境下进行,所以大家在进行借鉴的时候建议最好在3.8.0版本以上进行仿真。本文继续来对本书第六章的前5个例程进行介绍。

一. Python准备

如何确定自己安装好了python

win+R输入cmd进入命令行程序
在这里插入图片描述
点击“确定”
在这里插入图片描述
输入:python,回车
在这里插入图片描述
看到Python相关的版本信息,说明Python安装成功。

二. Python仿真

(1)新建一个chapter06_01.py文件,输入以下代码,图片也放在与.py文件同级文件夹下

# -*- coding: utf-8 -*-
# BY:Eastmount CSDN 2020-11-10
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('lena.png')

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像
new_img = np.zeros((height, width, 3), np.uint8)

#图像量化操作 量化等级为2
for i in range(height):
    for j in range(width):
        for k in range(3): #对应BGR三分量
            if img[i, j][k] < 128:
                gray = 0
            else:
                gray = 128
            new_img[i, j][k] = np.uint8(gray)
        
#显示图像
cv2.imshow("src", img)
cv2.imshow("Quantization", new_img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

保存.py文件
输入eixt()退出python,输入命令行进入工程文件目录
在这里插入图片描述
输入以下命令,跑起工程

python chapter06_01.py

在这里插入图片描述
没有报错,直接弹出图片,运行成功!
在这里插入图片描述
在这里插入图片描述

(2)新建一个chapter06_02.py文件,输入以下代码,图片也放在与.py文件同级文件夹下

# -*- coding: utf-8 -*-
# BY:Eastmount CSDN 2020-11-10
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('lena.png')

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#创建一幅图像
new_img1 = np.zeros((height, width, 3), np.uint8)
new_img2 = np.zeros((height, width, 3), np.uint8)
new_img3 = np.zeros((height, width, 3), np.uint8)

#图像量化等级为2的量化处理
for i in range(height):
    for j in range(width):
        for k in range(3): #对应BGR三分量
            if img[i, j][k] < 128:
                gray = 0
            else:
                gray = 128
            new_img1[i, j][k] = np.uint8(gray)

#图像量化等级为4的量化处理
for i in range(height):
    for j in range(width):
        for k in range(3): #对应BGR三分量
            if img[i, j][k] < 64:
                gray = 0
            elif img[i, j][k] < 128:
                gray = 64
            elif img[i, j][k] < 192:
                gray = 128
            else:
                gray = 192
            new_img2[i, j][k] = np.uint8(gray)

#图像量化等级为8的量化处理
for i in range(height):
    for j in range(width):
        for k in range(3): #对应BGR三分量
            if img[i, j][k] < 32:
                gray = 0
            elif img[i, j][k] < 64:
                gray = 32
            elif img[i, j][k] < 96:
                gray = 64
            elif img[i, j][k] < 128:
                gray = 96
            elif img[i, j][k] < 160:
                gray = 128
            elif img[i, j][k] < 192:
                gray = 160
            elif img[i, j][k] < 224:
                gray = 192
            else:
                gray = 224
            new_img3[i, j][k] = np.uint8(gray)

#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']

#显示图像
titles = ['(a) 原始图像', '(b) 量化-L2', '(c) 量化-L4', '(d) 量化-L8']  
images = [img, new_img1, new_img2, new_img3]  
for i in range(4):  
   plt.subplot(2,2,i+1), plt.imshow(images[i], 'gray'), 
   plt.title(titles[i])  
   plt.xticks([]),plt.yticks([])  
plt.show()


保存.py文件输入以下命令,跑起工程

python chapter06_02.py

在这里插入图片描述
没有报错,直接弹出图片,运行成功!
在这里插入图片描述

(3)新建一个chapter06_03.py文件,输入以下代码,图片也放在与.py文件同级文件夹下

# coding: utf-8
# BY:Eastmount CSDN 2020-11-10
import cv2
import numpy as np
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('nv.png') 

#图像二维像素转换为一维
data = img.reshape((-1,3))
data = np.float32(data)

#定义中心 (type,max_iter,epsilon)
criteria = (cv2.TERM_CRITERIA_EPS +
            cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

#设置标签
flags = cv2.KMEANS_RANDOM_CENTERS

#K-Means聚类 聚集成4
compactness, labels, centers = cv2.kmeans(data, 4, None, criteria, 10, flags)


#图像转换回uint8二维类型
centers = np.uint8(centers)
res = centers[labels.flatten()]
dst = res.reshape((img.shape))

#图像转换为RGB显示
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst = cv2.cvtColor(dst, cv2.COLOR_BGR2RGB)


#用来正常显示中文标签
plt.rcParams['font.sans-serif']=['SimHei']

#显示图像
titles = ['原始图像', '聚类量化 K=4']  
images = [img, dst]  
for i in range(2):  
   plt.subplot(1,2,i+1), plt.imshow(images[i], 'gray'), 
   plt.title(titles[i])  
   plt.xticks([]),plt.yticks([])  
plt.show()

保存.py文件输入以下命令,跑起工程

python chapter06_03.py

在这里插入图片描述
没有报错,直接弹出图片,运行成功!
在这里插入图片描述
(4)新建一个chapter06_04.py文件,输入以下代码,图片也放在与.py文件同级文件夹下

# -*- coding: utf-8 -*-
# BY:Eastmount CSDN 2020-11-10
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('lena.png')

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#采样转换成16*16区域
numHeight = int(height/16)
numWidth = int(width/16)

#创建一幅图像
new_img = np.zeros((height, width, 3), np.uint8)

#图像循环采样16*16区域
for i in range(16):
    #获取Y坐标
    y = i*numHeight
    for j in range(16):
        #获取X坐标
        x = j*numWidth
        #获取填充颜色 左上角像素点
        b = img[y, x][0]
        g = img[y, x][1]
        r = img[y, x][2]
        
        #循环设置小区域采样
        for n in range(numHeight):
            for m in range(numWidth):
                new_img[y+n, x+m][0] = np.uint8(b)
                new_img[y+n, x+m][1] = np.uint8(g)
                new_img[y+n, x+m][2] = np.uint8(r)
        
#显示图像
cv2.imshow("src", img)
cv2.imshow("Sampling", new_img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

保存.py文件输入以下命令,跑起工程

python chapter06_04.py

在这里插入图片描述
没有报错,直接弹出图片,运行成功!
在这里插入图片描述
在这里插入图片描述

(5)新建一个chapter06_05.py文件,输入以下代码,图片也放在与.py文件同级文件夹下

# -*- coding: utf-8 -*-
# BY:Eastmount CSDN 2020-11-10
import cv2  
import numpy as np  
import matplotlib.pyplot as plt

#读取原始图像
img = cv2.imread('scenery.png')

#获取图像高度和宽度
height = img.shape[0]
width = img.shape[1]

#采样转换成8*8区域
numHeight = int(height/8)
numwidth = int(width/8)

#创建一幅图像
new_img = np.zeros((height, width, 3), np.uint8)

#图像循环采样8*8区域
for i in range(8):
    #获取Y坐标
    y = i*numHeight
    for j in range(8):
        #获取X坐标
        x = j*numwidth
        #获取填充颜色 左上角像素点
        b = img[y, x][0]
        g = img[y, x][1]
        r = img[y, x][2]
        
        #循环设置小区域采样
        for n in range(numHeight):
            for m in range(numwidth):
                new_img[y+n, x+m][0] = np.uint8(b)
                new_img[y+n, x+m][1] = np.uint8(g)
                new_img[y+n, x+m][2] = np.uint8(r)
        
#显示图像
cv2.imshow("src", img)
cv2.imshow("Sampling", new_img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

保存.py文件输入以下命令,跑起工程

python chapter06_05.py

在这里插入图片描述
没有报错,直接弹出图片,运行成功!
在这里插入图片描述
在这里插入图片描述

三. 小结

本文主要介绍在Python中调用OpenCV库对图像进行像素级的量化,K-Means聚类分析,以及循环采样填充等操作。由于本书的介绍比较系统全面,所以会出一个系列文章进行全系列仿真实现,下一篇文章将继续介绍第六章节的最后5例仿真实例,感兴趣的还是建议去原书第六章深入学习理解。每天学一个Python小知识,大家一起来学习进步阿!

本系列示例主要参考杨老师GitHub源码,安利一下地址:ImageProcessing-Python(喜欢记得给个star哈!)

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mozun2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值