Python OpenCV中的numpy与图像类型转换操作


Posted in Python onDecember 11, 2020

Python OpenCV存储图像使用的是Numpy存储,所以可以将Numpy当做图像类型操作,操作之前还需进行类型转换,转换到int8类型

import cv2
import numpy as np
# 使用numpy方式创建一个二维数组
img = np.ones((100,100))
# 转换成int8类型
img = np.int8(img)
# 颜色空间转换,单通道转换成多通道, 可选可不选
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.imwrite("demo.jpg", img)

补充知识:Python中读取图片并转化为numpy.ndarray()数据的6种方式

方式:                                        返回类型

OpenCV                                      np.ndarray
PIL                                               PIL.JpegImagePlugin.JpegImageFile
keras.preprocessing.image         PIL.JpegImagePlugin.JpegImageFile
Skimage.io                                  np.ndarray
matplotlib.pyplot                          np.ndarray
matplotlib.image                          np.ndarray

import numpy as np
import cv2
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from PIL import Image
import skimage.io as io
import matplotlib.pyplot as plt
import matplotlib.image as mpig 
 
'''
方式:   返回类型
OpenCV   np.ndarray
PIL    PIL.JpegImagePlugin.JpegImageFile
keras.preprocessing.image PIL.JpegImagePlugin.JpegImageFile
Skimage.io   np.ndarray
matplotlib.pyplot  np.ndarray
matplotlib.image  np.ndarray
'''
 
imagePath="E:/DataSet/test1/trainSet/bus/300.jpg" 
 
'''
方式一:使用OpenCV
'''
img1=cv2.imread(imagePath)
print("img1:",img1.shape)
print("img1:",type(img1))
print("-"*10)
 
'''
方式二:使用PIL
'''
img2=Image.open(imagePath)
print("img2:",img2)
print("img2:",type(img2))
#转换成np.ndarray格式
img2=np.array(img2)
print("img2:",img2.shape)
print("img2:",type(img2))
print("-"*10) 
 
'''
方式三:使用keras.preprocessing.image
'''
img3=load_img(imagePath)
print("img3:",img3)
print("img3:",type(img3))
#转换成np.ndarray格式,使用np.array(),或者使用keras里的img_to_array()
#使用np.array()
#img3=np.array(img2)
#使用keras里的img_to_array()
img3=img_to_array(img3)
print("img3:",img3.shape)
print("img3:",type(img3))
print("-"*10) 
 
'''
方式四:使用Skimage.io
'''
img4=io.imread(imagePath)
print("img4:",img4.shape)
print("img4:",type(img4))
print("-"*10) 
 
'''
方式五:使用matplotlib.pyplot
'''
img5=plt.imread(imagePath)
print("img5:",img5.shape)
print("img5:",type(img5))
print("-"*10) 
 
'''
方式六:使用matplotlib.image
'''
img6=mpig.imread(imagePath)
print("img6:",img6.shape)
print("img6:",type(img6))
print("-"*10)

运行结果:

Using TensorFlow backend.
img1: (256, 384, 3)
img1: <class 'numpy.ndarray'>
----------
img2: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x256 at 0x249608A8C50>
img2: <class 'PIL.JpegImagePlugin.JpegImageFile'>
img2: (256, 384, 3)
img2: <class 'numpy.ndarray'>
----------
img3: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=384x256 at 0x2496B5A23C8>
img3: <class 'PIL.JpegImagePlugin.JpegImageFile'>
img3: (256, 384, 3)
img3: <class 'numpy.ndarray'>
----------
img4: (256, 384, 3)
img4: <class 'numpy.ndarray'>
----------
img5: (256, 384, 3)
img5: <class 'numpy.ndarray'>
----------
img6: (256, 384, 3)
img6: <class 'numpy.ndarray'>
----------

Python OpenCV中的numpy与图像类型转换操作

以上这篇Python OpenCV中的numpy与图像类型转换操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python数据结构之二叉树的遍历实例
Apr 29 Python
Python生成pdf文件的方法
Aug 04 Python
使用Python设计一个代码统计工具
Apr 04 Python
python语言基本语句用法总结
Jun 11 Python
python Django的web开发实例(入门)
Jul 31 Python
python elasticsearch从创建索引到写入数据的全过程
Aug 04 Python
Python for循环及基础用法详解
Nov 08 Python
python隐藏类中属性的3种实现方法
Dec 19 Python
Python django框架开发发布会签到系统(web开发)
Feb 12 Python
python将音频进行变速的操作方法
Apr 08 Python
tensorflow安装成功import tensorflow 出现问题
Apr 16 Python
python解包概念及实例
Feb 17 Python
使用python操作lmdb对数据读取的实例
Dec 11 #Python
PyTorch 中的傅里叶卷积实现示例
Dec 11 #Python
python中append函数用法讲解
Dec 11 #Python
python实现图像随机裁剪的示例代码
Dec 10 #Python
python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)
Dec 10 #Python
python 实现的IP 存活扫描脚本
Dec 10 #Python
class类在python中获取金融数据的实例方法
Dec 10 #Python
You might like
php 无限级分类学习参考之对ecshop无限级分类的解析 带详细注释
2010/03/23 PHP
php设计模式 DAO(数据访问对象模式)
2011/06/26 PHP
php 删除目录下N分钟前创建的所有文件的实现代码
2013/08/10 PHP
一漂亮的PHP图片验证码实例
2014/03/21 PHP
用js实现手把手教你月入万刀(转贴)
2007/11/07 Javascript
浏览器无法运行JAVA脚本的解决方法
2008/01/09 Javascript
写了10年的Javascript也未必全了解的连续赋值运算
2011/03/25 Javascript
Javascript 学习笔记之 对象篇(二) : 原型对象
2014/06/24 Javascript
jQuery实现自定义下拉列表
2015/01/05 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
jquery判断checkbox是否选中及改变checkbox状态的实现方法
2016/05/26 Javascript
JavaScript随机生成颜色的方法
2016/10/15 Javascript
vue.js-div滚动条隐藏但有滚动效果的实现方法
2018/03/03 Javascript
实例详解vue.js浅度监听和深度监听及watch用法
2018/08/16 Javascript
vue+axios 前端实现的常用拦截的代码示例
2018/08/23 Javascript
如何在Vue中使用CleaveJS格式化你的输入内容
2018/12/14 Javascript
详解bootstrap-fileinput文件上传控件的亲身实践
2019/03/21 Javascript
微信小程序实现多选框功能的实例代码
2020/06/24 Javascript
使用Python的Twisted框架实现一个简单的服务器
2015/04/16 Python
python实现用于测试网站访问速率的方法
2015/05/26 Python
Python 实现异步调用函数的示例讲解
2018/10/14 Python
详解Python做一个名片管理系统
2019/03/14 Python
在Python中预先初始化列表内容和长度的实现
2019/11/28 Python
TensorFlow设置日志级别的几种方式小结
2020/02/04 Python
基于梯度爆炸的解决方法:clip gradient
2020/02/04 Python
如何在Windows中安装多个python解释器
2020/06/16 Python
python程序实现BTC(比特币)挖矿的完整代码
2021/01/20 Python
如何开发一款堪比APP的微信小程序(腾讯内部团队分享)
2016/12/22 HTML / CSS
法国包包和行李箱销售网站:Bagage24.fr
2020/03/24 全球购物
咨询公司各岗位职责
2013/12/02 职场文书
婚纱摄影师求职信范文
2014/04/17 职场文书
机械机修工岗位职责
2014/08/03 职场文书
基层党员对照检查材料
2014/08/25 职场文书
2016年优秀团支部事迹材料
2016/02/26 职场文书
Vue通过懒加载提升页面响应速度
2021/05/10 Vue.js
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL