OpenCv example with python test

實作系統Ubuntu 14.04 以及 老師整合的檔案


範例網址1.

使用到的圖片

程式碼

import numpy as np
import cv2
img = cv2.imread('messi.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',img)
    cv2.destroyAllWindows()

執行結果



範例網址2.

程式碼

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()
    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

When everything done, release the capture

cap.release() cv2.destroyAllWindows()

執行結果



範例網址3.(同上)

程式碼

import numpy as np
import cv2
cap = cv2.VideoCapture(0)

Define the codec and create VideoWriter object

fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret==True: frame = cv2.flip(frame,1) # write the flipped frame out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break

Release everything if job is finished

cap.release() out.release() cv2.destroyAllWindows()

執行結果

得到檔案名稱為output的avi影片檔


Comments

comments powered by Disqus