-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step1_maskCTImage.cpp
204 lines (172 loc) · 3.5 KB
/
Step1_maskCTImage.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
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <time.h>
#include "CImg.h"
using namespace std;
using namespace cimg_library;
#define DMIN -20000
class Punkt
{
public:
int x;
int y;
Punkt(){}
Punkt(int X,int Y) {x=X;y=Y;}
bool operator == (const Punkt &p) const
{
if ((*this).x == p.x && (*this).y == p.y )
return true;
else
return false;
}
};
int main(int argc, char *argv[])
{
clock_t start, end;
double cpu_time_used;
int Ni=512,Nj=512;
CImg<signed short> dane(Ni,Nj);
dane.load_raw(argv[1],Ni,Nj,1,1,false,true);
// cerr << "Punkt (0,0) " << dane(0,0) << " " << dane(1,0) << " " << dane(0,1) << endl;
start = clock();
if (dane(0,0)>=0 && dane(1,0)>=0 && dane(0,1) >=0)
{
for(int i=0;i<Ni;i++)
for(int j=0;j<Nj;j++)
dane(i,j) -= 1024;
}
// dane.display();
int TH;
TH = -191; //Według metodyki
if (argc>=3) TH = atoi(argv[2]);
CImg<unsigned char> maska(Ni,Nj);
maska.fill(0);
for(int i=0;i<Ni;i++)
for(int j=0;j<Nj;j++)
{
if (dane(i,j)>TH) maska(i,j) = 255;
}
/******************************************/
queue<Punkt> kolejka;
int X=0,Y=0;
int BOX = 5;
for(X=Ni/2-BOX;X<=Ni/2+BOX;X++)
for(Y=Nj/2-BOX;Y<=Nj/2+BOX;Y++)
{
if (maska(X,Y)==0) continue;
maska(X,Y) = 1;
kolejka.push(Punkt(X,Y));
}
if (kolejka.size()==0)
{
// cerr << "Kolejka pusta!!!" << endl;
getchar();
}
// cerr << "kolejka zainicjowana" << endl;
int licz = 0;
while(kolejka.size())
{
licz++;
// if (licz%10000==0) cerr << "Licznik =" << kolejka.size() << endl;
Punkt p = kolejka.front();
kolejka.pop();
int I = p.x;
int J = p.y;
for(int i1=-1;i1<=1;i1++)
for(int j1=-1;j1<=1;j1++)
{
if (abs(i1)+abs(j1)>=1 && I+i1>=0 && I+i1<Ni && J+j1>=0 && J+j1<Nj)
{
if (maska(I+i1,J+j1)==255)
{
maska(I+i1,J+j1) = 1;
kolejka.push(Punkt(I+i1,J+j1));
}
}
}
}
for(int i=0;i<Ni;i++)
for(int j=0;j<Nj;j++)
{
if (maska(i,j)!=1) maska(i,j) = 0;
else maska(i,j)=255;
}
// maska.display();
for(X=0;X<=BOX;X++)
for(Y=0;Y<=BOX;Y++)
{
if (maska(X,Y)!=0) continue;
maska(X,Y) = 128;
kolejka.push(Punkt(X,Y));
}
for(X=Ni-BOX;X<Ni;X++)
for(Y=0;Y<=BOX;Y++)
{
if (maska(X,Y)!=0) continue;
maska(X,Y) = 128;
kolejka.push(Punkt(X,Y));
}
for(X=0;X<=BOX;X++)
for(Y=Nj-BOX;Y<Nj;Y++)
{
if (maska(X,Y)!=0) continue;
maska(X,Y) = 128;
kolejka.push(Punkt(X,Y));
}
for(X=Ni-BOX;X<Ni;X++)
for(Y=Nj-BOX;Y<Nj;Y++)
{
if (maska(X,Y)!=0) continue;
maska(X,Y) = 128;
kolejka.push(Punkt(X,Y));
}
if (kolejka.size()==0)
{
// cerr << "Kolejka pusta!!!" << endl;
getchar();
}
// cerr << "kolejka zainicjowana" << endl;
licz = 0;
while(kolejka.size())
{
licz++;
// if (licz%10000==0) cerr << "Licznik =" << kolejka.size() << endl;
Punkt p = kolejka.front();
kolejka.pop();
int I = p.x;
int J = p.y;
for(int i1=-1;i1<=1;i1++)
for(int j1=-1;j1<=1;j1++)
{
if (abs(i1)+abs(j1)>=1 && I+i1>=0 && I+i1<Ni && J+j1>=0 && J+j1<Nj)
{
if (maska(I+i1,J+j1)==0)
{
maska(I+i1,J+j1) = 128;
kolejka.push(Punkt(I+i1,J+j1));
}
}
}
}
for(int i=0;i<Ni;i++)
for(int j=0;j<Nj;j++)
{
if (maska(i,j)==128) maska(i,j) = 0;
else maska(i,j)=255;
}
// maska.display();
for(int i=0;i<Ni;i++)
for(int j=0;j<Nj;j++)
{
if (!maska(i,j)) dane(i,j) = DMIN;
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
cerr << cpu_time_used << endl;
char name[1000];
sprintf(name,"maska_%s",argv[1]);
dane.save_raw(name);
}