forked from Brick-Mover/RAISR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
90 lines (71 loc) · 2.58 KB
/
Utils.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
#include <dirent.h>
#include "Utils.h"
#include <sys/stat.h>
using namespace std;
using namespace cv;
//https://stackoverflow.com/questions/10167534/how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv
string type2str(int type) {
string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
r += "C";
r += (chans+'0');
return r;
}
void debugMat(Mat m) {
cout << "Rows: " << m.rows << ", Cols: " << m.cols << endl;
cout << "M = "<< endl << " " << m << endl << endl;
waitKey();
}
//https://stackoverflow.com/questions/9905093/how-to-check-whether-two-matrixes-are-identical-in-opencv
bool matIsEqual(const cv::Mat mat1, const cv::Mat mat2) {
// treat two empty mat as identical as well
if (mat1.empty() && mat2.empty()) {
return true;
}
// if dimensionality of two mat is not identical, these two mat is not identical
if (mat1.cols != mat2.cols || mat1.rows != mat2.rows || mat1.dims != mat2.dims) {
return false;
}
cv::Mat diff;
cv::compare(mat1, mat2, diff, cv::CMP_NE);
int nz = cv::countNonZero(diff);
return nz == 0;
}
void readListOfImage(string& dirPath, vector<Mat>& imageMatList, vector<string>& imageNameList) {
DIR *dir;
struct dirent *entry;
string filePath;
struct stat fileStat;
if ((dir = opendir (dirPath.c_str())) != NULL) {
//print all the files and directories within directory
while ((entry = readdir (dir)) != NULL) {
filePath= dirPath + "/" + entry->d_name;
// check if file is valid and check if file is not a directory
if (stat( filePath.c_str(), &fileStat )) continue;
if (S_ISDIR( fileStat.st_mode )) continue;
cout<< "read image: " << filePath.c_str() << endl;
imageNameList.push_back(string(entry->d_name));
Mat currentImage = imread(filePath.c_str(), 0);
if (!currentImage.data) {
cout << "Read current image error!" << endl;
exit (EXIT_FAILURE);
}
imageMatList.push_back(currentImage);
}
closedir (dir);
} else {
cout << "Error opening directory" << endl;
exit (EXIT_FAILURE);
}
}