OpenCv3--Python3.6 (first)圖像、視頻簡單操作

First.視頻簡單操作

def get_video_infor():
    capture = cv2.VideoCapture(0)      #開啓攝像頭 只有一個攝像頭0
    flag = capture.isOpened()     #判斷是否打開
    while flag:
        ret,frame = capture.read()       #frame  幀 ret:表示true 或者false 表示有沒有讀到圖片,輸出空圖像
        frame = cv2.flip(frame,1)     #1:左右對調 -1:上下對調
        cv2.imshow("video:",frame)
        c = cv2.waitKey(50)
        if c == 27:                 #esc的十進制 按esc暫停再可關閉
            break
    capture.release()       #關閉攝像頭

Secondly.圖像的簡單操作

def get_image_infor(image):
    height = image.shape[0]                  #圖像的寬/高
    width = image.shape[1]                   #圖像的長
    channels = image.shape[2]             #圖像的通道數
    print("height: %s , width: %s , channels: %s "% (height,width,channels))
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pixel_now = image[row,col,c]           #記錄當前像素值
                image[row, col,  c] = 255 - pixel_now   #對當前像素值進行操作
    cv2.imshow("pic",image)                         #顯示新的圖像
    print(type(image))                                   #輸出圖像矩陣的類型
    print(image.shape)									#輸出圖像的大小/像素(包括通道數)
    print(image.size)										#輸出圖像的大小/像素點的個數
    print(image.dtype)									#輸出圖像的位數

    image_data = np.array(image)
    print(image_data)									#輸出圖像矩陣內容

Thirdly.自建圖像

def imgae_operation():
    #自建圖像矩陣 400*400 3通道
    #image = np.zeros([400,400,3],np.uint8)  #3通道圖片
    #image[:,:,0] = np.ones([400,400])*255     #BGR
    image = np.ones([400,400,1],np.uint8)*127   #單通道圖片
    '''
    之所以np.ones函數參數類型是uint8,
     是因爲uint8數的範圍爲0~255,
     那麼爲0時恰好爲黑色,
     爲255時恰好爲白色。
     若函數參數類型爲int8,
     則int8類型數的範圍爲-128~127,
    那麼-128則爲黑色,127爲白色
    '''
    cv2.imshow("p",image)

Last最後總的代碼:

import cv2
import numpy as np

#opencv和視頻
def get_video_infor():
    capture = cv2.VideoCapture(0)#開啓攝像頭 只有一個攝像頭0
    flag = capture.isOpened()#判斷是否打開
    while flag:
        ret,frame = capture.read()#frame  幀 ret:表示true 或者false 表示有沒有讀到圖片,輸出空圖像
        frame = cv2.flip(frame,1)#1:左右對調 -1:上下對調
        cv2.imshow("video:",frame)
        c = cv2.waitKey(50)
        if c == 27:#esc的十進制 按esc暫停再可關閉
            break
    capture.release()#關閉攝像頭


#opencv和圖像
def get_image_infor(image):
    height = image.shape[0]#圖像的寬/高
    width = image.shape[1]#圖像的長
    channels = image.shape[2]#圖像的通道數
    print("height: %s , width: %s , channels: %s "% (height,width,channels))
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pixel_now = image[row,col,c]#記錄當前像素值
                image[row, col,  c] = 255 - pixel_now
    cv2.imshow("pic",image)#顯示新的圖像
    print(type(image))#輸出圖像矩陣的類型
    print(image.shape)#輸出圖像的大小/像素(包括通道數)
    print(image.size)#輸出圖像的大小/像素點的個數
    print(image.dtype)#輸出圖像的位數

    image_data = np.array(image)
    print(image_data)#輸出圖像矩陣內容


#opencv和numpy對圖像的操作
def imgae_operation():
    #自建圖像矩陣 400*400 3通道
    #image = np.zeros([400,400,3],np.uint8)  #3通道圖片
    #image[:,:,0] = np.ones([400,400])*255     #BGR
    image = np.ones([400,400,1],np.uint8)*127   #單通道圖片
    '''
    之所以np.ones函數參數類型是uint8,
     是因爲uint8數的範圍爲0~255,
     那麼爲0時恰好爲黑色,
     爲255時恰好爲白色。
     若函數參數類型爲int8,
     則int8類型數的範圍爲-128~127,
    那麼-128則爲黑色,127爲白色
    '''
    cv2.imshow("p",image)


img = cv2.imread("./1.jpg",1)
cv2.namedWindow("picture:",cv2.WINDOW_AUTOSIZE)
cv2.imshow("picture:",img)			

#注意哦 若是namedWindow和imshow裏面的對窗口的標題不一樣會出現兩個窗口,一個圖片一個灰色

#求對圖像每一像素進行操作所用的時間
t1 = cv2.getTickCount()					#獲取cpu到現在轉動的圈數
get_image_infor(img)
t2 = cv2.getTickCount()			#執行完操作後cpu轉動的圈數
t = (t2 - t1)/cv2.getTickFrequency()*1000			#求幾毫秒鐘 cv2.getTickFrequency()cpu一秒鐘轉的圈數
print("time: %s"%(t))
imgae_operation()
get_video_infor()
cv2.waitKey(0)
cv2.destroyAllWindows()

以上有問題的地方或者有哪些不對的地方歡迎大家給我指出來
註釋的內容是我自己理解的 可能也有不對的地方 大家多多見諒
希望大家能夠指點一二 才能使我進步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章