forked from AllskyTeam/allsky
-
Notifications
You must be signed in to change notification settings - Fork 9
/
keogram.cpp
187 lines (169 loc) · 5.23 KB
/
keogram.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Simple keogram composition program using OpenCV
// Copyright 2018 Jarno Paananen <[email protected]>
// Based on a script by Thomas Jacquin
// SPDX-License-Identifier: MIT
#include <cstdlib>
#include <glob.h>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <vector>
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
if (argc < 4)
{
std::cout << KRED << "You need to pass 3 arguments: source directory, "
"image extension, output file"
<< std::endl;
std::cout << "Optionally you can pass after them: " << std::endl;
std::cout << " -no-label - Disable hour labels" << std::endl;
std::cout << " -fontname = Font Name - Default = 0 - Font Types "
"(0-7), Ex. 0 = simplex, 4 = triplex, 7 = script"
<< std::endl;
std::cout << " -fontcolor = Font Color - Default = 255 0 0 - Text "
"blue (BRG)"
<< std::endl;
std::cout << " -fonttype = Font Type - Default = 8 - Font Line "
"Type,(0-2), 0 = AA, 1 = 8, 2 = 4"
<< std::endl;
std::cout << " -fontsize - Default = 2.0 - Text Font Size" << std::endl;
std::cout << " -fontline - Default = 3 - Text Font "
"Line Thickness"
<< std::endl;
std::cout << " ex: keogram ../images/current/ jpg keogram.jpg -fontsize 2" << std::endl;
std::cout << " ex: keogram . png ~/allsky/keogram.jpg -no-label" << KNRM << std::endl;
return 3;
}
std::string directory = argv[1];
std::string extension = argv[2];
std::string outputfile = argv[3];
bool labelsEnabled = true;
int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontScale = 2;
int fontType = 8;
int thickness = 3;
char fontColor[3] = { 255, 0, 0 };
// Handle optional parameters
for (int a = 4; a < argc; ++a)
{
if (!strcmp(argv[a], "-no-label"))
{
labelsEnabled = false;
}
else if (!strcmp(argv[a], "-fontname"))
{
fontFace = atoi(argv[++a]);
}
else if (!strcmp(argv[a], "-fonttype"))
{
fontType = atoi(argv[++a]);
}
else if (!strcmp(argv[a], "-fontsize"))
{
fontScale = atof(argv[++a]);
}
else if (!strcmp(argv[a], "-fontline"))
{
thickness = atoi(argv[++a]);
}
else if (!strcmp(argv[a], "-fontcolor"))
{
fontColor[0] = atoi(argv[++a]);
fontColor[1] = atoi(argv[++a]);
fontColor[2] = atoi(argv[++a]);
}
}
glob_t files;
std::string wildcard = directory + "/*." + extension;
glob(wildcard.c_str(), 0, NULL, &files);
if (files.gl_pathc == 0)
{
globfree(&files);
std::cout << "No images found, exiting." << std::endl;
return 0;
}
cv::Mat accumulated;
int prevHour = -1;
cv::Mat image;
for (size_t f = 0; f < files.gl_pathc * 2; f++)
{
if (!(f & 1))
image = cv::imread(files.gl_pathv[f / 2], cv::IMREAD_UNCHANGED);
if (!image.data)
{
std::cout << "Error reading file " << basename(files.gl_pathv[f / 2]) << std::endl;
continue;
}
if (!(f & 1))
std::cout << "[" << (f / 2) + 1 << "/" << files.gl_pathc << "] " << basename(files.gl_pathv[f / 2]) << std::endl;
// If we don't have image yet, create one using height and format from
// the source image and width from number of files
if (accumulated.empty())
{
accumulated.create(image.rows, files.gl_pathc * 2, image.type());
}
// Copy middle column to destination
image.col(image.cols / 2).copyTo(accumulated.col(f));
if (labelsEnabled)
{
struct stat s;
stat(files.gl_pathv[f / 2], &s);
struct tm *t = localtime(&s.st_mtime);
if (t->tm_hour != prevHour)
{
if (prevHour != -1)
{
// Draw a dashed line and label for hour
cv::LineIterator it(accumulated, cv::Point(f, 0), cv::Point(f, accumulated.rows));
for (int i = 0; i < it.count; i++, ++it)
{
// 4 pixel dashed line
if (i & 4)
{
uchar *p = *it;
for (int c = 0; c < it.elemSize; c++)
{
*p = ~(*p);
p++;
}
}
}
// Draw text label to the left of the dash
char hour[3];
snprintf(hour, 3, "%02d", t->tm_hour);
std::string text(hour);
int baseline = 0;
cv::Size textSize = cv::getTextSize(text, fontFace, fontScale, thickness, &baseline);
if (f - textSize.width >= 0)
{
cv::putText(accumulated, text,
cv::Point(f - textSize.width, accumulated.rows - textSize.height), fontFace,
fontScale, cv::Scalar(fontColor[0], fontColor[1], fontColor[2]), thickness,
fontType);
}
}
prevHour = t->tm_hour;
}
}
}
globfree(&files);
std::vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(95);
cv::imwrite(outputfile, accumulated, compression_params);
}