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切换pip安装源的方法详解
Nov 18 Python
Python中optparser库用法实例详解
Jan 26 Python
python顺序的读取文件夹下名称有序的文件方法
Jul 11 Python
Matplotlib中文乱码的3种解决方案
Nov 15 Python
python tkinter canvas 显示图片的示例
Jun 13 Python
pycharm new project变成灰色的解决方法
Jun 27 Python
Pytorch抽取网络层的Feature Map(Vgg)实例
Aug 20 Python
Numpy的简单用法小结
Aug 28 Python
Python数组拼接np.concatenate实现过程
Apr 18 Python
python logging通过json文件配置的步骤
Apr 27 Python
使用python-Jenkins批量创建及修改jobs操作
May 12 Python
Python连接mysql数据库及简单增删改查操作示例代码
Aug 03 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
自动跳转中英文页面
2006/10/09 PHP
fleaphp下不确定的多条件查询的巧妙解决方法
2008/09/11 PHP
Yii2 GridView实现列表页直接修改数据的方法
2016/05/16 PHP
thinkPHP框架实现生成条形码的方法示例
2018/06/06 PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
2020/08/20 PHP
prototype1.4中文手册
2006/09/22 Javascript
改善你的jQuery的25个步骤 千倍级效率提升
2010/02/11 Javascript
AlertBox 弹出层信息提示框效果实现步骤
2010/10/11 Javascript
jQuery EasyUI API 中文文档 - ComboBox组合框
2011/10/07 Javascript
js中点击空白区域时文本框与隐藏层的显示与影藏问题
2013/08/26 Javascript
JS给超链接加确认对话框的方法
2015/02/24 Javascript
jQuery 全选 全部选 反选 实现代码
2016/08/17 Javascript
很酷的星级评分系统原生JS实现
2016/08/25 Javascript
JavaScript 最佳实践:帮你提升代码质量
2016/12/03 Javascript
layui表格checkbox选择全选样式及功能的实例
2018/03/07 Javascript
详解基于vue的服务端渲染框架NUXT
2018/06/20 Javascript
Puppeteer环境搭建的详细步骤
2018/09/21 Javascript
JavaScript使用表单元素验证表单的示例代码
2019/08/20 Javascript
[28:05]完美世界DOTA2联赛循环赛Inki vs DeMonsTer 第一场 10月30日
2020/10/31 DOTA
使用Python的Twisted框架编写非阻塞程序的代码示例
2016/05/25 Python
代码分析Python地图坐标转换
2018/02/08 Python
解决Python3 被PHP程序调用执行返回乱码的问题
2019/02/16 Python
python实现输出一个序列的所有子序列示例
2019/11/18 Python
在keras中获取某一层上的feature map实例
2020/01/24 Python
Python流程控制常用工具详解
2020/02/24 Python
Python中如何引入第三方模块
2020/05/27 Python
python获取天气接口给指定微信好友发天气预报
2020/12/28 Python
Html5之webcoekt播放JPEG图片流
2020/09/22 HTML / CSS
全球酒店比价网:HotelsCombined
2017/06/20 全球购物
营销总监岗位职责范本
2014/02/26 职场文书
2014年大学庆元旦迎新年活动方案
2014/03/09 职场文书
小学生学雷锋演讲稿
2014/04/25 职场文书
王兆力在市委党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
大学生逃课检讨书
2015/05/04 职场文书
2019消防宣传标语!
2019/07/10 职场文书
JavaScript实现两个数组的交集
2022/03/25 Javascript