Python OpenCV中的numpy與圖像類型轉(zhuǎn)換操作
Python OpenCV存儲圖像使用的是Numpy存儲,所以可以將Numpy當(dāng)做圖像類型操作,操作之前還需進行類型轉(zhuǎn)換,轉(zhuǎn)換到int8類型
import cv2 import numpy as np # 使用numpy方式創(chuàng)建一個二維數(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)
補充知識: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)
運行結(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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)搜索Google Scholar論文信息的示例代碼
這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)搜索Google Scholar論文信息的功能,文中的示例代碼講解詳細,需要的可以參考一下2023-03-03Python創(chuàng)建相同值數(shù)組/列表的兩種方法
眾所周知數(shù)組是一種用來在計算機中存儲連續(xù)的相同類型數(shù)值的數(shù)據(jù)結(jié)構(gòu),這篇文章主要給大家介紹了關(guān)于Python創(chuàng)建相同值數(shù)組/列表的兩種方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12Python爬取YY評級分數(shù)并保存數(shù)據(jù)實現(xiàn)過程解析
這篇文章主要介紹了Python爬取YY評級分數(shù)并保存數(shù)據(jù)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06使用Python Pandas處理億級數(shù)據(jù)的方法
這篇文章主要介紹了使用Python Pandas處理億級數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06淺談JupyterNotebook導(dǎo)出pdf解決中文的問題
這篇文章主要介紹了淺談JupyterNotebook導(dǎo)出pdf解決中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04基于python+pandoc實現(xiàn)html批量轉(zhuǎn)word
pandoc是一個強大的文檔格式轉(zhuǎn)換工具,支持豐富的格式轉(zhuǎn)換,并盡可能的保留原來的排版,號稱文檔格式轉(zhuǎn)換的瑞士軍刀,本文將給大家介紹一下使用python搭配pandoc實現(xiàn)html批量轉(zhuǎn)word,感興趣的朋友可以參考閱讀下2023-09-09