Python OpenCV中的numpy与图像类型转换操作
作者:泥石流中的一股清流 时间:2023-11-17 09:24:20
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'>
----------
来源:https://blog.csdn.net/qq_31261509/article/details/94383575
标签:Python,OpenCV,numpy,图像转换
0
投稿
猜你喜欢
跟老齐学Python之编写类之一创建实例
2021-05-06 21:16:56
Python+Pillow+Pytesseract实现验证码识别
2023-07-19 14:50:44
Python selenium get_cookies获取cookie不全的解决方案
2021-09-17 07:39:52
总结python中pass的作用
2021-02-15 18:34:45
关于Python函数的定义和参数
2021-05-08 20:37:28
在线HTML编辑器原理(eweb原理)
2009-01-08 12:25:00
Python使用描述符实现属性类型检查的案例解析
2022-07-30 00:39:15
Python matplotlib画图时图例说明(legend)放到图像外侧详解
2021-03-05 13:42:45
基于Python log 的正确打开方式
2021-05-29 21:42:59
Python Flask 转换器的使用详解
2023-06-30 15:42:09
Python设计模式之外观模式实例详解
2022-01-18 16:39:37
巧妙的自关联运用
2012-10-07 10:55:58
python3利用Dlib19.7实现人脸68个特征点标定
2021-05-05 19:13:06
MySQL数据库备份的基础知识大全
2009-12-20 18:14:00
python产生模拟数据faker库的使用详解
2022-05-25 22:43:35
Python语言规范之Pylint的详细用法
2022-11-26 03:12:25
python matplotlib实现条形图的填充效果
2022-03-12 07:55:20
JavaScript 数组的 uniq 方法
2007-12-07 18:28:00
Python实现AI自动玩俄罗斯方块游戏
2021-11-16 23:11:41
wxPython色环电阻计算器
2023-12-21 20:08:54