-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.cc
121 lines (94 loc) · 2.83 KB
/
image.cc
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
#include <stdexcept>
#include "image.h"
Mat Image::get_image() {
if (img.data) {
// image already loaded
return img;
}
// load on the fly image from file
LOG(DEBUG) << "Loading image from file: " << filename;
img = imread(filename, CV_LOAD_IMAGE_UNCHANGED);
if (!img.data) {
throw std::runtime_error("Could not open file.");
}
LOG(DEBUG) << "Loaded image size: " << img.size()
<< ", channels: " << img.channels();
return img;
}
void Image::add_transparency_layer(string filename) {
Mat image = get_image();
LOG(DEBUG) << "Loading alpha channel from file: " << filename;
Mat mask = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if (!mask.data) {
throw std::runtime_error("Could not open file.");
}
vector<Mat> input(2);
input[0] = image;
input[1] = mask;
merge(input, img);
LOG(DEBUG) << "Image size: " << img.size()
<< ", channels: " << img.channels();
}
Mat Image::get_image_gray() {
if (img_gray.data) {
// gray image already loaded
return img_gray;
}
// convert on the fly image gray from image
Mat img_color;
img_color = get_image();
LOG(DEBUG) << "Converting color to gray image";
cvtColor(img, img_gray, COLOR_BGR2GRAY);
if (!img_gray.data) {
throw std::runtime_error("Could not get gray image");
}
return img_gray;
}
ImageFeaturesPtr Image::get_image_features() {
if (features) {
return features;
}
Mat image_gray = get_image_gray();
features.reset(new ImageFeatures);
// get SIFT like features
int rc;
LOG(DEBUG) << "Getting SIFT-like features";
rc = get_features(image_gray, *features);
if (rc != 0) {
LOG(ERROR) << "Unable to compute image features";
features.reset();
return ImageFeaturesPtr();
}
return features;
}
void Image::write(FileStorage& fs) const{
LOG(DEBUG) << "Serializing Image";
fs << "{"
<< "name" << name
<< "filename" << filename
<< "camera_mat" << K
<< "coords" << coords
<< "features" << *features
<< "}";
}
void Image::read(const FileNode& node) {
LOG(DEBUG) << "De-serializing Image";
features.reset(new ImageFeatures());
node["name"] >> name;
node["filename"] >> filename;
node["camera_mat"] >> K;
node["coords"] >> coords;
node["features"] >> *features;;
}
Mat dewarp_channels(const Mat input, const Mat H, Size output_size) {
int channels = input.channels();
vector<Mat> input_channels(channels);
vector<Mat> output_channels(channels);
split(input, input_channels);
for (int i = 0; i < channels; i++) {
warpPerspective(input_channels[i], output_channels[i], H, output_size);
}
Mat output;
merge(output_channels, output);
return output;
}