欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python OpenCV中的numpy與圖像類型轉換操作

 更新時間:2020年12月11日 09:31:57   作者:泥石流中的一股清流  
這篇文章主要介紹了Python OpenCV中的numpy與圖像類型轉換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

Python OpenCV存儲圖像使用的是Numpy存儲,所以可以將Numpy當做圖像類型操作,操作之前還需進行類型轉換,轉換到int8類型

import cv2
import numpy as np
# 使用numpy方式創(chuàng)建一個二維數組
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實現搜索Google Scholar論文信息的示例代碼

    Python實現搜索Google Scholar論文信息的示例代碼

    這篇文章主要為大家詳細介紹了如何利用Python實現搜索Google Scholar論文信息的功能,文中的示例代碼講解詳細,需要的可以參考一下
    2023-03-03
  • Python創(chuàng)建相同值數組/列表的兩種方法

    Python創(chuàng)建相同值數組/列表的兩種方法

    眾所周知數組是一種用來在計算機中存儲連續(xù)的相同類型數值的數據結構,這篇文章主要給大家介紹了關于Python創(chuàng)建相同值數組/列表的兩種方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • 簡單介紹Python下自己編寫web框架的一些要點

    簡單介紹Python下自己編寫web框架的一些要點

    這篇文章主要介紹了簡單介紹Python下自己編寫web框架的一些要點,示例代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • Python爬取YY評級分數并保存數據實現過程解析

    Python爬取YY評級分數并保存數據實現過程解析

    這篇文章主要介紹了Python爬取YY評級分數并保存數據實現過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • python實現web方式logview的方法

    python實現web方式logview的方法

    這篇文章主要介紹了python實現web方式logview的方法,涉及Python基于web模塊操作Linux命令的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • 使用Python Pandas處理億級數據的方法

    使用Python Pandas處理億級數據的方法

    這篇文章主要介紹了使用Python Pandas處理億級數據的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • Python解析JSON數據的示例代碼

    Python解析JSON數據的示例代碼

    JSON格式是網站和API使用的通用標準格式,現在主流的一些數據庫(如PostgreSQL)都支持JSON格式,在本文中,我們將介紹如何使用Python解析JSON數據,感興趣的小伙伴跟著小編一起來看看吧
    2024-09-09
  • django表單實現下拉框的示例講解

    django表單實現下拉框的示例講解

    今天小編就為大家分享一篇django表單實現下拉框的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 淺談JupyterNotebook導出pdf解決中文的問題

    淺談JupyterNotebook導出pdf解決中文的問題

    這篇文章主要介紹了淺談JupyterNotebook導出pdf解決中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 基于python+pandoc實現html批量轉word

    基于python+pandoc實現html批量轉word

    pandoc是一個強大的文檔格式轉換工具,支持豐富的格式轉換,并盡可能的保留原來的排版,號稱文檔格式轉換的瑞士軍刀,本文將給大家介紹一下使用python搭配pandoc實現html批量轉word,感興趣的朋友可以參考閱讀下
    2023-09-09

最新評論