(超全)python图像处理详细解析(3)

图像处理

    • 23.保存视频每一帧图像
    • 24.把png图像转换成jpg并保存
    • 25.改变图像尺寸
    • 26.改变图像比例
    • 27.旋转图像
    • 28.亮度调整
    • 29.log对数调整
    • 30.判断图像对比度
    • 31.调整强度
      • (1)强度调节
      • (2)uint8转float
    • 32.绘制直方图和均衡化
    • 33.彩色图片三通道直方图

23.保存视频每一帧图像

import cv2
from skimage import io
import os

class AVILoader:
    def __init__(self, video_file):
        self.video_file = video_file
        self.cap = cv2.VideoCapture(self.video_file)

    def __call__(self, frame):
        self.cap.set(cv2.CAP_PROP_POS_FRAMES, frame)
        ret, frame = self.cap.read()
        if ret:
            return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        else:
            return None

video_file = 'sp.mp4'
av_loader = AVILoader(video_file)

frames = range(0, 100, 20)
output_folder = 'frames'
os.makedirs(output_folder, exist_ok=True)

# 保存每一帧为图像文件
for frame in frames:
    img = av_loader(frame)
    if img is not None:
        filename = os.path.join(output_folder, f'frame_{frame}.jpg')
        io.imsave(filename, img)
        io.imshow(img)  # 显示图像
        io.show()       # 显示图像窗口

# 创建图像集合
ic = io.ImageCollection(os.path.join(output_folder, '*.jpg'))
# 输出图像集合

在这里插入图片描述

24.把png图像转换成jpg并保存

from skimage import io, transform, color
import numpy as np

def convert_gray(f):
    rgb = io.imread(f)
    gray = color.rgb2gray(rgb)
    dst = transform.resize(gray, (256, 256))

    return dst

# 指定文件夹路径
#data_dir = 'D:\rgzn\wl'  

# 读取文件夹中的所有png文件
image_dir = 'D:/rgzn/wl'
str=image_dir+'/*.png'
coll = io.ImageCollection(str, load_func=convert_gray)

# 保存处理后的图像
for i in range(len(coll)):
# 保存为jpg格式,注意文件路径和文件名的拼接
io.imsave("D:/rgzn/wl1" + np.str(i) + '.jpg', coll[i])  

在这里插入图片描述

25.改变图像尺寸

改变图片尺寸resize(200,200)
函数格式为:
skimage.transform.resize(image, output_shape)
image: 需要改变尺寸的图片
output_shape: 新的图片尺寸

from skimage import transform,data,io
import matplotlib.pyplot as plt
img=io.imread('mao.jpg')
a=img.shape

plt.subplot(2,2,1)
plt.title('1')
plt.imshow(img)


plt.subplot(2,2,2)
plt.title('2')
dst=transform.resize(img,(700,500))
plt.imshow(dst)

plt.subplot(2,2,3)
plt.title('3')
dst=transform.resize(img,(350,250))
plt.imshow(dst)

plt.subplot(2,2,4)
plt.title('4')
dst=transform.resize(img,(175,125))
plt.imshow(dst)

在这里插入图片描述

26.改变图像比例

按比例缩放rescale

from skimage import transform,data,io
import matplotlib.pyplot as plt
img=io.imread('mao.jpg')

plt.subplot(1,2,1)
plt.title('1')
plt.imshow(img)

plt.subplot(1,2,2)
plt.title('2')
#将原始图像放缩到原来的0.1倍
dst=transform.rescale(img,0.1)
plt.imshow(dst)

在这里插入图片描述

27.旋转图像

旋转 rotate

from skimage import transform,data,io
import matplotlib.pyplot as plt

# 读取图像
img = io.imread('mao.jpg')
print(img.shape)

# 旋转图像
img1 = transform.rotate(img, 60)
print(img1.shape)

img2 = transform.rotate(img, 30, resize=True)
print(img2.shape)

# 使用同一个窗口显示两个旋转后的图像
plt.figure('resize')

# 创建子图布局
plt.subplot(1, 2, 1)
plt.title('1')
plt.imshow(img1, plt.cm.gray)

plt.subplot(1, 2, 2)
plt.title('2')
plt.imshow(img2, plt.cm.gray)

# 显示窗口
plt.show()

在这里插入图片描述

28.亮度调整

图像亮度与对比度的调整,是放在skimage包的exposure模块里面
对原图像的像素,进行幂运算,得到新的像素值。公式中的g就是gamma值。
如果gamma>1, 新图像比原图像暗
如果gamma<1,新图像比原图像亮
函数格式为:skimage.exposure.adjust_gamma(image, gamma=1)
gamma参数默认为1,原像不发生变化 。

import numpy as np
from skimage import transform,exposure,img_as_float
import matplotlib.pyplot as plt

img=io.imread('iii.jpg')
#gam1是原始图像调暗3倍,而gam2是原始图像调亮0.1倍。
gam1= exposure.adjust_gamma(img,3)  #调暗
gam2= exposure.adjust_gamma(img,0.1)  #调亮
plt.figure('adjust_gamma',figsize=(8,8))

plt.subplot(131)
plt.title('1')
plt.imshow(img,plt.cm.gray)
plt.axis('off')

plt.subplot(132)
plt.title('2')
plt.imshow(gam1,plt.cm.gray)
plt.axis('off')

plt.subplot(133)
plt.title('3')
plt.imshow(gam2,plt.cm.gray)
plt.axis('off')

plt.show()

在这里插入图片描述

29.log对数调整

与gamma相反

import numpy as np
from skimage import transform,exposure,img_as_float,io
import matplotlib.pyplot as plt

img = io.imread('iii.jpg')
#使用exposure模块中的adjust_log函数对读取的图像进行对数变换
gam1 = exposure.adjust_log(img)
#创建一个名为'adjust_gamma'的图表,设置图表大小为(8,8)。
plt.figure('adjust_gamma',figsize=(8,8))

plt.subplot(121)
plt.title('origin image')
#图中显示变量img的图像,使用灰度色彩映射。
plt.imshow(img,plt.cm.gray)
plt.axis('off')

plt.subplot(122)
plt.title('log')
plt.imshow(gam1,plt.cm.gray)
plt.axis('off')

plt.show()

在这里插入图片描述

30.判断图像对比度

函数:is_low_contrast(img)

from skimage import exposure,img_as_float,io
img1 = io.imread('liii.png')
#判断图像是否为低对比度图像
result=exposure.is_low_contrast(img1)
print(result)

运行结果:
False

31.调整强度

(1)强度调节

import numpy as np
from skimage import exposure
#创建一个包含三个元素的一维数组,元素值分别为51, 102, 153,数据类型为无符号8位整数。
image = np.array([51, 102, 153], dtype=np.uint8)
#rescale_intensity函数对输入的图像强度进行重新缩放
mat=exposure.rescale_intensity(image)
print(mat)

运行结果:
[ 0 127 255]

(2)uint8转float

通过img_as_float()函数将unit8类型转换为float型,实际上还有更简单的方法,就是乘以1.0

import numpy as np
image = np.array([51, 102, 153], dtype=np.uint8)
print(image*1.0)

运行结果:
[ 51. 102. 153.]

float类型的范围是[0,1],因此对float进行rescale_intensity 调整后,范围变为[0,1],而不是[0,255]

import numpy as np
from skimage import exposure
image = np.array([51, 102, 153], dtype=np.uint8)
tmp=image*1.0
#rescale_intensity函数对输入的图像强度进行重新缩放
mat=exposure.rescale_intensity(tmp)  
print(mat)

运行结果:
[0. 0.5 1. ]

32.绘制直方图和均衡化

绘图都可以调用matplotlib.pyplot库来进行,其中的hist函数可以直接绘制直方图。

n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')

hist的参数非常多,但常用的就这六个,只有第一个是必须的,后面四个可选
arr: 需要计算直方图的一维数组
bins: 直方图的柱数,可选项,默认为10
normed: 是否将得到的直方图向量归一化。默认为0
facecolor: 直方图颜色
edgecolor: 直方图边框颜色
alpha: 透明度
histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’

from skimage import io
import matplotlib.pyplot as plt


img = io.imread('iii.jpg')
img1 = io.imread('liii.png')

plt.figure('hist')

plt.subplot(221)  
plt.title('1')
plt.imshow(img)
plt.subplot(222)  
plt.title('2')
#绘制图像直方图
n, bins, patches = plt.hist(img.ravel(), bins=256, density=True, edgecolor='None', facecolor='black')
plt.axis('off')  

plt.subplot(223)  
plt.title('3')
plt.imshow(img1)
plt.subplot(224)  
plt.title('4')
#绘制图像直方图
n, bins, patches = plt.hist(img1.ravel(), bins=256, density=True, edgecolor='None', facecolor='black')
plt.axis('off')  

plt.show()

在这里插入图片描述

33.彩色图片三通道直方图

from skimage import io
import matplotlib.pyplot as plt

img=io.imread('iii.jpg')
ar=img[:,:,0].flatten()
plt.hist(ar,bins=256,density=True,facecolor='r',edgecolor='r')
ag=img[:,:,1].flatten()
plt.hist(ag,bins=256,density=True,facecolor='g',edgecolor='g')
ab=img[:,:,2].flatten()
plt.hist(ab,bins=256,density=True,facecolor='g',edgecolor='b')
plt.show()

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/577475.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于streamlit快速部署机器学习项目(Public URL)

基于streamlit的AIGC项目前端展示 1.Streamlit 简介与入门1.1 安装 Streamlit1.2 开发Streamlit应用程序1.3 启动并运行1.3.1 本地运行1.3.2 部署 现在LLM技术发展迅速&#xff0c;很多人在学习的时候&#xff0c;都想展示效果&#xff0c;并且想部署在服务器上&#xff0c;但是…

原型链prototype、__proto、constructor的那些问题整理

再了解原型链之前,我们先来看看构造函数和实例对象的打印结构 - 函数 这里我们定义一个构造函数Fn,然后打印它的结构吧 function Fn(){} console.dir(Fn)控制台得到结构 从上面结构我们能看的出来,函数有两种原型,一种是作为函数特有的原型:prototype,另一种是作为对象的__…

数字旅游:通过科技赋能,创新旅游服务模式,提供智能化、个性化的旅游服务,满足游客多元化、个性化的旅游需求

目录 一、数字旅游的概念与内涵 二、科技赋能数字旅游的创新实践 1、大数据技术的应用 2、人工智能技术的应用 3、物联网技术的应用 4、云计算技术的应用 三、智能化、个性化旅游服务的实现路径 1、提升旅游服务的智能化水平 2、实现旅游服务的个性化定制 四、数字旅…

计算机网络大框架图形

如标题&#xff0c;精心画了一个计算机网络的框架性的图&#xff0c;包含了计算机网络的核心思想&#xff0c;在此分享和备份下。各层具体协议参考TCP/IP常用协议栈图解-CSDN博客

工厂数字化三部曲/业务、数据和IT融合

工厂数字化三部曲: 业务、数据和IT融合 在当今数字化转型的潮流中&#xff0c;企业面临着将业务、数据和IT融合的挑战和机遇。数字化转型不仅是技术上的升级&#xff0c;更是对企业运营模式和管理体系的全面优化和重构。通过业务“数字化”阶段的细致分析和整合&#xff0c;以及…

葡萄牙语翻译中文难吗,葡译中如何翻译比较好?

葡萄牙&#xff0c;一个充满魅力的国度&#xff0c;拥有着悠久的历史和丰富的文化传统。葡萄牙语作为这个国家的官方语言&#xff0c;在全球范围内享有广泛的声誉。那么&#xff0c;葡萄牙语翻译中文难吗&#xff1f;又该如何进行高质量的翻译呢&#xff1f; 业内人士指出&…

蛋糕购物商城

蛋糕购物商城 运行前附加数据库.mdf&#xff08;或使用sql生成数据库&#xff09; 登陆账号&#xff1a;admin 密码&#xff1a;123456 修改专辑价格时去掉&#xffe5;以及上传专辑图片 c#_asp.net 蛋糕购物商城 网上商城 三层架构 在线购物网站&#xff0c;电子商务系统 …

打造智能语音机器人-用语音控制机器人

人工智能现已成为国家发展重大战略&#xff0c;智能语音技术作为人工智能产业链上的关键一环&#xff0c;AI应用成熟的技术之一&#xff0c;人工智能的发展也进入了一个崭新的阶段。那么打造智能语音机器人怎样实现用语音控制机器人呢&#xff1f;和小编一起来看看。 选择合适的…

七天速记前端八股文(重点)

for in和正常for循环的区别 在遍历数组时&#xff0c;正常的 for 循环通常比 for...in 循环更适合。虽然 for...in 循环可以用于遍历数组&#xff0c;但它有一些潜在的问题和限制。 下面是一些使用 for 循环相对于 for...in 循环的优势&#xff1a; 顺序遍历&#xff1a;for…

61、回溯-分割回文串

思路&#xff1a; 还是全排列的思路&#xff0c;列出每一种组合&#xff0c;然后验证是否是回文&#xff0c;如果是子串放入path中&#xff0c;在验证其他元素是否也是回文。代码如下&#xff1a; class Solution {// 主方法&#xff0c;用于接收一个字符串s并返回所有可能的…

Prometheus数据模型与查询语言:构建高效监控系统的关键

&#x1f407;明明跟你说过&#xff1a;个人主页 &#x1f3c5;个人专栏&#xff1a;《Prometheus&#xff1a;监控的神》 &#x1f3c5; &#x1f516;行路有良友&#xff0c;便是天堂&#x1f516; 目录 一、引言 1、Prometheus诞生史 二、Prometheus的数据模型与查询语…

想要应聘前端工程师——了解前端招聘需求

市场对前端工程师的需求依然旺盛。所谓知己知彼,百战不殆,分析各个公司对前端工程师的招聘需求,一方面可以了解到前端各细分领域在企业的需求情况,调整自己对岗位和薪资的期待,另一方面可以获得各种前端技术在企业中的应用情况,调整自己的学习和面试准备方向。因篇幅所限…

Office Word自动编号转文本

原理 使用office自带的宏功能&#xff0c;一键替换 过程 调出word的“开发工具”选项 文件->选项->自定义功能区->选中开发工具->确定 创建宏 开发工具->宏->创建宏 编写宏 在弹出来的框里&#xff0c;替换代码为 Sub num2txt() ActiveDocument.…

分布式版本控制系统——Git

分布式版本控制系统——Git 一、Git安装二、创建版本库三、将文件交给Git管理四、Git的工作区和暂存区1.工作区&#xff08;Working Directory&#xff09;2.版本库 五、版本回退和撤销修改1.版本回退2.撤销修改 六、删除文件七、常用基础命令总结八、参考 分布式版本控制系统&…

ETL简介以及使用ETL(Kettle)进行数据接入的具体例子

目录 ETL介绍 ETL简介 ETL包含的三部分 ETL基本概念 ETL资源库 ETL变量 业务表梳理以及接入规划 数据接入流程 业务表梳理 ETL任务规范 接入规划 数据接入中的方便工具 具体例子 导出生产表信息 1、ORACLE 2、MYSQL ETL数据增量抽取任务开发 1、ORACLE通用流程…

外观模式【结构型模式C++】

1.概述 外观模式是一种结构型设计模式&#xff0c; 能为程序库、 框架或其他复杂类提供一个简单的接口。 2.结构   外观角色&#xff08;Facade&#xff09;&#xff1a;为多个子系统对外提供一个共同的接口&#xff0c;知道哪些子系统负责处理请求&#xff0c;将客户端的请…

机器学习-保险花销预测笔记+代码

读取数据 import numpy as np import pandas as pddatapd.read_csv(rD:\人工智能\python视频\机器学习\5--机器学习-线性回归\5--Lasso回归_Ridge回归_多项式回归\insurance.csv,sep,) data.head(n6) EDA 数据探索 import matplotlib.pyplot as plt %matplotlib inlineplt.hi…

六天以太坊去中心化租房平台,前端+合约源码

六天以太坊去中心化租房平台 概述项目结构合约部署运行项目功能介绍一、首页二、房东后台我的房屋我的订单上架新房屋 三、租户后台我的房屋我的订单 四、仲裁后台 下载地址 概述 六天区块链房屋租赁系统&#xff0c;采用去中心化的方式实现了房屋的租赁功能。房东可在平台上托…

Linux基础——Linux开发工具(gcc/g++,gdb)

前言&#xff1a;在上一篇我们简单介绍了yum&#xff0c;vim的一些常用的指令和模式&#xff0c;现在让我们来进一步了解其他的Linux环境基础开发工具gcc/g&#xff0c;gdb。 如果对前面yum和vim有什么不懂的建议回顾去回顾上期知识&#xff01;&#xff01;&#xff01; Linu…

C语言基础:初识指针(二)

当你不知道指针变量初始化什么时&#xff0c;可以初始化为空指针 int *pNULL; 我们看NULL的定义&#xff0c;可以看出NULL是0被强制转化为Void* 类型的0&#xff1b;实质还是个0&#xff1b; 如何避免野指针&#xff1a; 1. 指针初始化 2. 小心指针越界 3. 指针指向空间…
最新文章