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安装cx_Oracle模块常见问题与解决方法
Feb 21 Python
python调用xlsxwriter创建xlsx的方法
May 03 Python
Python3中的列表生成式、生成器与迭代器实例详解
Jun 11 Python
Pandas读写CSV文件的方法示例
Mar 27 Python
Python实现决策树并且使用Graphviz可视化的例子
Aug 09 Python
softmax及python实现过程解析
Sep 30 Python
用pytorch的nn.Module构造简单全链接层实例
Jan 14 Python
python如何求数组连续最大和的示例代码
Feb 04 Python
使用python实现微信小程序自动签到功能
Apr 27 Python
Python中socket网络通信是干嘛的
May 27 Python
Python替换NumPy数组中大于某个值的所有元素实例
Jun 08 Python
python链表类中获取元素实例方法
Feb 23 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
Terran魔法科技
2020/03/14 星际争霸
php通过strpos查找字符串出现位置的方法
2015/03/17 PHP
详解Laravel5.6 Passport实现Api接口认证
2018/07/27 PHP
详解阿里云视频直播PHP-SDK接入教程
2020/07/09 PHP
一步一步制作jquery插件Tabs实现过程
2010/07/06 Javascript
用jquery和json从后台获得数据集的代码
2011/11/07 Javascript
js 完美图片新闻轮转效果,腾讯大粤网首页图片轮转改造而来
2011/11/21 Javascript
在新窗口打开超链接的方法小结
2013/04/14 Javascript
zTree插件之单选下拉菜单实例代码
2013/11/07 Javascript
jquery弹窗插件colorbox绑定动态生成元素的方法
2014/06/20 Javascript
整理Javascript基础入门学习笔记
2015/11/29 Javascript
JS折半插入排序算法实例
2015/12/02 Javascript
jquery判断复选框是否选中进行答题提示特效
2015/12/10 Javascript
谈谈我对JavaScript DOM事件的理解
2015/12/18 Javascript
基于HTML模板和JSON数据的JavaScript交互(移动端)
2016/04/06 Javascript
基于JavaScript实现活动倒计时效果
2017/04/20 Javascript
node中使用es5/6以及支持性与性能对比
2017/08/11 Javascript
vue2.x+webpack快速搭建前端项目框架详解
2017/11/30 Javascript
深入理解js 中async 函数的含义和用法
2018/05/13 Javascript
Bootstrap导航菜单点击后无法自动添加active的处理方法
2018/08/10 Javascript
nodejs 使用nodejs-websocket模块实现点对点实时通讯
2018/11/28 NodeJs
详解 微信小程序开发框架(MINA)
2019/05/17 Javascript
vue.js实现图书管理功能
2019/09/24 Javascript
Python定义二叉树及4种遍历方法实例详解
2018/07/05 Python
django 使用全局搜索功能的实例详解
2019/07/18 Python
pandas如何处理缺失值
2019/07/31 Python
Python Django 命名空间模式的实现
2019/08/09 Python
wxPython实现整点报时
2019/11/18 Python
pandas dataframe 中的explode函数用法详解
2020/05/18 Python
推荐10个CSS3 制作的创意下拉菜单效果
2014/02/11 HTML / CSS
HTML5混合开发二维码扫描以及调用本地摄像头
2017/12/27 HTML / CSS
EQVVS官网:设计师男装和女装
2018/10/24 全球购物
党员公开承诺事项
2014/03/25 职场文书
中共广东省委常委会党的群众路线教育实践活动整改方案
2014/09/23 职场文书
小学美术教学反思
2016/02/17 职场文书
十大经典日本动漫排行榜 海贼王第三,犬夜叉仅第八
2022/03/18 日漫