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学习小技巧之列表项的排序
May 20 Python
Python实现按学生年龄排序的实际问题详解
Aug 29 Python
人生苦短我用python python如何快速入门?
Mar 12 Python
shell命令行,一键创建 python 模板文件脚本方法
Mar 20 Python
python可视化实现代码
Jan 15 Python
Python简单处理坐标排序问题示例
Jul 11 Python
elasticsearch python 查询的两种方法
Aug 04 Python
对python while循环和双重循环的实例详解
Aug 23 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
May 19 Python
如何用PyPy让你的Python代码运行得更快
Dec 02 Python
python基于pexpect库自动获取日志信息
Feb 01 Python
pytorch 6 batch_train 批训练操作
May 28 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
重置版游戏视频
2020/04/09 魔兽争霸
php安全配置 如何配置使其更安全
2011/12/16 PHP
一个严格的PHP Session会话超时时间设置方法
2014/06/10 PHP
PHP扩展Swoole实现实时异步任务队列示例
2019/04/13 PHP
JScript中的undefined和&quot;undefined&quot;的区别
2007/03/08 Javascript
详谈nodejs异步编程
2014/12/04 NodeJs
AngularJS  双向数据绑定详解简单实例
2016/10/20 Javascript
bootstrap vue.js实现tab效果
2017/02/07 Javascript
Angular directive递归实现目录树结构代码实例
2017/05/05 Javascript
nodejs基于WS模块实现WebSocket聊天功能的方法
2018/01/12 NodeJs
Webpack中publicPath路径问题详解
2018/05/03 Javascript
使用vue-router完成简单导航功能【推荐】
2018/06/28 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
[01:19:33]DOTA2-DPC中国联赛 正赛 iG vs VG BO3 第一场 2月2日
2021/03/11 DOTA
使用Python编写一个模仿CPU工作的程序
2015/04/16 Python
python使用psutil模块获取系统状态
2016/08/27 Python
基于Django的ModelForm组件(详解)
2017/12/07 Python
Python实现的远程登录windows系统功能示例
2018/06/21 Python
Python OpenCV读取png图像转成jpg图像存储的方法
2018/10/28 Python
python实现微信每日一句自动发送给喜欢的人
2019/04/29 Python
python 如何用urllib与服务端交互(发送和接收数据)
2021/03/04 Python
伦敦高级内衣品牌:Agent Provocateur(大内密探)
2016/08/23 全球购物
澳大利亚拥有最佳跳伞降落点和最好服务的跳伞项目运营商:Skydive Australia
2018/03/05 全球购物
英国派对礼服和连衣裙购物网站:TFNC London
2018/07/07 全球购物
美国最大的电子宠物训练产品制造商:PetSafe
2018/10/12 全球购物
javascript实现用户必须勾选协议实例讲解
2021/03/24 Javascript
自我鉴定书范文
2013/10/02 职场文书
应届毕业生自我评价分享
2013/12/15 职场文书
小学生开学感言
2014/02/28 职场文书
班级寄语大全
2014/04/10 职场文书
机关党建工作汇报材料
2014/08/20 职场文书
四查四看整改措施
2014/09/19 职场文书
教师求职信怎么写
2015/03/20 职场文书
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫
Python&Matlab实现灰狼优化算法的示例代码
2022/03/21 Python
Java由浅入深通关抽象类与接口(下篇)
2022/04/26 Java/Android