2016年10月20日 星期四

Python_Note15

matplotlib

官網
http://matplotlib.org/index.html

Pyplot tutorial
http://matplotlib.org/users/pyplot_tutorial.html

Matplotlib 教程 (中文)
http://liam0205.me/2014/09/11/matplotlib-tutorial-zh-cn/

Matplotlib Examples
http://matplotlib.org/examples/

API content
http://matplotlib.org/api/index.html

matplotlib.pyplot.plot(*args**kwargs)

plot(x, y)        # plot x and y using default line style and color
plot(x, y, 'bo')  # plot x and y using blue circle markers
plot(y)           # plot y using x as index array 0..N-1
plot(y, 'r+')     # ditto, but with red plusses

a.plot(x1, y1, 'g^', x2, y2, 'g-')

matplotlib.colors

A module for converting numbers or color arguments to RGB or RGBA

class matplotlib.colors.ListedColormap(colorsname='from_list'N=None)

http://matplotlib.org/api/colors_api.html?highlight=colors#matplotlib.colors.ListedColormap


matplotlib,imshow

Display an image on the axes.

imshow(Xcmap=Nonenorm=Noneaspect=Noneinterpolation=Nonealpha=Nonevmin=Nonevmax=Noneorigin=None,extent=Noneshape=Nonefilternorm=1filterrad=4.0imlim=Noneresample=Noneurl=None**kwargs)

Note:OpenCV讀圖是BRG模式,而OpenCV則是RGB,所以彩色圖在這兩者之間直接轉換會有問題。
Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.

範例:
原圖


彩色OpenCV讀,matplotlib寫
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg',0)
plt.imshow(img, interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()

Out:

灰階OpenCV讀,matplotlib寫
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('image.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()

Out:

利用cv2.split() 來把b、g、r拆解,在以r、g、b格式合併即可解決:
import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('image.jpg')
print(img)
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121);plt.imshow(img)  # expects distorted color
plt.subplot(122);plt.imshow(img2)  # expect true color
plt.show()

cv2.imshow('bgr image',img)  # expects true color
cv2.imshow('rgb image',img2)  # expects distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()

沒有留言:

張貼留言