Opencv-Python學習筆記一之 imread, imshow, imwrite

opencv computer vision with python

在閑暇時間里找到這本關于opencv的書,邊看邊學,在這也是記錄下在這學習過程中的一些筆記,方便以后真正用到時查閱。

教程可查閱:opencv-python tutorial

opencv-python tutorial.png

windows環境下安裝opencv

1、windows系統是win7+64位, python版本號是3.5,因此需要找到與其相對應的opencv安裝版本

可以在這里找到各種版本下的安裝文件: opencv_python-3.1.0-cp35-cp35m-win_amd64.whl

2、下載好opencv的安裝文件后,通過pip install opencv_python-3.1.0-cp35-cp35m-win_amd64.whl進行安裝

install opencv

3、等第二步安裝完成后,查看是否安裝成功

opencv-version

通過查看版本號可知opencv-python已安裝成功。

opencv 簡單入門

1、讀取圖片 imread(path)

Use the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given.
Second argument is a flag which specifies the way image should be read.
cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag.
cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.

OpenCV目前支持讀取bmp、jpg、png、tiff等常用格式。

通過上面的英文我們知道opencv提供了一個imread方法讓我們去讀取圖片,該方法有兩個參數:第一個參數是我們所要讀取圖片的路徑, 第二個參數是圖片讀取的方式,如IMREAD_COLOR 表示加載彩色圖片(默認),IMREAD_GRAYSCALE以黑白方式加載圖片,IMREAD_UNCHANGED原圖加載含alpha channel信息。其中第二個參數的這三個值也可用1,0,-1來表示,方便我們在編程時的輸入。

阿爾法通道(Alpha Channel)是指一張圖片的透明和半透明度.

import cv2

#采取默認方式讀取圖片,即cv2.imread(path, 1)
img = cv2.imreda('img800.jpg')

#黑白方式讀取圖片
img1 = cv2.imread('img800', 0)

imread-imshow
2、圖片顯示 imshow()

opencv提供了cv2.imshow()來顯示圖片,該方法有兩個參數,第一個為窗口名,第二個為所要顯示的圖片。

Use the function cv2.imshow() to display an image in a window. The window automatically fits to the image size.
First argument is a window name which is a string. second argument is our image. You can create as many windows as you wish, but with different window names.

直接上代碼:
第一種方法:

import cv2

img1 = cv2.imread('img800.jpg', 0)
#取名圖片窗口名為gray,顯示圖片是img1
cv2.imshow('gray', img1)
#添cv2.waitKey(0),在IDLE中執行窗口直接無響應。在命令行中執行的話,則是一閃而過
cv2.waitKey(0)
#釋放窗口,簡單暴力釋放掉所有的窗口
cv2.destroyAllWindows()

其中cv2.waitKey()cv2.destroyAllWindows()方法在上面程序中有解釋,官方解釋如下:

cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes like, if key a is pressed etc which we will discuss below.
cv2.destroyAllWindows() simply destroys all the windows we created. If you want to destroy any specific window, use the function cv2.destroyWindow() where you pass the exact window name as the argument.

第二種方法:
先通過cv2.namedWindow()對圖片窗口先進行命名并進行其他設置,如可改變窗口大小。
代碼如下:

import cv2 

img2 = cv2.imread('img800.jpg')
#通過設置第二個參數使得圖片窗口可調節大小,默認是不可調的(cv2.WINDOW_AUTOSIZE)
cv2.namedWindow('color', cv2.WINDOW_NORMAL)
cv2.imshow('color',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

There is a special case where you can already create a window and load image to it later. In that case, you can specify whether window is resizable or not. It is done with the functioncv2.namedWindow(). By default, the flag is cv2.WINDOW_AUTOSIZE. But if you specify flag to be cv2.WINDOW_NORMAL, you can resize window. It will be helpful when image is too large in dimension and adding track bar to windows.

cv2.WINDOW_AUTOSIZE:根據原圖大小進行展示
cv2.WINDOW_NORMAL:圖片窗口可調節大小

namedWindow()
3、保存圖片 cv2.imwrite()

opencv提供了cv2.imwrite()方法保存圖片

Use the function cv2.imwrite() to save an image.
First argument is the file name, second argument is the image you want to save.

#保存圖片到當前工作目錄
cv2.imwrite('saveimg.png', img1)

imwrite()的第三個參數設置


##對于JPEG,其表示的是圖像的質量,用0-100的整數表示,默認為95。 
##注意,cv2.IMWRITE_JPEG_QUALITY類型為Long,必須轉換成int。
cv2.imwrite('saveimg11.jpg', img1, [int(cv2.IMWRITE_JPEG_QUALITY), 5])

##對于png圖片,第三個參數表示的是壓縮級別。
##cv2.IMWRITE_PNG_COMPRESSION,從0到9,壓縮級別越高,圖像尺寸越小。默認級別為3
cv2.imwrite('saveimg11.png', img1, [int(cv2.IMWRITE_JPEG_QUALITY), 5])
save image

例子1:根據用戶在鍵盤所敲下的不同按鍵對圖片進行處理,如ESC退出,按下‘s’時保存圖片并退出。

import numpy as np
import cv2

img = cv2.imread('img800.jpg',0)
cv2.imshow('image',img)
#本臺機器系統是64位 k = cv2.waitKey(0) & 0xF
#若是32位系統請用 k = cv2.waitKey(0)
k = cv2.waitKey(0) & 0xFF
if k == 27:         # 按下ESC退出
    cv2.destroyAllWindows()
elif k == ord('s'): # 按下's'保存圖片并退出
    cv2.imwrite('savegray.png',img)
    cv2.destroyAllWindows()

例子2:通過opencv來讀取圖片,用matplotlib來顯示圖片,充分利用其plot的諸多特性。

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('img800.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) 
plt.show()

因為opencv與matplotlib對圖片的讀取值順序等的差異,在二者互相交替使用時需要注意一些細節,更多問題討論大家可以參考 some problem when you try to load color image in OpenCV and display it in Matplotlib

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.

show Image by matplotlib.png

【寫在最后】opencv-python學習第一篇就先記錄下這些,后面會根據學習情況及時更新筆記。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容