2016年7月31日 星期日

Python_Note19

Python_OpenCV

參考資料:

OpenCV3.1版官方手冊 (完全沒有Python的範例 2016/08/01)
http://docs.opencv.org/3.1.0/#gsc.tab=0

OpenCV3.0-beta官方手冊 (這個版本比較完整,有針對Python的指令及範例,建議新手可以查這個,有問題再去看最新版)
http://docs.opencv.org/3.0-beta/index.html

OpenCV-Python Tutorials
(這是官方出的教學指南,下面兩個網址內容都一樣,也有PDF檔可下載)
http://docs.opencv.org/3.2.0/d6/d00/tutorial_py_root.html  (OpenCV 3.2 for Python, 2017/3/31更新)
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html
https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

Install OpenCV-Python in Windows

參考:Python_install OpenCV-3.1、或官方手冊 Install OpenCV-Python in Windows

cv2.VideoCapture() 

在進行跟讀寫影像檔有關的指令時,會沒有作用。此外,如果是檔案讀寫不成功時,此模組也不會出現error,所以如果程式沒有照預想的情況執行卻有沒有error,應該是ffmpeg沒設環境變數的關係,譬如在  Playing Video from file 這個示範例中,
import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
  • 執行後沒反應就是編碼這邊有問題。手冊裡寫:Note: Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer. 
  • 因為OpenCV在讀寫影像檔時會執行ffmpeg來編譯,但是若環境變數沒設定,則會無法編譯而導致後續無法進行影像處理的問題。
  • 解決方式:將ffmpeg的檔案位置增加到環境變數即可。注意,這裡要看使用者是直接用opecvbuild的環境,還是自己build的環境,兩邊都會有ffmpeg檔,要選對的檔案的資料夾位置來加入環境變數。譬如我的是 D:\library\opencv3.1-pythonbuild\bin\Release;,而不是OpenCV的所在位置 D:\library\opencv3.1\build\bin。或者將opencv_ffmpeg310_64.dll 複製到python資料夾下 ( 我的是C:\Anaconda3 )也可以。

Drawing Functions in OpenCV

OpenCV基本幾何畫圖函數有:
  • cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img
  • cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → img
  • cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img
  • cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → img
  • cv2.ellipse(img, box, color[, thickness[, lineType]]) → img
  • cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img 
  • cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → Nonev2.putText()

上述函數皆有以下引數 (argument):
  • img : The image where you want to draw the shapes
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If -1 is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves. 

範例:Logo of OpenCV
import numpy as np
import cv2
#create a black image
img = np.zeros((800,557,3),np.uint8)

#convert the black image into white image
img = img + 255

img = cv2.ellipse(img,(281,133),(128,128),0,120,420,(0,0,255),-1)
img = cv2.circle(img,(281,133), 42, (255,255,255), -1)

img = cv2.ellipse(img,(138,384),(128,128),0,0,300,(0,255,0),-1)
img = cv2.circle(img,(138,384), 42, (255,255,255), -1)

img = cv2.ellipse(img,(419,384),(128,128),0,-60,240,(255,0,0),-1)
img = cv2.circle(img,(419,384), 42, (255,255,255), -1)


font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(35,650), font, 4,(0,0,0),15,cv2.LINE_AA)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Out:


Events in OpenCV

>>> import cv2
>>> events = [i for i in dir(cv2) if 'EVENT' in i]
>>> print events
['EVENT_FLAG_ALTKEY', 'EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON', 'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON', 'EVENT_FLAG_SHIFTKEY', 'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN', 'EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK', 'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP', 'EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE', 'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK', 'EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']






沒有留言:

張貼留言