Python OpenCv学习基础知识一

Python OpenCv学习基础知识一

一、图像操作

import numpy as np
import cv2
from matplotlib import pyplot as plt


def Test1():



    """
    最基础的读取内容
    """


    img = cv2.imread("E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\test1.jpg")
    # 读取图片注意一定是绝对路径
    # 注意这里需要使用的是绝对路径。
    # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # 上面哪一行有点问题,不要加。
    # read pictures.

    cv2.namedWindow("image", cv2.WINDOW_NORMAL)
    # set window
    # 第一个参数是命名

    cv2.resizeWindow("image", 640, 480)
    # set the size of the window
    # 设置窗口的大小

    cv2.moveWindow("image", 0, 0)
    # set the position of the window
    # (0, 0) 对应于是在整个屏幕的左上角。
    # 设置显示窗口的位置

    cv2.imshow("image", img)
    # show the img
    # 第一个参数表示选择的窗口,第二个参数表示显示的图片   

    cv2.waitKey(0)
    # wait for a keyboard event
    # 等待按钮按下去

    cv2.destroyAllWindows()
    # 等到按下了0这个按键的话就会直接退出来啦。


def Test2():



    """
    保存图片
    """


    img = cv2.imread("E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\test2.jpg")
    cv2.namedWindow("image", cv2.WINDOW_NORMAL)
    cv2.imshow("image", img)

    k = cv2.waitKey(0)

    if k == 27:  # 27 -> esc.
        # 按下Esc以后就会直接退出去了。
        cv2.destroyAllWindows()
        # esc

    elif k == ord('s'):
        cv2.imwrite("E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\test20.jpg", img)
        # 保存图片
        cv2.destroyAllWindows()
        # 保存以后在退出去。
    

def Test3():



    """
    使用matplotlib来展示图片。
    """ 


    img = cv2.imread("E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\test2.jpg", 0)
    # 0 其实加不加都可以,影响不大


    plt.imshow(img,cmap='gray',interpolation = 'bicubic')
    plt.xticks([]),plt.yticks([])  #to hide tick values on X and Y axis
    plt.show()


if __name__ == '__main__':
    


    # Test1()

    # Test2()

    Test3()


    pass
    

二、摄像头

"""
cv2处理视频的基本操作

视频

摄像头捕获视频来显示(摄像头)。


摄像头捕获。





"""


import numpy as np
import cv2


# 0 默认摄像头。

cap = cv2.VideoCapture(0)  # 摄像头(默认的)。

# 使用默认的摄像头来进行拍照啦。

while(True):
    ret, frame = cap.read()  # 读取摄像头的内容。
    # 获取摄像头的内容。

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # 简单的处理

    cv2.imshow('frame', gray)
    # 显示图像

    if cv2.waitKey(1) &0xFF == ord('q'):
        # 如果按下 q 按键,那么,就会直接退出 while 循环
        break 
        # 设置退出条件。


# 退出循环以后需要销毁摄像头以及相框的显示
cap.release()
# cap release
cv2.destroyAllWindows()
# windows destroy

"""
以上是使用摄像头来显示视频的。
"""



三、播放视频

"""
视频处理

读取本地的视频,然后进行显示。


读取本地视频并且进行展示出来了。



"""



import numpy as np
import cv2


cap = cv2.VideoCapture('E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\test2.mp4')
# 注意这里仍然是绝对的路径,绝对路径!!!!!


while(True):
    ret, frame = cap.read()
    # 读取内容。

    

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) &0xFF == ord('q'):  # 退出去了。


        break

cap.release()

cv2.destroyAllWindows()
# 释放显示器,摧毁所有的窗口。





四、保存视频

"""
保存视频
"""


import numpy as np
import cv2


# 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('E:\\\\1\\\\Documents\\\\PyTorch\\\\pytorch\\\\others\\\\opencv_cv_2\\\\output.mp4'
,fourcc, 20.0, (640,480))
# 这里仍然是绝对路径。
# 保存视频。




while(cap.isOpened()):
    ret, frame = cap.read()
    # 读取

    if ret==True:
        # frame = cv2.flip(frame,0)
        # 这个是视频进行翻转的操作,可以不要的。


        # 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()
# 窗口的释放



"""
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,0)

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


"""
这段代码是会直接进行视频翻转的操作的。




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,0)

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

最后,谢谢大家的阅读与支持了啊。

おすすめ

転載: blog.csdn.net/m0_54218263/article/details/122491612