-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMorphology_Labeling.cpp
More file actions
360 lines (308 loc) · 11 KB
/
Morphology_Labeling.cpp
File metadata and controls
360 lines (308 loc) · 11 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
KImageGray KImageGray::BinaryDilate(const int& nType, const int nSize){
KImageGray igOut = *this;
int d = nSize / 2;
if(nType == _WHITE)
{
for(int i=d,ii=Row()-(d+1); ii>=d; i++,ii--)
for(int j=d,jj=Col()-(d+1); jj>=d; j++,jj--)
{
try{
if(_ppA[i][j] == 0)
for(int p=-d;p<=d;p++)
for(int q=-d;q<=d;q++)
if(_ppA[i+p][j+q]){
igOut._ppA[i][j] = 255;
throw 1;
}
}
catch(int res){
continue;
}
}
}
else if(nType == _BLACK)
{
for(int i=d,ii=Row()-(d+1); ii>=d; i++,ii--)
for(int j=d,jj=Col()-(d+1); jj>=d; j++,jj--)
{
try{
if(_ppA[i][j])
for(int p=-d;p<=d;p++)
for(int q=-d;q<=d;q++)
if(_ppA[i+p][j+q]==0){
igOut._ppA[i][j] = 0;
throw 1;
}
}
catch(int res){
continue;
}
}
}
return igOut;
}
KImageGray KImageGray::BinaryErode(const int& nType, const int nSize){
KImageGray igOut = *this;
int d = nSize / 2;
if(nType == _WHITE)
{
for(int i=d,ii=Row()-(d+1); ii>=d; i++,ii--)
for(int j=d,jj=Col()-(d+1); jj>=d; j++,jj--)
{
try{
if(_ppA[i][j])
for(int p=-d;p<=d;p++)
for(int q=-d;q<=d;q++)
if(_ppA[i+p][j+q]==0){
igOut._ppA[i][j] = 0;
throw 1;
}
}
catch(int res){
continue;
}
}
}
else if(nType == _BLACK)
{
for(int i=d,ii=Row()-(d+1); ii>=d; i++,ii--)
for(int j=d,jj=Col()-(d+1); jj>=d; j++,jj--)
{
try{
if(_ppA[i][j]==0)
for(int p=-d;p<=d;p++)
for(int q=-d;q<=d;q++)
if(_ppA[i+p][j+q]){
igOut._ppA[i][j] = 255;
throw 1;
}
}
catch(int res){
continue;
}
}
}
return igOut;
}
KArray<unsigned short> KImageGray::Neighbor_4(int* blobCnt, int* maxId){
using KImageWord = KArray<unsigned short>;
using KBlob = std::vector<KPOINT>;
using KBlobList = std::vector<KBlob*>;
int nRow = Row();
int nCol = Col();
KBlobList lBlobList;
KImageWord iwColor(nRow, nCol);
unsigned char uCur;
unsigned char uLeft, uUp;
unsigned short wLeftColor, wUpColor;
int BlobCnt = 0;
for(int i=0;i<nRow;i++){
for(int j=0;j<nCol;j++){
uCur = _ppA[i][j];
if(uCur==0) continue;
uLeft = j==0?0:_ppA[i][j-1];
uUp = i==0?0:_ppA[i-1][j];
wLeftColor = j==0?0:iwColor[i][j-1];
wUpColor = i==0?0:iwColor[i-1][j];
//if new blob is obtained
if(uUp==0 && uLeft==0){
//blob creation
KBlob* opBlob = new KBlob;
lBlobList.push_back(opBlob);
//store the pixel coord.
opBlob->push_back(KPOINT(i,j));
//write the blob color
iwColor[i][j] = lBlobList.size();
BlobCnt++;
}
else if(uUp && uLeft==0){
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
else if(uUp==0 && uLeft){
iwColor[i][j] = wLeftColor;
lBlobList[wLeftColor-1]->push_back(KPOINT(i,j));
}
else{
if(wLeftColor == wUpColor){
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
else{
for(auto& kp:*(lBlobList[wLeftColor-1])){
iwColor[kp.nX][kp.nY] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(kp.nX, kp.nY));
}
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
BlobCnt--;
}
}
}
}
if(blobCnt != nullptr) *blobCnt = BlobCnt;
if(maxId != nullptr) *maxId = lBlobList.size();
for(auto&x:lBlobList)
delete x;
return iwColor;
}
KArray<unsigned short> KImageGray::Neighbor_8(int* blobCnt, int* maxId){
using KImageWord = KArray<unsigned short>;
using KBlob = std::vector<KPOINT>;
using KBlobList = std::vector<KBlob*>;
int nRow = Row();
int nCol = Col();
KBlobList lBlobList;
KImageWord iwColor(nRow, nCol);
unsigned char uCur;
unsigned char uLeft, uUp, uDiag;
unsigned short wLeftColor, wUpColor, wDiagColor;
int BlobCnt = 0;
for(int i=0;i<nRow;i++){
for(int j=0;j<nCol;j++){
uCur = _ppA[i][j];
if(uCur==0) continue;
uLeft = j==0?0:_ppA[i][j-1];
uUp = i==0?0:_ppA[i-1][j];
uDiag = (i==0||j==0)?0:_ppA[i-1][j-1];
wLeftColor = j==0?0:iwColor[i][j-1];
wUpColor = i==0?0:iwColor[i-1][j];
wDiagColor = (i==0||j==0)?0:iwColor[i-1][j-1];
//if new blob is obtained
if(uUp==0 && uLeft==0 && uDiag==0){
//blob creation
KBlob* opBlob = new KBlob;
lBlobList.push_back(opBlob);
//store the pixel coord.
opBlob->push_back(KPOINT(i,j));
//write the blob color
iwColor[i][j] = lBlobList.size();
BlobCnt++;
}
else if(uUp && uLeft==0 && uDiag==0){
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
else if(uUp==0 && uLeft && uDiag==0){
iwColor[i][j] = wLeftColor;
lBlobList[wLeftColor-1]->push_back(KPOINT(i,j));
}
else if(uUp==0 && uLeft==0 && uDiag){
iwColor[i][j] = wDiagColor;
lBlobList[wDiagColor-1]->push_back(KPOINT(i,j));
}
else if(uLeft && uUp && uDiag==0){
if(wLeftColor==wUpColor){
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
else{
//merge left to up
for(auto& kp:*(lBlobList[wLeftColor-1])){
iwColor[kp.nX][kp.nY] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(kp.nX, kp.nY));
}
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
BlobCnt--;
}
}
else if(uLeft && uDiag && uUp==0){
iwColor[i][j] = wLeftColor;
lBlobList[wLeftColor-1]->push_back(KPOINT(i,j));
}
else if(uUp && uDiag && uLeft==0){
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
else{ //uUp == uLeft == uDiag
iwColor[i][j] = wUpColor;
lBlobList[wUpColor-1]->push_back(KPOINT(i,j));
}
}
}
if(blobCnt != nullptr) *blobCnt = BlobCnt;
if(maxId != nullptr) *maxId = lBlobList.size();
for(auto&x:lBlobList)
delete x;
return iwColor;
}
void MainFrame::on_checkBox3by3_clicked()
{
ui->checkBox5by5->setChecked(false);
ui->checkBox3by3->setChecked(true);
}
void MainFrame::on_checkBox5by5_clicked()
{
ui->checkBox3by3->setChecked(false);
ui->checkBox5by5->setChecked(true);
}
//===========================================//
void MainFrame::on_pushDilationErosion_clicked()
{
KImageGray igMain;
if(_q_pFormFocused != 0 && _q_pFormFocused->ImageGray().Address() && _q_pFormFocused->ID() == "OPEN")
igMain = _q_pFormFocused->ImageGray();
else
return;
//Ostu Thresholding
KImageGray igBin;
KBINARIZATION_OUTPUT* opBinOutput = KHisto().Ostu(&igMain, &igBin);
int nSize = (ui->checkBox3by3->isChecked()?3:5); //체크박스는 무조건 둘 중 하나는 되어있는 상태
KImageGray dilatedImg = igBin.BinaryDilate(_WHITE, nSize); //BinaryDilate
KImageGray erodedImg = igBin.BinaryErode(_WHITE, nSize); //BinaryErode
ImgForm<KImageGray>::Create(*this, "Dilated", dilatedImg);
ImgForm<KImageGray>::Create(*this, "Eroded", erodedImg);
}
void MainFrame::on_checkBox4N_clicked()
{
ui->checkBox8N->setChecked(false);
ui->checkBox4N->setChecked(true);
}
void MainFrame::on_checkBox8N_clicked()
{
ui->checkBox4N->setChecked(false);
ui->checkBox8N->setChecked(true);
}
void MainFrame::on_pushLabel_clicked()
{
KImageGray igMain;
if(_q_pFormFocused != 0 && _q_pFormFocused->ImageGray().Address() && (_q_pFormFocused->ID() == "Dilated" || _q_pFormFocused->ID() == "Eroded"))
igMain = _q_pFormFocused->ImageGray();
else
return;
//Neighbor 실행
int blobCnt, maxId;
KArray<unsigned short> KWords;
const char* ID;
if(ui->checkBox4N->isChecked()){ //4-Neighbor가 체크되어 있다면
KWords = igMain.Neighbor_4(&blobCnt, &maxId);
ID = "4-Neighbor";
}
else{ //8-Neighbor가 체크되어 있다면
KWords = igMain.Neighbor_8(&blobCnt, &maxId);
ID = "8-Neighbor";
}
//Blob 별 색 부여
KImageColor oImg(igMain.Row(), igMain.Col());
vector<KCOLOR32> colors;
colors.push_back(KCOLOR32(0,0,0));
int dR = 97, dG = 83, dB = 67; //소수를 이용하여 색이 최대한 안 겹치도록 함
for(int i = 1;i<=maxId;i++){ //Blob 라벨 별 RGB 생성
int r = (i * dR) % 185 + 70;
int g = (i * dG) % 185 + 70;
int b = (i * dB) % 185 + 70;
colors.push_back(KCOLOR32(r, g, b));
}
for(int i = KWords.Row() -1; i>=0;i--){ //Color Image 객체 생성
for(int j = KWords.Col()-1;j>=0;j--){
oImg._ppA[i][j].r = colors[KWords[i][j]].r;
oImg._ppA[i][j].g = colors[KWords[i][j]].g;
oImg._ppA[i][j].b = colors[KWords[i][j]].b;
}
}
if(ui->listWidget->isVisible() == false)
on_buttonShowList_clicked();
ui->listWidget->addItem(QString("Number of labels ---> " + QString::number(blobCnt)));
ImgForm<KImageColor>::Create(*this, ID, oImg);
}