opencv读取本地视频

C++

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    cvNamedWindow("WIND0", CV_WINDOW_AUTOSIZE);
    CvCapture* capture = cvCreateFileCapture(argv[1]);
    IplImage* frame;

    while(1) 
    {
        frame = cvQueryFrame(capture);
        if (!frame) break;
        cvShowImage("WIND0", frame);
        char c = cvWaitKey(33);
        if (c == 'c') break;
    }

    cvReleaseCapture(&capture);
    cvDestroyWindow("WIND0");


    return 0;
}

 

#include  
#include  
#include 
 
using namespace cv;
 
int main(int argc, char** argv)
{
 
    VideoCapture cap("E:\\A_Develope\\test_video\\fitness.avi");
    if (!cap.isOpened())
    {
        return -1;
    }
    Mat frame;
    while (1)
    {
        cap >> frame;
        if (frame.empty()) break;
        imshow("当前视频", frame);
        if (waitKey(30) >= 0)
            break;
    }
    return 0;
}

 

python

import cv2

cap = cv2.VideoCapture('put your video name here')
while(cap.isOpened()):
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

获取像素值并修改
import cv2
import numpy as np

img = cv2.imread('/home/blvin/图片/don.png')

#获取像素值
px = img[10,100]
print(px)

blue = img[10,100,2]
print(blue)

#修改像素值
img[10,100] = [255,255,255]
print(img[10,100])

#获取像素值及修改的更好方法
print(img.item(10,100,2))
img.itemset((10,100,2),100)
print(img.item(10,100,2))

获取图像属性
#获取图像属性
print(img.shape)
#如果是灰度图,返回值仅有行数和列数

#返回像素点数目
print(img.size)

#返回图像的数据类型
print(img.dtype)
 

 

Python OpenCV对本地视频文件进行分帧保存

# coding=utf-8

import os
import cv2

videos_src_path = "/home/wgp/视频/"
video_formats = [".MP4", ".MOV"]
frames_save_path = "/home/wgp/视频/"
width = 320
height = 240
time_interval = 50


def video2frame(video_src_path, formats, frame_save_path, frame_width, frame_height, interval):
    """
    将视频按固定间隔读取写入图片
    :param video_src_path: 视频存放路径
    :param formats: 包含的所有视频格式
    :param frame_save_path: 保存路径
    :param frame_width: 保存帧宽
    :param frame_height: 保存帧高
    :param interval: 保存帧间隔
    :return: 帧图片
    """
    videos = os.listdir(video_src_path)

    def filter_format(x, all_formats):
        if x[-4:] in all_formats:
            return True
        else:
            return False

    videos = filter(lambda x: filter_format(x, formats), videos)

    for each_video in videos:
        print "正在读取视频:", each_video

        each_video_name = each_video[:-4]
        os.mkdir(frame_save_path + each_video_name)
        each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + "/"

        each_video_full_path = os.path.join(video_src_path, each_video)

        cap = cv2.VideoCapture(each_video_full_path)
        frame_index = 0
        frame_count = 0
        if cap.isOpened():
            success = True
        else:
            success = False
            print("读取失败!")

        while(success):
            success, frame = cap.read()
            print "---> 正在读取第%d帧:" % frame_index, success

            if frame_index % interval == 0:
                resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA)
                # cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_index, resize_frame)
                cv2.imwrite(each_video_save_full_path + "%d.jpg" % frame_count, resize_frame)
                frame_count += 1

            frame_index += 1

    cap.release()


if __name__ == '__main__':
    video2frame(videos_src_path, video_formats, frames_save_path, width, height, time_interval)

你可能感兴趣的:(图像)