opencv:matplotlib.pyplot模塊

 

1.subplot函數

模塊matplotlib.pyplot提供了函數matplotlib.pyplot.subplot()用來向當前窗口內添加一個子窗口對象。該函數的語法格式爲:

matplotlib.pyplot.subplot(nrows,ncols,index)

nrows爲行數。 ncols爲列數。 index爲窗口序號(序號是從1開始的)

import cv2
import matplotlib.pyplot as plt
img=cv2.imread("D:/dog.jpg",0)
equ=cv2.equalizeHist(img)
plt.figure("subplot")
plt.subplot(121),plt.hist(img.ravel(), 256)
plt.subplot(122),plt.hist(equ.ravel(), 256)

2.matplotlib.pyplot.imshow()

matplotlib.pyplot.imshow(X,cmap=None)

式中:X爲圖像信息,可以是各種形式的數值,cmap表示色彩空間,該值是可選項,默認值爲null,默認使用RGB(A)色彩空間。

import cv2
import matplotlib.pyplot as plt
img=cv2.imread("D:/dog.jpg")
imgRGB=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.figure("xianshi")
plt.subplot(121)
plt.imshow(img),plt.axis('off')
plt.subplot(122)
plt.imshow(imgRGB),plt.axis('off')

import cv2
import matplotlib.pyplot as plt
o=cv2.imread("d:/dog.jpg")
g=cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
plt.figure("huidu")
plt.subplot(221)
plt.imshow(o),plt.axis('off')
plt.subplot(222)
plt.imshow(o,cmap=plt.cm.gray),plt.axis('off')
plt.subplot(223)
plt.imshow(g),plt.axis('off')
plt.subplot(224)
plt.imshow(g,cmap=plt.cm.gray),plt.axis('off')

 

 

● 左上角是原始圖像。因爲沒有對讀取的圖像進行通道轉換,直接將其作爲彩色圖像顯示,所以顯示結果中通道順序是錯亂的,圖像沒有正常顯示。

● 右上角是我們嘗試將彩色圖像作爲原始圖像,將其色彩空間參數設置爲“cmap=plt.cm.gray”,讓彩色圖像以灰度圖像展示的結果,圖像顯示失敗。

● 左下角是使用默認色彩空間參數,顯示灰度圖像的結果。這種情況下,灰度圖像會採用默認的RGB模式顯示。可以看到,圖像沒有正常顯示。

● 右下角是將灰度圖像作爲原始圖像,並將其色彩空間參數設置爲“cmap=plt.cm.gray”而顯示的灰度圖像,圖像顯示正常。

 

import cv2
import matplotlib.pyplot as plt
o=cv2.imread('d:/dog.jpg')
g=cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
plt.figure('huidu')
plt.subplot(221)
plt.imshow(g,cmap=plt.cm.gray_r)
plt.subplot(222)
plt.imshow(g,cmap=plt.cm.gray)
plt.subplot(223)
plt.imshow(g,cmap='gray')
plt.subplot(224)
plt.imshow(g,cmap='gray_r')

色彩空間參數cmap的參數值“plt.cm.gray_r”及“gray_r”中的“r”是英文“reverse”的縮寫,表示逆轉的意思。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章