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 相关文章推荐
Django imgareaselect手动剪切头像实现方法
May 26 Python
Python实现快速多线程ping的方法
Jul 15 Python
对Python进行数据分析_关于Package的安装问题
May 22 Python
浅谈Python实现Apriori算法介绍
Dec 20 Python
Python实现的文本对比报告生成工具示例
May 22 Python
Python正则匹配判断手机号是否合法的方法
Dec 09 Python
在Pycharm中使用GitHub的方法步骤
Jun 13 Python
Python将string转换到float的实例方法
Jul 29 Python
解决Mac下使用python的坑
Aug 13 Python
python os.path.isfile 的使用误区详解
Nov 29 Python
Python控制台实现交互式环境执行
Jun 09 Python
如何用python处理excel表格
Jun 09 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调用Webservice实例代码
2011/07/29 PHP
mac下安装nginx和php
2013/11/04 PHP
PHP导入导出Excel代码
2015/07/07 PHP
学习php设计模式 php实现观察者模式(Observer)
2015/12/09 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
PHP入门教程之自定义函数用法详解(创建,调用,变量,参数,返回值等)
2016/09/11 PHP
php opendir()列出目录下所有文件的实例代码
2016/10/02 PHP
PHP实现留言板功能的详细代码
2017/03/25 PHP
javascript里模拟sleep(两种实现方式)
2013/01/25 Javascript
jQuery基于函数重载实现自定义Alert函数样式的方法
2016/07/27 Javascript
jquery hover 不停闪动问题的解决方法(亦为stop()的使用)
2017/02/10 Javascript
使用html+js+css 实现页面轮播图效果(实例讲解)
2017/09/21 Javascript
微信小程序实现鼠标拖动效果示例
2017/12/01 Javascript
angular4笔记系列之内置指令小结
2018/11/09 Javascript
JavaScript静态作用域和动态作用域实例详解
2019/06/17 Javascript
vue集成chart.js的实现方法
2019/08/20 Javascript
echarts.js 动态生成多个图表 使用vue封装组件操作
2020/07/19 Javascript
[00:30]塑造者的传承礼包-戴泽“暗影之焰”套装展示视频
2014/04/04 DOTA
python检测lvs real server状态
2014/01/22 Python
python之import机制详解
2014/07/03 Python
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
python Selenium爬取内容并存储至MySQL数据库的实现代码
2017/03/16 Python
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
Python pandas实现excel工作表合并功能详解
2019/08/29 Python
Python 实现加密过的PDF文件转WORD格式
2020/02/04 Python
使用wxpy实现自动发送微信消息功能
2020/02/28 Python
Django模板之基本的 for 循环 和 List内容的显示方式
2020/03/31 Python
魔幻般冒泡背景的CSS3按钮动画
2016/02/27 HTML / CSS
夏洛特和乔治婴儿和儿童时装精品店:Charlotte and George
2018/06/06 全球购物
日本最大美瞳直送网:Morecontact(中文)
2019/04/03 全球购物
可以在一个PHP文件里面include另外一个PHP文件两次吗
2015/05/22 面试题
女大学生毕业找工作的自我评价
2013/10/03 职场文书
大学毕业生文采飞扬的自我鉴定
2013/12/03 职场文书
《分一分》教学反思
2014/04/13 职场文书
会计专业应届生自荐信
2014/06/28 职场文书
典型事迹材料范文
2014/12/29 职场文书