學習如何使用opencv。Chapter 1影像讀取 、Chapter 2 影像轉換。
Opencv
可用於影像處理、電腦視覺、影像辨識等。所以想說先去了解 Opencv 是要如何使用。目前是參考這個影片自己練習。
Chapter 1 影像讀取
如何將讀取到的影像、影片或是 Webcam 用 Opencv 呼叫視窗顯示
import cv2
#------part 1 show the lena image
'''
img = cv2.imread("lena.png") #read the lena.png
cv2.imshow("Output",img) #show lena.png in "Output" window
cv2.waitKey(0) #"0" is mean: hold previous command to user press anykey.
'''
#------part 2 show the mp4 video
'''
cap= cv2.VideoCapture("test_video.mp4")#read the test_video.mp4
while True:
success,img = cap.read()
cv2.imshow("Video",img)
if cv2.waitKey(3) & 0xFF ==ord('q'):
break
'''
#------part 3 show the webcam vidoe stream
cap = cv2.VideoCapture(0)#Catch camera0 video stream
cap.set(3,640) #width of the frames in the video stream
cap.set(4,480) #height of the frames in the video stream
cap.set(10,128) #brightness of the image (only for cameras)
while True:
success,img = cap.read()
cv2.imshow("Capture",img)
if cv2.waitKey(1) & 0xFF ==ord('q'):
break
Chapter 2 影像轉換
將影像色調轉換 ( 灰階 ) 、高斯模糊、 Canny (邊緣檢測)、 dilate (擴張)、 erode (侵蝕)。
可以將原本的顯示模式 (BGR) 切換成 RGB、HSV、Gray等顯示色調的模式。
-
Gaussianblur (高斯模糊)
import cv2
import numpy as np
img = cv2.imread("lena.png")
kernel = np.ones((5,5),np.uint8) #return a new array of given shape and type, filled with ones.
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #convert lena.png to gray
imgBlur = cv2.GaussianBlur(imgGray,(7,7),0) #blurs an image using a Gaussian filter
imgCanny = cv2.Canny(img,195,195) #blurs an image using a Canny filter
imgDialation = cv2.dilate(imgCanny,kernel,iterations=1) #dilate Canny filter image
imgEroded = cv2.erode(imgDialation,kernel,iterations=1)
cv2.imshow("Gray Image",imgGray)
cv2.imshow("Blur Image",imgBlur)
cv2.imshow("Canny Image",imgCanny)
cv2.imshow("Dilate Image",imgDialation)
cv2.imshow("Eroded Image",imgEroded)
cv2.waitKey(0)
No comments:
Post a Comment