04-28 14:02
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Programming / OpenCV] 1초마다 지정 폴더에 이미지 저장하기 본문

Programming/OpenCV

[Programming / OpenCV] 1초마다 지정 폴더에 이미지 저장하기

cinema4dr12 2017. 7. 26. 17:24

다음 코드는 웹캠으로부터 입력받은 Frame Capture 이미지의 좌측 상단에 현재의 현재의 날짜와 시간을 출력하고, 이미지를 해당 경로(D:/WebCam/)에 1초 간격으로 이미지를 저장하는 OpenCV 코드입니다. 이미지를 일정한 시간 간격으로 저장하는데 유용한 코드입니다.


WebCamp.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * @file WebCam_Demo.cpp
 * @brief Sample code showing how to save images in 1 sec. interval
 * @author Geol Choi, ph.D
 */
 
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <ctime>
 
using namespace std;
using namespace cv;
 
void CreateFolder( const char* path );
 
/* @ function main */
int main( int argc, char** argv )
{
    // open the default camera
    int device = 0;
    cv::VideoCapture cap( device );
 
    std::string strFilePath = "";
    std::clock_t start;
    double duration;
    
    int nNew_duration = 0;
    int nOld_duration = 0;
 
    // make directory
    std::time_t rawtime;
    std::tm* timeinfo;
    char c_buffer[80];
    char c_pathBuffer[80];
 
    std::time( &rawtime );
    timeinfo = std::localtime( &rawtime );
 
    std::strftime( c_buffer, 80"%Y%m%d", timeinfo );
 
    std::string strBasePath( c_buffer );
    sprintf_s( c_pathBuffer, "D:\\WebCam\\%s", strBasePath );
 
    CreateFolder( c_pathBuffer );
 
    std::string basePath( c_pathBuffer );
 
    start = std::clock();
 
    // check if we succeeded
    if!cap.isOpened() )
        return -1;
 
    cv::namedWindow( "WebCam Frame Capture");
 
    /// Text Location
    cv::Point myPoint;
    myPoint.x = 10;
    myPoint.y = 40;
 
    /// Font Face
    int myFontFace = 2;
 
    /// Font Scale
    double myFontScale = 1.2;
 
    int tmp = 1;
    bool bTmp = false;
 
    for( ; ; ) {
        if!bTmp ) {
            std::cout << "Please enter any key to start video capture: " << std::endl;
            std::cin >> tmp;
            bTmp = true;
        }
 
        cv::Mat frame;
 
        // current date/time based on current system
        std::time( &rawtime );
 
        timeinfo = std::localtime( &rawtime );
        std::strftime( c_buffer, 80"%Y-%m-%d-%H:%M:%S", timeinfo );
    
        // convert now to string form
        std::string myText( c_buffer );
 
        // another time format
        std::strftime( c_buffer, 80"%H%M%S", timeinfo );
        std::string str( c_buffer );
        
        // get a new frame from camera
        cap >> frame;
 
        cv::putText( frame, myText, myPoint, myFontFace, myFontScale, cv::Scalar( 255128) );
 
        cv::imshow( "WebCam Frame Capture", frame );
 
        duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
        nNew_duration = int( duration );
 
        if( nNew_duration - nOld_duration > ) {
            std::stringstream ss;
            ss << basePath << "\\" << str << ".png";
            std::string s_ = ss.str();
 
            cv::imwrite( s_, frame );
        }
 
        nOld_duration = nNew_duration;
 
        if( cv::waitKey( 10 >= 10 break;
    }
 
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
 
 
/* @ function CreateFolder */
void CreateFolder( const char* path )
{
    wchar_t wtext[20];
    std::mbstowcs( wtext, path, strlen( path ) + );//Plus null
    LPWSTR ptr = wtext;
 
    if!CreateDirectory( ptr, NULL ) )
    {
        return;
    }
}
cs


저장된 이미지 출력


Comments