-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFitInGrid.cpp
122 lines (117 loc) · 3.38 KB
/
FitInGrid.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
#include "FitInGrid.h"
using namespace std;
namespace cvImagePipeline {
namespace Filter {
///////////////////////////////////////////////////////////////////////////
// class FitInGrid
IMPLEMENT_CVFILTER(FitInGrid);
FitInGrid::FitInGrid()
:
width("width", 640), height("height", 480),
cols("cols", 2), rows("rows", 2),
interleave("interleave", 0),
interleaveIndex(0),
cvInterMode("cvInterMode", CV_INTER_CUBIC)
{
defParam(width);
defParam(height);
defParam(cols);
defParam(rows);
defParam(rows);
defParam(interleave);
defParam(cvInterMode);
undefInputMat("");
refresh();
}
FitInGrid::~FitInGrid() {
int curr_count = dynInputMat.size();
for(int i = 0; i < curr_count; i++) {
delete dynInputMat.at(i);
dynInputMat.at(i) = 0;
}
}
void FitInGrid::execute() {
for (int row = 0; row < rows; row++) {
for(int col = 0; col < cols; col++) {
int index = row * cols + col;
if (index % (interleave + 1) != interleaveIndex) {
continue;
}
stringstream ss;
ss << index;
std::string inputMatName = ss.str();
const cv::Mat& inputMat = getInputMat(inputMatName);
cv::Mat grid(refOutputMat(), cv::Rect(col * gridW, row * gridH, gridW, gridH));
if(inputMat.empty()) {
grid = cv::Scalar(0.4, 0.4, 0.4);
} else {
cv::Mat temp;
double alpha = 1.0;
double beta = 0.0;
switch(inputMat.depth()) {
case CV_8S: alpha = 1.0; beta = 128.0; break;
case CV_16U: alpha = 1.0 / 0x10000; beta = 0.0; break;
case CV_16S: alpha = 1.0 / 0x8000; beta = 128.0; break;
case CV_32F: alpha = 255.0; beta = 0.0; break;
}
inputMat.convertTo(temp, CV_8UC3, alpha, beta);
if(temp.channels() != 3) {
cv::Mat temp2;
cv::cvtColor(temp, temp2, CV_GRAY2BGR, 3);
cv::resize(temp2, grid, cv::Size(gridW, gridH), 0.0, 0.0, cvInterMode);
} else {
cv::resize(temp, grid, cv::Size(gridW, gridH), 0.0, 0.0, cvInterMode);
}
}
}
}
interleaveIndex++;
if (interleaveIndex > interleave) {
interleaveIndex = 0;
}
}
void FitInGrid::onPropertyChange(Property& property) {
refresh();
}
void FitInGrid::refresh() {
gridW = width / cols;
gridH = height / rows;
refOutputMat() = cv::Mat::zeros(height, width, CV_8UC3);
int curr_count = dynInputMat.size();
int next_count = (int)cols * (int)rows;
if(next_count < curr_count) {
for(int i = next_count; i < curr_count; i++) {
stringstream ss;
ss << i;
std::string name = ss.str();
undefInputMat(name);
delete dynInputMat[i];
dynInputMat[i] = 0;
}
while(dynInputMat.size() > (size_t)next_count) {
dynInputMat.pop_back();
}
} else if(next_count > curr_count) {
for(int i = curr_count; i < next_count; i++) {
cv::Mat* inputMat = new cv::Mat(cv::Mat::zeros(gridH, gridW, CV_8UC3));
dynInputMat.push_back(inputMat);
std::stringstream ss;
ss << i;
std::string name = ss.str();
defInputMat(name);
setInputMat(name, *inputMat);
}
}
}
void FitInGrid::onInputMatConnected(const std::string& inputMatName) {
stringstream ss(inputMatName);
int index;
ss >> index;
const cv::Mat& inputMat = getInputMat(inputMatName);
if(dynInputMat[index] != 0 && dynInputMat[index] != &inputMat) {
delete dynInputMat[index];
dynInputMat[index] = 0;
}
}
}
}