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

OpenCV-Python?對圖像的基本操作代碼

 更新時間:2021年11月26日 10:38:36   作者:WSKH0929  
這篇文章主要介紹了OpenCV-Python?對圖像的基本操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 設(shè)置兼容中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:

D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll

D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll

  warnings.warn("loaded more than 1 DLL from .libs:\n%s" %

cv.__version__

'4.5.1'
rain = cv.imread('img/rain.jpg')[:500,:500,:]

view = cv.imread('img/view.jpg')

print(rain.shape)

print(view.shape)

(500, 500, 3)

(500, 500, 3)
plt.figure()

plt.subplot(1,2,1)

m1 = plt.imshow(rain[:,:,::-1])

plt.title('rain.jpg')

plt.subplot(1,2,2)

m2 = plt.imshow(view[:,:,::-1])

plt.title('view.jpg')
Text(0.5, 1.0, 'view.jpg')

在這里插入圖片描述

1.圖像的加法?

# cv加法
add_img = cv.add(rain,view)
plt.imshow(add_img[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2fed160>

在這里插入圖片描述

# numpy 加法
add_img2 = rain + view
plt.imshow(add_img2[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2d4d4c0>

在這里插入圖片描述

2.圖像的混合

# 圖像的混合(按照權(quán)重)
img3 = cv.addWeighted(rain,0.2,view,0.8,0)
plt.imshow(img3[:,:,::-1])
<matplotlib.image.AxesImage at 0x1fdc2f01a00>

在這里插入圖片描述

3.圖像的縮放

# 獲取絕對尺寸 行,列
row,col = rain.shape[:2]

print("row:",row,",col:",col)

row: 500 ,col: 500

# 圖像放大
res = cv.resize(rain,(2*row,2*col))
plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("放大前")
plt.subplot(1,2,2)
m2 = plt.imshow(res[:,:,::-1])
plt.title("放大后")

Text(0.5, 1.0, '放大后')

在這里插入圖片描述

# 使用相對坐標 進行圖像縮小
res2 = cv.resize(rain,None,fx=0.5,fy=0.5)
plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("縮小前")
plt.subplot(1,2,2)
m2 = plt.imshow(res2[:,:,::-1])
plt.title("縮小后")

Text(0.5, 1.0, '縮小后')

?在這里插入圖片描述

4.圖像的平移

# 創(chuàng)建平移矩陣  x方向上平移100,y方向上平移50
M = np.float32([[1,0,100],[0,1,50]])
M

array([[  1.,   0., 100.],
       [  0.,   1.,  50.]], dtype=float32)

# cv.warpAffine(要平移的圖形,平移矩陣,背景畫布大小)
res = cv.warpAffine(rain,M,rain.shape[:2])

plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("平移前")
plt.subplot(1,2,2)
m2 = plt.imshow(res[:,:,::-1])
plt.title("平移后")

Text(0.5, 1.0, '平移后')

在這里插入圖片描述

5.圖像的旋轉(zhuǎn)

row,col = rain.shape[:2]

# 生成旋轉(zhuǎn)矩陣 getRotationMatrix2D(旋轉(zhuǎn)中心坐標,旋轉(zhuǎn)角度,縮放比例)
M = cv.getRotationMatrix2D((row/2,col/2),45,1)

# cv.warpAffine(要旋轉(zhuǎn)的圖形,旋轉(zhuǎn)矩陣,背景畫布大小)
res = cv.warpAffine(rain,M,(col,row))

plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("旋轉(zhuǎn)前")
plt.subplot(1,2,2)
m2 = plt.imshow(res[:,:,::-1])
plt.title("旋轉(zhuǎn)后")

Text(0.5, 1.0, '旋轉(zhuǎn)后')

在這里插入圖片描述

6.圖像的仿射變換

pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[100,100],[200,50],[100,250]])

M = cv.getAffineTransform(pts1,pts2)
M

array([[ 0.66666667,  0.        , 66.66666667],
       [-0.33333333,  1.        , 66.66666667]])

res = cv.warpAffine(rain,M,rain.shape[:2])

plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("仿射變換前")
plt.subplot(1,2,2)
m2 = plt.imshow(res[:,:,::-1])
plt.title("仿射變換后")

Text(0.5, 1.0, '仿射變換后')

在這里插入圖片描述

7.圖像的透射變換

pst1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
pst2 = np.float32([[100,145],[300,100],[80,290],[310,300]])

M = cv.getPerspectiveTransform(pst1,pst2)
M

array([[ 3.98327670e-01, -2.09876559e-02,  7.49460064e+01],
       [-1.92233080e-01,  4.29335771e-01,  1.21896057e+02],
       [-7.18774228e-04, -1.33393850e-05,  1.00000000e+00]])

res = cv.warpPerspective(rain,M,rain.shape[:2])

plt.figure()
plt.subplot(1,2,1)
m1 = plt.imshow(rain[:,:,::-1])
plt.title("透射變換前")
plt.subplot(1,2,2)
m2 = plt.imshow(res[:,:,::-1])
plt.title("透射變換后")

Text(0.5, 1.0, '透射變換后')

在這里插入圖片描述

8.圖像金字塔

# 上采樣

img_up = cv.pyrUp(rain)
# 下采樣

img_down = cv.pyrDown(rain)

plt.figure()

plt.subplot(1,3,1)

m1 = plt.imshow(rain[:,:,::-1])

plt.title("原圖")

plt.subplot(1,3,2)

m2 = plt.imshow(img_up[:,:,::-1])

plt.title("上采樣")

plt.subplot(1,3,3)

m2 = plt.imshow(img_down[:,:,::-1])

plt.title("下采樣")
Text(0.5, 1.0, '下采樣')

?在這里插入圖片描述

到此這篇關(guān)于OpenCV-Python 對圖像的基本操作的文章就介紹到這了,更多相關(guān)OpenCV-Python圖像操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何用python給朋友做生日快樂祝福

    如何用python給朋友做生日快樂祝福

    生活中除了給男神女神表白,還會遇到朋友過生日,所以小編今天就給大家?guī)砹艘粋€送給朋友的生日祝福程序,這篇文章主要給大家介紹了關(guān)于如何用python給朋友做生日快樂祝福的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • python 實現(xiàn)識別圖片上的數(shù)字

    python 實現(xiàn)識別圖片上的數(shù)字

    這篇文章主要介紹了python 識別圖片上的數(shù)字,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • python 實現(xiàn)查詢Neo4j多節(jié)點的多層關(guān)系

    python 實現(xiàn)查詢Neo4j多節(jié)點的多層關(guān)系

    今天小編就為大家分享一篇python 實現(xiàn)查詢Neo4j多節(jié)點的多層關(guān)系,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 如何創(chuàng)建第一個Pygame程序

    如何創(chuàng)建第一個Pygame程序

    本文主要介紹了如何創(chuàng)建第一個Pygame程序,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Python中類型檢查的詳細介紹

    Python中類型檢查的詳細介紹

    Python是一種非常動態(tài)的語言,函數(shù)定義中完全沒有類型約束。下面這篇文章主要給大家詳細介紹了Python中類型檢查的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • Python標準庫sys庫常用功能詳解

    Python標準庫sys庫常用功能詳解

    這篇文章主要介紹了Python標準庫sys庫常用功能詳解,sys是Python提供的程序與解釋器交互的標準庫,文章圍繞主題展開相關(guān)介紹,需要的朋友可以參考一下
    2022-07-07
  • 深入解析Python的Tornado框架中內(nèi)置的模板引擎

    深入解析Python的Tornado框架中內(nèi)置的模板引擎

    模板引擎是Web開發(fā)框架中負責(zé)前端展示的關(guān)鍵,這里我們就來以實例深入解析Python的Tornado框架中內(nèi)置的模板引擎,來學(xué)習(xí)如何編寫Tonardo的模板.
    2016-07-07
  • Python中g(shù)event模塊協(xié)程使用

    Python中g(shù)event模塊協(xié)程使用

    協(xié)程是一種用戶態(tài)的輕量級線程,本文主要介紹了Python中g(shù)event模塊協(xié)程使用,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)

    Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)

    本文詳細講解了Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)的實現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • Python實現(xiàn)的讀取電腦硬件信息功能示例

    Python實現(xiàn)的讀取電腦硬件信息功能示例

    這篇文章主要介紹了Python實現(xiàn)的讀取電腦硬件信息功能,結(jié)合實例形式分析了Python基于wmi庫讀取電腦CPU、磁盤、網(wǎng)絡(luò)、進程等硬件信息相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05

最新評論