-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutil.cpp
187 lines (165 loc) · 5.01 KB
/
util.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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
//
// Copyright (C) 2015 MINAGAWA Takuya.
// Third party copyrights are property of their respective owners.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//M*/
#include "util.h"
#include <sstream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <opencv2/imgproc/imgproc.hpp>
// intをStringへ変換
std::string Int2String(int x)
{
std::stringstream strstr;
strstr << x;
return strstr.str();
}
bool hasExtention(const std::string& file_name, const std::vector<std::string>& extentions)
{
std::string ext = boost::filesystem::path(file_name).extension().string();
std::vector<std::string>::const_iterator it = extentions.begin(),
it_e = extentions.end();
for (; it != it_e; it++) {
if (ext == *it)
return true;
}
return false;
}
// ディレクトリから画像ファイル名一覧を取得
bool ReadImageFilesInDirectory(const std::string& img_dir, std::vector<std::string>& image_lists)
{
using namespace boost::filesystem;
path img_dir_path(img_dir);
if(!is_directory(img_dir_path)){
return false;
}
directory_iterator end;
for(directory_iterator p(img_dir_path); p != end; ++p){
path file_p = p->path();
std::string ext = file_p.extension().string();
if(hasImageExtention(file_p.string())){
image_lists.push_back(file_p.string());
}
}
return true;
}
void DrawHistogram(const cv::Mat& histogram, cv::Mat& draw_img, int width)
{
double minval, maxval;
cv::minMaxLoc(histogram, &minval, &maxval);
cv::Mat norm_hist;
histogram.convertTo(norm_hist, CV_32FC1, (double)width / maxval);
if(histogram.cols == 1){
draw_img = cv::Mat::zeros(histogram.rows, width, CV_8UC1);
for(int r=0; r<histogram.rows; r++){
for(int c=0; c<norm_hist.at<float>(r,0); c++){
draw_img.at<unsigned char>(r, c) = 255;
}
}
}
else if(histogram.rows == 1){
draw_img = cv::Mat::zeros(width, histogram.cols, CV_8UC1);
for(int c=0; c<histogram.cols; c++){
for(int r=0; r<norm_hist.at<float>(0,c); r++){
draw_img.at<unsigned char>(width - r -1, c) = 255;
}
}
}
}
cv::Mat Convert8UC3(const cv::Mat& src)
{
cv::Mat tmp, dst;
if(src.depth() != CV_8U){
cv::normalize(src, tmp, 255, 0, cv::NORM_MINMAX, CV_8U);
}
else{
tmp = src;
}
if(tmp.channels() == 1){
cv::cvtColor(tmp, dst, cv::COLOR_GRAY2RGB);
}
else{
dst = tmp;
}
return dst;
}
cv::Mat ConcatinateImage(const cv::Mat& src1, const cv::Mat& src2, bool hol)
{
cv::Mat img1 = Convert8UC3(src1);
cv::Mat img2 = Convert8UC3(src2);
cv::Mat concatImg;
cv::Size img_size;
cv::Point pt;
if(hol){
img_size.width = img1.cols + img2.cols;
img_size.height = std::max(img1.rows, img2.rows);
pt.x = img1.cols;
pt.y = 0;
}
else{
img_size.width = std::max(img1.cols, img2.cols);
img_size.height = img1.rows + img2.rows;
pt.x = 0;
pt.y = img1.rows;
}
concatImg = cv::Mat::zeros(img_size, img1.type());
img1.copyTo(concatImg(cv::Rect(0,0,img1.cols,img1.rows)));
img2.copyTo(concatImg(cv::Rect(pt.x,pt.y,img2.cols,img2.rows)));
return concatImg;
}
template<typename T> void SaveMatCSV_T(const std::string& filename, const cv::Mat& mat)
{
T* data_ptr = (T*)mat.data;
std::ofstream ofs(filename.c_str());
for(int r=0; r<mat.rows; r++){
for(int c=0; c<mat.cols-1; c++){
ofs << *data_ptr << ",";
data_ptr++;
}
ofs << *data_ptr << std::endl;
data_ptr++;
}
}
void SaveMatCSV(const std::string& filename, const cv::Mat& mat)
{
if(mat.type() == CV_8U){
SaveMatCSV_T<unsigned char>(filename,mat);
}
else if(mat.type() == CV_32S){
SaveMatCSV_T<int>(filename,mat);
}
else if(mat.type() == CV_32F){
SaveMatCSV_T<float>(filename,mat);
}
else if(mat.type() == CV_64F){
SaveMatCSV_T<double>(filename,mat);
}
}