-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathAnnotationToMask.cpp
More file actions
156 lines (150 loc) · 6.4 KB
/
AnnotationToMask.cpp
File metadata and controls
156 lines (150 loc) · 6.4 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
#include "AnnotationToMask.h"
#include "AnnotationList.h"
#include "Annotation.h"
#include "AnnotationGroup.h"
#include "multiresolutionimageinterface/MultiResolutionImageWriter.h"
#include "core/Box.h"
#include "core/ProgressMonitor.h"
#include "core/PathologyEnums.h"
#include <algorithm>
void AnnotationToMask::setProgressMonitor(ProgressMonitor* monitor) {
_monitor = monitor;
}
void AnnotationToMask::convert(const std::shared_ptr<AnnotationList>& annotationList, const std::string& maskFile, const std::vector<unsigned long long>& dimensions, const std::vector<double>& spacing, const std::map<std::string, int> nameToLabel, const std::vector<std::string> nameOrder) const {
bool hasGroups = !annotationList->getGroups().empty();
std::vector<std::shared_ptr<Annotation> > annotations = annotationList->getAnnotations();
for (auto annotation = annotations.begin(); annotation != annotations.end(); ++annotation) {
if (!(*annotation)->isClockwise()) {
std::vector<Point> coords = (*annotation)->getCoordinates();
std::reverse(coords.begin(), coords.end());
(*annotation)->setCoordinates(coords);
}
}
if (!nameOrder.empty() && !nameToLabel.empty()) {
std::vector<std::shared_ptr<Annotation> > unorderedAnnotations = annotations;
annotations.clear();
for (unsigned int i = 0; i < nameOrder.size(); ++i) {
std::string currentName = nameOrder[i];
for (std::vector<std::shared_ptr<Annotation> >::iterator it = unorderedAnnotations.begin(); it != unorderedAnnotations.end(); ++it) {
bool matchesName = false;
if (hasGroups) {
if ((*it)->getGroup()) {
if ((*it)->getGroup()->getName() == currentName) {
matchesName = true;
}
}
}
else {
if ((*it)->getName() == currentName) {
matchesName = true;
}
}
if (matchesName) {
annotations.push_back((*it));
}
}
}
}
MultiResolutionImageWriter writer;
if (_monitor) {
writer.setProgressMonitor(_monitor);
}
if (writer.openFile(maskFile) == 0) {
writer.setColorType(pathology::ColorType::Monochrome);
writer.setCompression(pathology::Compression::LZW);
writer.setTileSize(512);
writer.setDataType(pathology::DataType::UChar);
writer.setInterpolation(pathology::Interpolation::NearestNeighbor);
std::vector<double> spacing_copy(spacing);
writer.setSpacing(spacing_copy);
writer.writeImageInformation(dimensions[0], dimensions[1]);
unsigned char* buffer = new unsigned char[512 * 512];
for (unsigned long long ty = 0; ty < dimensions[1]; ty += 512) {
for (unsigned long long tx = 0; tx < dimensions[0]; tx += 512) {
std::fill(buffer, buffer + 512 * 512, 0);
for (std::vector<std::shared_ptr<Annotation> >::const_iterator annotation = annotations.begin(); annotation != annotations.end(); ++annotation) {
if (!nameToLabel.empty() && !(*annotation)->getGroup() && hasGroups) {
continue;
}
std::string nm = (*annotation)->getName();
std::vector<Point> coords = (*annotation)->getCoordinates();
std::vector<Point> bbox = (*annotation)->getImageBoundingBox();
if (!coords.empty()) {
coords.push_back(coords[0]);
}
int label = 1;
if (!nameToLabel.empty()) {
std::map<std::string, int>::const_iterator it;
if (hasGroups) {
it = nameToLabel.find((*annotation)->getGroup()->getName());
}
else {
it = nameToLabel.find(nm);
}
if (it != nameToLabel.end()) {
label = it->second;
}
else {
label = 0;
}
}
for (unsigned int y = 0; y < 512; ++y) {
if (ty + y >= dimensions[1]) {
break;
}
if (ty + y > bbox[0].getY() && ty + y < bbox[1].getY()) {
for (unsigned int x = 0; x < 512; ++x) {
if (tx + x > bbox[0].getX() && tx + x < bbox[1].getX()) {
if (tx + x >= dimensions[0]) {
break;
}
int in_poly = wn_PnPoly(Point(static_cast<float>(tx + x), static_cast<float>(ty + y)), coords) != 0 ? 1 : 0;
if (nameOrder.empty()) {
buffer[y * 512 + x] = in_poly * label > buffer[y * 512 + x] ? in_poly * label : buffer[y * 512 + x];
}
else if (in_poly) {
buffer[y * 512 + x] = in_poly * label;
}
}
}
}
}
}
writer.writeBaseImagePart((void*)buffer);
}
}
writer.finishImage();
delete[] buffer;
}
}
int AnnotationToMask::cn_PnPoly(const Point& P, const std::vector<Point>& V) const {
int cn = 0; // the crossing number counter
// loop through all edges of the polygon
for (int i = 0; i < V.size() - 1; i++) { // edge from V[i] to V[i+1]
if (((V[i].getY() <= P.getY()) && (V[i + 1].getY() > P.getY())) // an upward crossing
|| ((V[i].getY() > P.getY()) && (V[i + 1].getY() <= P.getY()))) { // a downward crossing
// compute the actual edge-ray intersect x-coordinate
float vt = (float)(P.getY() - V[i].getY()) / (V[i + 1].getY() - V[i].getY());
if (P.getX() < V[i].getX() + vt * (V[i + 1].getX() - V[i].getX())) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x
}
}
return (cn & 1);
}
int AnnotationToMask::wn_PnPoly(const Point& P, const std::vector<Point>& V) const {
int wn = 0; // the winding number counter
// loop through all edges of the polygon
for (int i = 0; i<V.size() - 1; i++) { // edge from V[i] to V[i+1]
if (V[i].getY() <= P.getY()) { // start y <= P.y
if (V[i + 1].getY() > P.getY()) // an upward crossing
if (isLeft(V[i], V[i + 1], P) > 0) // P left of edge
++wn; // have a valid up intersect
}
else { // start y > P.y (no test needed)
if (V[i + 1].getY() <= P.getY()) // a downward crossing
if (isLeft(V[i], V[i + 1], P) < 0) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
}