LoginSignup
1
0

More than 3 years have passed since last update.

cameraを読み込み

Posted at

code


#include<stdlib.h>
#include<opencv2\opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>

using namespace cv;
using namespace std;



void open_camera()
{
    Mat frame;//フレームレート
    VideoCapture capture(0);//カメラを開ける
    double fps;
    char string[10];
    namedWindow("FPS");
    double t = 0;

    while (1)
    {
        t = (double)getTickCount();
        if (capture.isOpened())
        {
            capture >> frame;//フレームを読み取る
            t = ((double)getTickCount() - t) / getTickFrequency();// tはコードがそこで実行されるのにかかる時間(秒単位)で、fpsはその逆数
            fps = 1.0 / t;
            sprintf_s(string, "%.2f", fps);
            std::string fpsString("FPS:");
            fpsString += string;
            putText(frame,
                fpsString,
                Point(5, 20),
                FONT_HERSHEY_SIMPLEX,
                0.5,
                Scalar(255, 255, 255));
            imshow("FPS", frame);
            waitKey(30);
        }   
    }
}


void save_vedio()
{
    VideoCapture capture;//現在のカメラのビデオ情報を取得する
    capture.open(0);//カメラを開ける
    Size size = Size(capture.get(CV_CAP_PROP_FRAME_WIDTH),capture.get(CV_CAP_PROP_FRAME_HEIGHT));//ビデオパスを開き、基本情報を設定する
    VideoWriter writer;//保存関数をインスタンス化
    writer.open("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, size, true);//保存情報を設定する

    Mat frame;
    namedWindow("output", CV_WINDOW_AUTOSIZE);

    while (capture.read(frame))//フレームを読み取る
    {
        imshow("writing vedio", frame);
        writer.write(frame);//フレームごとに保存する
        waitKey(30);
    }

}

void open_vedio()
{
    VideoCapture capture;
    capture.open("out.avi");//保存した動画読み出す

    while (1)
    {
        Mat frame;
        capture >> frame;
        if (frame.empty())
        {
            break;
        }
        imshow("reading vedio", frame);
        waitKey(30);
    }

}
int main()
{
    open_camera();//問題1+2
    //save_vedio();//問題3、動画像をファイルに保存
    //open_vedio();//問題3、動画を読み出して
}

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0