-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcliapplication.cpp
More file actions
212 lines (169 loc) · 6.06 KB
/
cliapplication.cpp
File metadata and controls
212 lines (169 loc) · 6.06 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "cliapplication.h"
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <boost/program_options.hpp>
#include <vector>
namespace po = boost::program_options;
void onSliceChange( int a, void* data );
static int windowId = 0;
class SliceDisplayer
{
public:
SliceDisplayer(SegmentationManager* segManager) : SliceDisplayer( segManager, "SegResult") {
this->windowName += "_" + std::to_string(windowId++);
}
SliceDisplayer(SegmentationManager* segManager, std::string windowName ) {
this->segManager = segManager;
this->windowName = windowName;
this->current = 0;
this->labelsAlpha = 70;
}
void display() {
size_t numSlices = segManager->size();
cv::namedWindow( windowName, cv::WINDOW_AUTOSIZE );// Create a window for display.
std::string sliceTrackbarName = "Slice (" + std::to_string(numSlices) + ")";
cv::createTrackbar(sliceTrackbarName, windowName, ¤t, numSlices - 1, onSliceChange, this);
cv::createTrackbar("Alpha", windowName, &this->labelsAlpha, 100, onSliceChange, this);
onSliceChange(0, this);
cv::waitKey(0);
}
SegmentationManager* getSegmentationManager() const {
return this->segManager;
}
std::string getWindowName() const {
return this->windowName;
}
int getLabelsAlpha() const {
return labelsAlpha;
}
int getCurrent() const {
return current;
}
private:
int current;
std::string windowName;
int labelsAlpha;
SegmentationManager* segManager;
};
cv::Mat colorizeLabels(cv::Mat& labels)
{
cv::Mat res(labels.rows, labels.cols, CV_8UC3);
res = cv::Scalar( 0 );
std::vector<cv::Vec3b> colorTable( 256 ); // could be static
uchar color[3];
for(int i = 0; i < 256; i++) {
Seed::getColor(i, color);
colorTable[i][0] = color[0];
colorTable[i][1] = color[1];
colorTable[i][2] = color[2];
}
for(int i = 0; i < labels.rows; i++) {
for(int j = 0; j < labels.cols; j++) {
int label = labels.at<uchar>(i, j);
if(label != EMPTY) {
res.at<cv::Vec3b>(i, j) = colorTable[label];
}
}
}
return res;
}
void onSliceChange( int a, void* data ) {
SliceDisplayer* sliceDisplayer = (SliceDisplayer*) data;
SegmentationManager* segManager = sliceDisplayer->getSegmentationManager();
Slice* slice = segManager->getSlice( sliceDisplayer->getCurrent() );
cv::Mat& image = slice->getImg();
cv::Mat& segResult = slice->getSegmentationResult();
cv::Mat labels = colorizeLabels( segResult );
cv::Mat imgResized, labelsResized;
if( image.rows > 1000 ) {
int newHight = 600;
int newWidth = image.cols * newHight / image.rows;
cv::resize(image, imgResized, cv::Size(newWidth, newHight));
if( !labels.empty() ) cv::resize(labels, labelsResized, cv::Size(newWidth, newHight));
} else {
imgResized = image;
labelsResized = labels;
}
cv::Mat imgShow;
if( !labelsResized.empty() ) {
cv::Mat imgCombined;
float alpha = 1.0 - ( sliceDisplayer->getLabelsAlpha() / 100.f );
float beta = ( 1.0 - alpha);
cv::cvtColor( imgResized, imgResized, CV_GRAY2RGB );
cv::addWeighted( imgResized, alpha, labelsResized, beta, 0.0, imgCombined);
imgShow = imgCombined;
} else {
imgShow = imgResized;
}
cv::imshow( sliceDisplayer->getWindowName(), imgShow );
}
CliApplication::CliApplication( int argc, char *argv[] )
{
this->argc = argc;
this->argv = argv;
}
int CliApplication::exec()
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("import,i", po::value< std::vector<std::string> >(), "import list of slices")
("project,p", po::value< std::string >(), "load project file (*.tms)")
("gpu", po::value< bool >()->implicit_value(true), "enable/disable GPU optimizations")
("align,a", po::value< int >()->implicit_value(1), "align slices")
("segment,s", po::value< int >()->implicit_value(1), "segment the 3D volume")
("display,d", "display the result in a basic GUI")
("output,o", po::value< std::string >(), "output folder to resulting *.mrc files")
("export,e", po::value< std::string >(), "output folder to export slice images")
;
po::positional_options_description p;
p.add("import", -1);
po::variables_map vm;
po::store(po::command_line_parser(this->argc, this->argv).
options(desc).positional(p).run(), vm);
po::notify(vm);
if ( vm.count("help") ) {
std::cout << "Usage: TomSeg [options]\n";
std::cout << desc;
return EXIT_SUCCESS;
}
if ( vm.count("import") && !vm.count("project") ) {
segManager = SegmentationManager();
std::vector<std::string> filenames = vm["import"].as< std::vector<std::string> >();
segManager.setSlices( filenames );
}
if( vm.count("project") ) {
segManager = SegmentationManager( vm["project"].as< std::string >() );
}
if( vm.count("gpu") ) {
segManager.setUseGPU( vm["gpu"].as< bool >() );
}
if( segManager.isEmpty() ) {
std::cerr << "Empty Project (--help)" << std::endl;
return EXIT_FAILURE;
}
if( vm.count("align") ) {
for( int i = 0; i < vm["align"].as< int >(); i++ ) {
segManager.alignSlices();
}
}
if( vm.count("segment") ) {
for( int i = 0; i < vm["segment"].as< int >(); i++ ) {
segManager.segment();
}
}
if( vm.count("output") ) {
segManager.exportResult( vm["output"].as< std::string >() );
}
if( vm.count("export") ) {
segManager.exportSliceImages( vm["export"].as< std::string >() );
}
if( vm.count("display") ) {
SliceDisplayer displayer(&segManager);
displayer.display();
}
return EXIT_SUCCESS;
}