小白学python(opencv图像加载与保存)

577 阅读2分钟

图像加载

import cv2  as cv

src = cv.imread("C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg")#读取图片
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)#创建窗口
cv.imshow("input image",src)#把图片放到窗口中去
cv.waitKey(0) #没有的话直接就退出了
cv.destroyAllWindows()

上面是主体,用上面的图片进行读取里面的格式类型 我们定义一个测试函数

def get_image_info(image):
	print(type(image))#图像类型
    print(image.shape)#高,宽,通道数目
    print(image.size)#大小
	print(image.dtype)

此处注意python为解释型语言,一行一行运行,所以先定义函数再运行主体。 也可以定义主函数,用

if __name__ =="__main__":
    main()

方式并放到结尾,程序就会所有读取完再进行主函数,那么单个函数就无所谓放哪了。 结果为: 在这里插入图片描述 类型为numpy.ndarray 即高为2880,长5120,三通道; 44236830=288051203;

图像保存

在main加一句:

cv.imwrite("D:/result.png",src)

在这里插入图片描述

视频操作

摄像头操作

代码为

def video_demo():
    capture = cv.VideoCapture(0) #0为打开一个摄像头
    while(True):
        ret,frame = capture.read()
        frame= cv.flip(frame,1) #镜像调换
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27):    #esc为27,按esc即退出
            break

运行结果为: 在这里插入图片描述 cap.read():返回一个布尔值(True / False)。如果帧被正确读取,则返回true,否则返回false。可以通过检查这个返回值来判断视频是否结束。

从文件中播放视频

和从相机捕获视频相同,只需更改相机索引与视频文件名。 在显示帧时,选择适当的cv2.waitKey()时间,如果该值太小,视频会非常快,如果它太大,视频会很慢(这可以用来慢动作显示视频)。 正常情况下,25毫秒即可。(差不多??)

def video_demo():
    capture = cv.VideoCapture("E:/电影/[阳光电影www.ygdy8.com].疾速特攻.BD.720p.中英双字幕.mkv") #简简单单把0改为电影路径即可
    while(True):
        ret,frame = capture.read()
        frame= cv.flip(frame,1) #镜像调换
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27):    #esc为27,按esc即退出
            break

添加读取fps,大小,时长功能

def video_demo():#调用摄像头
    capture = cv.VideoCapture("E:/电影/[阳光电影www.ygdy8.com].疾速特攻.BD.720p.中英双字幕.mkv")
    while(True):
        ret,frame = capture.read()
        frame=cv.flip(frame,1)
        # 读取视频的fps,  大小
        fps=capture.get(cv.CAP_PROP_FPS)
        size=(capture.get(cv.CAP_PROP_FRAME_WIDTH),capture.get(cv.CAP_PROP_FRAME_HEIGHT))
        print("fps: {}\nsize: {}".format(fps,size))
        # 读取视频时长(帧总数)
        total = int(capture.get(cv.CAP_PROP_FRAME_COUNT))
        print("[INFO] {} total frames in video".format(total))
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27):    #esc为27
            break

总体代码:

import cv2  as cv
import  numpy as np
def main():
    src = cv.imread("C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg")
    cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
    cv.imshow("input image",src)
    video_demo()
    cv.waitKey(0)

    cv.destroyAllWindows()
    get_image_info(src)
    cv.imwrite("D:/result.png",src)

def get_image_info(image):
    print(type(image))
    print(image.shape)
    print(image.size)
    print(image.dtype)
    pixel_data = np.array(image)
    print(pixel_data)


def video_demo():#调用摄像头
    capture = cv.VideoCapture("E:/电影/[阳光电影www.ygdy8.com].疾速特攻.BD.720p.中英双字幕.mkv")
    while(True):
        ret,frame = capture.read()
        frame=cv.flip(frame,1)
        # 读取视频的fps,  大小
        fps=capture.get(cv.CAP_PROP_FPS)
        size=(capture.get(cv.CAP_PROP_FRAME_WIDTH),capture.get(cv.CAP_PROP_FRAME_HEIGHT))
        print("fps: {}\nsize: {}".format(fps,size))
        # 读取视频时长(帧总数)
        total = int(capture.get(cv.CAP_PROP_FRAME_COUNT))
        print("[INFO] {} total frames in video".format(total))
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27):    #esc为27
            break

if __name__ =="__main__":
    main()

运行截图: 在这里插入图片描述