Python OpenCV中的numpy與圖像類型轉(zhuǎn)換操作
Python OpenCV存儲(chǔ)圖像使用的是Numpy存儲(chǔ),所以可以將Numpy當(dāng)做圖像類型操作,操作之前還需進(jìn)行類型轉(zhuǎn)換,轉(zhuǎn)換到int8類型
import cv2
import numpy as np
# 使用numpy方式創(chuàng)建一個(gè)二維數(shù)組
img = np.ones((100,100))
# 轉(zhuǎn)換成int8類型
img = np.int8(img)
# 顏色空間轉(zhuǎn)換,單通道轉(zhuǎn)換成多通道, 可選可不選
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.imwrite("demo.jpg", img)
補(bǔ)充知識(shí):Python中讀取圖片并轉(zhuǎn)化為numpy.ndarray()數(shù)據(jù)的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))
#轉(zhuǎn)換成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))
#轉(zhuǎn)換成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)
運(yùn)行結(jié)果:
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與圖像類型轉(zhuǎn)換操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)搜索Google Scholar論文信息的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)搜索Google Scholar論文信息的功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-03-03
Python創(chuàng)建相同值數(shù)組/列表的兩種方法
眾所周知數(shù)組是一種用來在計(jì)算機(jī)中存儲(chǔ)連續(xù)的相同類型數(shù)值的數(shù)據(jù)結(jié)構(gòu),這篇文章主要給大家介紹了關(guān)于Python創(chuàng)建相同值數(shù)組/列表的兩種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
簡單介紹Python下自己編寫web框架的一些要點(diǎn)
這篇文章主要介紹了簡單介紹Python下自己編寫web框架的一些要點(diǎn),示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
Python爬取YY評(píng)級(jí)分?jǐn)?shù)并保存數(shù)據(jù)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Python爬取YY評(píng)級(jí)分?jǐn)?shù)并保存數(shù)據(jù)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
python實(shí)現(xiàn)web方式logview的方法
這篇文章主要介紹了python實(shí)現(xiàn)web方式logview的方法,涉及Python基于web模塊操作Linux命令的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
使用Python Pandas處理億級(jí)數(shù)據(jù)的方法
這篇文章主要介紹了使用Python Pandas處理億級(jí)數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
淺談JupyterNotebook導(dǎo)出pdf解決中文的問題
這篇文章主要介紹了淺談JupyterNotebook導(dǎo)出pdf解決中文的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
基于python+pandoc實(shí)現(xiàn)html批量轉(zhuǎn)word
pandoc是一個(gè)強(qiáng)大的文檔格式轉(zhuǎn)換工具,支持豐富的格式轉(zhuǎn)換,并盡可能的保留原來的排版,號(hào)稱文檔格式轉(zhuǎn)換的瑞士軍刀,本文將給大家介紹一下使用python搭配pandoc實(shí)現(xiàn)html批量轉(zhuǎn)word,感興趣的朋友可以參考閱讀下2023-09-09

