-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cxx
116 lines (86 loc) · 3.47 KB
/
main.cxx
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
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "boost/program_options.hpp"
#include "boost/filesystem.hpp"
int parseInputArguments(int argc, char *argv[],
std::string & InputPath)
{
boost::program_options::options_description description("InputPath");
description.add_options()
("help", "produce help message")
("InputPath", boost::program_options::value< std::string >(), "Directory that contains all data images (required)")
;
boost::program_options::variables_map variableMap;
boost::program_options::store(
boost::program_options::parse_command_line(argc, argv, description),
variableMap
);
boost::program_options::notify(variableMap);
if ( variableMap.count("help") || argc == 1 ) {
std::cout << description << std::endl;
return EXIT_FAILURE;
}
if ( variableMap.count("InputPath") ) {
InputPath = variableMap["InputPath"].as< std::string >();
std::cout << "Input directory: " << InputPath << std::endl;
} else {
std::cerr << "Directory not provided" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
std::string InputPathName;
if(parseInputArguments(argc, argv, InputPathName))
return EXIT_FAILURE;
boost::filesystem::path InputPath(InputPathName);
std::vector<boost::filesystem::path> ImagePaths;
if ( !boost::filesystem::exists( InputPath ) ) { return EXIT_FAILURE; }
boost::filesystem::directory_iterator end_itr;
for ( boost::filesystem::directory_iterator itr( InputPath );
itr != end_itr;
++itr )
{
if (boost::filesystem::extension(itr->path()) == ".jpg" ||
boost::filesystem::extension(itr->path()) == ".JPG")
{
ImagePaths.push_back(itr->path());
}
}
std::cout << "Processing " << ImagePaths.size() << " images." << std::endl;
cv::Mat SizeImage = cv::imread(ImagePaths[0].c_str(), CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat ImageWeigths = cv::Mat::zeros(SizeImage.size(), CV_32FC1);
cv::Mat ImageIndexs = cv::Mat::zeros(SizeImage.size(), CV_32FC1);
std::vector<cv::Mat> FinalImages;
cv::Mat FinalImage(ImageIndexs.size(), CV_8UC3);
for(size_t i = 0; i < ImagePaths.size(); ++i) {
cv::Mat img = cv::imread(ImagePaths[i].c_str(), CV_LOAD_IMAGE_COLOR);
for(int row = 0; row < img.rows; ++row) {
for(int col = 0; col < img.cols; ++col) {
cv::Vec3f RGBPixel = static_cast<cv::Vec3f>( img.at<cv::Vec3b>(row, col) );
float darkness = 1. / (RGBPixel[0]+RGBPixel[1]+RGBPixel[2]);
float diff = std::abs(RGBPixel[0]-RGBPixel[1]) + std::abs(RGBPixel[1]-RGBPixel[2]) + std::abs(RGBPixel[0]-RGBPixel[2]);
float weight = darkness * (5. + diff);
if(weight > ImageWeigths.at<float>(row, col)) {
ImageWeigths.at<float>(row, col) = weight;
ImageIndexs.at<float>(row, col) = i;
FinalImage.at<cv::Vec3b>(row, col) = img.at<cv::Vec3b>(row, col);
}
}
}
if(i % 10 == 0 && i != 0) {
FinalImages.push_back(FinalImage.clone());
ImageWeigths = cv::Mat::zeros(SizeImage.size(), CV_32FC1);
ImageIndexs = cv::Mat::zeros(SizeImage.size(), CV_32FC1);
}
}
size_t size = FinalImages.size();
std::cout << "Generated " << size << " images." << std::endl;
FinalImage = cv::Mat::zeros(ImageIndexs.size(), CV_8UC3);
for(size_t i = 0; i < size; ++i) {
FinalImage += FinalImages[i]/size;
}
cv::imwrite("final.png", FinalImage);
return EXIT_SUCCESS;
}