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()

以上有问题的地方或者有哪些不对的地方欢迎大家给我指出来
注释的内容是我自己理解的 可能也有不对的地方 大家多多见谅
希望大家能够指点一二 才能使我进步

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