代碼筆記-OpenCV3-計算機視覺-python語言實現(第二版)

1.基本I/O腳本

  1.1圖片讀取

 1 import time
 2 
 3 num = 2-1-3+3
 4 print(num)
 5 # 打開顯示圖像
 6 if num == 1:
 7     # 讀取圖像,支持 bmp、jpg、png、tiff 等經常使用格式
 8     img = cv2.imread(r"D:\UIAUTO\trainingSet\simpleGeometricFigure\1.jpg")
 9     # 建立窗口並顯示圖像
10     cv2.namedWindow("Image")
11     cv2.imshow("Image", img)
12     # 刷新窗口
13     cv2.waitKey(0)
14     # 釋放窗口
15     cv2.destroyAllWindows()
cv2讀取圖片

  1.2圖像與原始字節之間轉換

 1 # 2.1.2 圖像與原始字節之間的轉換
 2 # 隨機生成兩個圖像,他們位於腳本所在的目錄,圖像名爲RandomGray.png 和 RandomColor.png.
 3 if num == 2-1-2:
 4     randomByteArray = bytearray(os.urandom(120000))
 5     flatNumpyArray = numpy.array(randomByteArray)
 6 
 7     grayImage = flatNumpyArray.reshape(300, 400)
 8     cv2.imwrite('RandomGray.png', grayImage)
 9     bgrImage = flatNumpyArray.reshape(100, 400, 3)
10     cv2.imwrite('RandomColor.png', bgrImage)
圖像與原始字節之間的轉換

  1.3使用numpy.array訪問圖像數據

 1 # 2.1.3 使用numpy.array訪問圖像數據
 2 if num == 2-1-3+1:
 3     img = cv2.imread(r"D:\UIAUTO\trainingSet\simpleGeometricFigure\1.jpg")
 4     # 將jpg圖像在左上寬50高50區域像素,處轉化爲紅色像素
 5     for a in range(0, 50):
 6         for b in range(0, 50):
 7             img[a, b] = [2, 3, 255]
 8     cv2.imwrite("1.jpg", img)
 9 
10     # item(x,y,通道索引0-2)
11     print("x:49 y:49 R:", img.item(49, 49, 0))
12     print("x:49 y:49 G:", img.item(49, 49, 1))
13     print("x:49 y:49 B:", img.item(49, 49, 2))
14 
15     # 把區域像素設置爲白色,不改變讀入的圖片像素
16     for x in range(0, 50):
17         for y in range(0, 50):
18             img.itemset((x, y, 0), 250)
19             img.itemset((x, y, 1), 250)
20             img.itemset((x, y, 2), 250)
21     cv2.imwrite("21.jpg", img)
訪問圖像數據

  1.4像素變換

1 # 像素變換
2 # 這種變化速度優於上面的循環變換
3 if num == 2-1-3+2:
4     img = cv2.imread(r"D:\PythonWorkspace\deep leanr\venv\21.jpg")
5     img[0:200:1, 0:200:1, 1] = 2
6     cv2.imwrite('22.jpg', img)
7     time.sleep(2)
像素變換

  1.5圖像區域拷貝

 1 # 圖像區域拷貝
 2 if num == 2-1-3+3:
 3     img = cv2.imread(r'D:\PythonWorkspace\deep leanr\venv\18151.jpg')
 4     my_roi = img[0:100:1, 0:100:1]
 5     img[300:400:1, 300:400:1] = my_roi
 6     cv2.namedWindow("Image")
 7     cv2.imshow("Image", img)
 8     cv2.waitKey(0)  # 刷新時間
 9     cv2.destroyAllWindows()  # 釋放窗口
10     print(img.shape)  # 返回一個數組: [寬度,高度,通道數]
11     print(img.size)  # 像素大小
12     print(img.dtype)  # 圖像的數據類型
圖像區域拷貝

2.視頻I/O與捕獲攝像頭幀

  2.1視頻讀取

 1 import cv2
 2 
 3 
 4 # I/O視頻文件
 5 if num == 1:
 6     videoCapture = cv2.VideoCapture('MyInputVid.avi')
 7     fps = videoCapture.get(cv2.CAP_PROP_FPS)
 8     size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
 9             int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
10             )
11     videoWriter = cv2.VideoWriter('MyOutputVid.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
12     success, frame = videoCapture.read()
13     while success:  # Loop until there are no more frames.
14         videoWriter.write(frame)
15         success, frame = videoCapture.read()
視頻文件讀取

  2.2捕獲攝像頭的幀,寫入磁盤

 1 if num == 2:
 2     cameraCapture = cv2.VideoCapture(0)
 3     fps = 30  # an assumption
 4     size = (
 5         int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
 6         int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
 7     )
 8     videoWriter = cv2.VideoWriter('MyOutputVid.avi', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)
 9 
10     success, frame = cameraCapture.read()
11     numFrameRemaining = 10 * fps - 1
12     while success and numFrameRemaining > 0:
13         videoWriter.write(frame)
14         success, frame = cameraCapture.read()
15         numFrameRemaining -= 1
16     cameraCapture.release()
捕獲攝像頭的幀

  2.3在窗口顯示攝像頭幀,按鍵盤大部分鍵位關閉窗口

 1 # 在窗口顯示攝像頭幀
 2 # waitKey()的參數爲等待鍵盤觸發的時間, 單位爲毫秒, 其返回值是-1(表示沒有鍵被按下)或ASCLL碼.
 3 # 另外,python提供了一個標準函數ord(), 該函數能夠將字符轉換爲ASCLL碼. 例如, 輸入ord('a')會返回97
 4 
 5 # OpenCV的窗口函數和waitKey()函數互相依賴.OpenCV的窗口只有在調用waitKey()函數時纔會更新, waitKey()函數只有在OpenCV
 6 # 窗口成爲活動窗口時,才能捕獲輸入信息.
 7 
 8 # 鼠標回調函數setMouseCallback()有五個參數, 如前面的實列代碼所示.param爲可選參數,
 9 # 它是setMouseCallback()函數的第三個參數, 默認狀況下, 該參數是0. 回調事件參數能夠取以下的值, 他們分別對應不一樣的鼠標事件.
10 #   cv2.EVENT_MOUSEMOVE: 該事件對應鼠標移動
11 #   cv2.EVENT_LBUTTONDOWN: 鼠標左鍵按下
12 #   cv2.EVENT_RBUTTONDOWN: 鼠標右鍵按下
13 #   cv2.EVENT_MBUTTONDOWN: 鼠標中間鍵按下
14 #            _LBUTTONUP: 鼠標左鍵鬆開
15 #            _RBUTTONUP:
16 #            _MUBTTONUP:
17 #            _LBUTTONDBLCLK: 雙擊鼠標左鍵
18 #            _RBUTTONDBLCLK:
19 #            _MBUTTONDBLCLK:
20 # 鼠標回調的標誌參數多是一下事件的按位組合
21 #   cv2.EVENT_FLAG_LUBTTON: 該事件對應按下鼠標左鍵
22 #   cv2.EVENT_FLAG_RBUTTON: 該事件對應按下鼠標右鍵
23 #   cv2.EVENT_FLAG_MBUTTON: 該事件對應按下鼠標中間鍵
24 #            _FLAG_CTRLKEY: 按下Ctrl鍵
25 #            _FLAG_SHIFTKEY: 按下Shift鍵
26 #            _FLAG_ALTKEY: 按下Alt鍵
27 # 然而,OpenCV不提供任何處理窗口事件的方法. 例如, 當單擊窗口的關閉按鈕時, 並不能關閉應用程序.
28 # 因爲OpenCV有限的事件處理能力和GUI處理能力, 許多開發人員更喜歡將OpenCV集成到其餘應用程序框架中.
29 if num == 3:
30     clicked = False
31     def onMouse(event, x, y, flags, param):
32         global clicked
33         if event == cv2.EVENT_LUBTTONUP:
34             clicked = True
35     cameraCapture = cv2.VideoCapture(0)
36     cv2.namedWindow('MyWindow')
37     cv2.setMouseCallback('MyWindow', onMouse)
38 
39     print('Showing camera feed. Click window or press any key to stop')
40     success, frame = cameraCapture.read()
41     while success and cv2.waitKey(1) == -1 and not clicked:
42         cv2.imshow('MyWindow', frame)
43         success, frame = cameraCapture.read()
44 
45     cv2.destroyWindow('MyWindow')
46     cameraCapture.release()
按鍵關閉窗口

 

 

 

 

GitHub代碼示例地址:python

相關文章
相關標籤/搜索