-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathImageInput.cpp
More file actions
103 lines (80 loc) · 2.47 KB
/
ImageInput.cpp
File metadata and controls
103 lines (80 loc) · 2.47 KB
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
/*
* ImageInput.cpp
*
*/
#include <ctime>
#include <string>
#include <list>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>
#include "ImageInput.h"
ImageInput::~ImageInput() {
}
cv::Mat& ImageInput::getImage() {
return _img;
}
time_t ImageInput::getTime() {
return _time;
}
void ImageInput::setOutputDir(const std::string & outDir) {
_outDir = outDir;
}
void ImageInput::saveImage() {
struct tm date;
localtime_r(&_time, &date);
char filename[PATH_MAX];
strftime(filename, PATH_MAX, "/%Y%m%d-%H%M%S.png", &date);
std::string path = _outDir + filename;
if (cv::imwrite(path, _img)) {
log4cpp::Category::getRoot() << log4cpp::Priority::INFO << "Image saved to " + path;
} else {
log4cpp::Category::getRoot() << log4cpp::Priority::ERROR << "Failed to save image to " + path;
}
}
DirectoryInput::DirectoryInput(const Directory& directory) :
_directory(directory) {
_filenameList = _directory.list();
_filenameList.sort();
_itFilename = _filenameList.begin();
}
bool DirectoryInput::nextImage() {
if (_itFilename == _filenameList.end()) {
return false;
}
std::string path = _directory.fullpath(*_itFilename);
_img = cv::imread(path.c_str());
// read time from file name
struct tm date;
memset(&date, 0, sizeof(date));
date.tm_year = atoi(_itFilename->substr(0, 4).c_str()) - 1900;
date.tm_mon = atoi(_itFilename->substr(4, 2).c_str()) - 1;
date.tm_mday = atoi(_itFilename->substr(6, 2).c_str());
date.tm_hour = atoi(_itFilename->substr(9, 2).c_str());
date.tm_min = atoi(_itFilename->substr(11, 2).c_str());
date.tm_sec = atoi(_itFilename->substr(13, 2).c_str());
_time = mktime(&date);
log4cpp::Category::getRoot() << log4cpp::Priority::INFO << "Processing " << *_itFilename << " of " << ctime(&_time);
// save copy of image if requested
if (!_outDir.empty()) {
saveImage();
}
_itFilename++;
return true;
}
CameraInput::CameraInput(int device) {
_capture.open(device);
}
bool CameraInput::nextImage() {
time(&_time);
// read image from camera
bool success = _capture.read(_img);
log4cpp::Category::getRoot() << log4cpp::Priority::INFO << "Image captured: " << success;
// save copy of image if requested
if (success && !_outDir.empty()) {
saveImage();
}
return success;
}