-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTRUnet_Detection.cpp
2922 lines (2590 loc) · 85.6 KB
/
CTRUnet_Detection.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "CTRUnet_Detection.h"
void CTRUnet_Detection::Lungmask_clean(cv::Mat& result_img)
{
int width = result_img.cols;
int height = result_img.rows;
Matrix<unsigned short> matmasklungcopy(width, height, result_img.data);
for (int i = 0; i < 512 * 512; i++)
{
if ((&result_img.at<unsigned char>(0, 0))[i] != 0)
{
matmasklungcopy.pdata[i] = 1;
}
}
//连通域去除
Erase_holl(matmasklungcopy, 0.05);//消除连通域小于0.05的区域
for (int i = 0; i < 512 * 512; i++)
{
if (matmasklungcopy.pdata[i] != 0)
{
(&result_img.at<unsigned char>(0, 0))[i] = 255;
}
else
{
(&result_img.at<unsigned char>(0, 0))[i] = 0;
}
}
}
int CTRUnet_Detection::CTR_main(unsigned char* presult_img, int downsampwidth, int downsampheight, int params[])
{
int imgwidth = params[0];
int imgheight = params[1];
//数据转换。
Matrix<unsigned short> matmasklung(downsampwidth, downsampheight);
//构建左右肺野区域。
Matrix<unsigned short> matleftlungmask(downsampwidth, downsampheight);
Matrix<unsigned short> matrightlungmask(downsampwidth, downsampheight);
for (int i = 0; i < matmasklung.Matrix_length(); i++)
{
if (presult_img[i] != 0)
{
matmasklung.pdata[i] = 1;
}
}
int signum = 1;
vector<int> xvec_left;
vector<int> yvec_left;
vector<int> xvec_right;
vector<int> yvec_right;
signum = LeftRightSegimg(matmasklung, matleftlungmask, matrightlungmask, xvec_left, yvec_left, xvec_right, yvec_right);
if (signum != 1)
{
return 0;
}
//
//左侧轮廓的顶点位置。
int lefttop_Y = *min_element(yvec_left.begin(), yvec_left.end());
int lefttop_X = xvec_left[min_element(yvec_left.begin(), yvec_left.end())- yvec_left.begin()];
//左侧轮廓的底点位置。
vector<int> tempribX;
vector<int> tempribY; //
for (int i = 0; i < xvec_left.size(); i++)
{
if (xvec_left[i] < lefttop_X)
{
tempribX.push_back(xvec_left[i]);
tempribY.push_back(yvec_left[i]);
}
}
int leftbot_Y = *max_element(tempribY.begin(), tempribY.end());
int leftbot_X = tempribX[max_element(tempribY.begin(), tempribY.end()) - tempribY.begin()];
//右侧轮廓的顶点位置
int righttop_Y= *min_element(yvec_right.begin(), yvec_right.end());
int righttop_X = xvec_right[min_element(yvec_right.begin(), yvec_right.end())- yvec_right.begin()];
//右侧轮廓的底点位置
tempribX.clear();
tempribY.clear();
for (int i = 0; i < xvec_right.size(); i++)
{
if (xvec_right[i] > righttop_X)
{
tempribX.push_back(xvec_right[i]);
tempribY.push_back(yvec_right[i]);
}
}
int rightbot_Y = *max_element(tempribY.begin(), tempribY.end());
int rightbot_X = tempribX[max_element(tempribY.begin(), tempribY.end()) - tempribY.begin()];
vector<int> xvec_leftrib;
vector<int> yvec_leftrib;
vector<int> xvec_leftdiaph;
vector<int> yvec_leftdiaph;//左侧分割
signum = RibDiaphSeg_left(matleftlungmask, xvec_left, yvec_left, xvec_leftrib, yvec_leftrib, xvec_leftdiaph, yvec_leftdiaph);
if (signum != 1)
{
return 0;
}
vector<int> xvec_rightrib;
vector<int> yvec_rightrib;
vector<int> xvec_rightdiaph;
vector<int> yvec_rightdiaph;//右侧分割
signum = RibDiaphSeg_right(matrightlungmask, xvec_right, yvec_right, xvec_rightrib, yvec_rightrib, xvec_rightdiaph, yvec_rightdiaph);
if (signum != 1)
{
return 0;
}
//寻找中心分割线
int Rib_Leftind = *max_element(xvec_right.begin(), xvec_right.end());//找到最左侧的肋骨位置
int Rib_Rightind = *min_element(xvec_left.begin(), xvec_left.end());//找到最右侧的肋骨位置
//心胸比中间线的X方向分割线位置
int Midseg_X = (Rib_Leftind + Rib_Rightind) / 2;
//左侧横纵隔膜边界排序
vector<int> Xdiaph_Leftorder;
vector<int> Ydiaph_Leftorder;
Matrix<unsigned short> matLableimg_left(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_left, xvec_leftdiaph, yvec_leftdiaph, Xdiaph_Leftorder, Ydiaph_Leftorder);//
//cv::Mat imgmat1(downsampheight, downsampwidth, CV_16UC1, matLableimg_left.pdata);
//cv::Mat garay1;
//cv::threshold(imgmat1, garay1, 0, 65535, cv::THRESH_BINARY);//二值化
//cv::imshow("左侧横纵", garay1);
//cv::waitKey(0);
//右侧横纵隔膜边界排序
vector<int> Xdiaph_Rightorder;
vector<int> Ydiaph_Rightorder;
Matrix<unsigned short> matLableimg_right(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_right, xvec_rightdiaph, yvec_rightdiaph, Xdiaph_Rightorder, Ydiaph_Rightorder);//
//cv::Mat imgmat2(downsampheight, downsampwidth, CV_16UC1, matLableimg_right.pdata);
//cv::Mat garay2;
//cv::threshold(imgmat2, garay2, 0, 65535, cv::THRESH_BINARY);//二值化
//cv::imshow("右侧横纵", garay2);
//cv::waitKey(0);
//判断图像的左侧是左肺还是右肺,边界点包含于肺野点中,只需取其中一个点判断即可
int diaph_Leftind = 0; //左侧横隔肌有效点的位置 。 //计算获取拐点
int hart_Leftind = 0; //左侧心脏有效点的位置
int leftmaxdisind;//左侧拐点的最大距离的索引位置
int diaph_Rightind = 0; //右侧有效点的位置
int hart_Rightind = 0; //右侧有效点的位置
int rightmaxdisind;//右侧拐点的最大的索引位置
int deta_Y = 20;
//判断左右肺部区域
int LeftdisX = 0;
int RightdisX = 0;
Matrix<unsigned short> matLeftdis(matleftlungmask.width, matleftlungmask.height);
Matrix<unsigned short> matRightdis(matrightlungmask.width, matrightlungmask.height);
LeftRight_dist(LeftdisX, RightdisX, Midseg_X, matLeftdis, matRightdis,
xvec_leftrib, yvec_leftrib, xvec_rightrib, yvec_rightrib,
Xdiaph_Leftorder, Ydiaph_Leftorder, Xdiaph_Rightorder, Ydiaph_Rightorder);
if (LeftdisX < RightdisX)//(matmasklung.pdata[index] == 1)//等于1为右肺,左侧对应右肺
{
//先算右肺
vector<float> nvect_leftratio;//右肺部每个点的斜率值,leftmaxdisind为右肺的斜率
GetHeartDiaphPoint_rightlung(Xdiaph_Leftorder, Ydiaph_Leftorder, leftmaxdisind, nvect_leftratio, 1);//图像左侧传1,叉乘为正
//右肺斜率对应的Y坐标
int leftmaxdis_Y = Ydiaph_Leftorder[leftmaxdisind];//左肺的拐点寻找就在此点的位置上下进行寻找
leftmaxdis_Y = leftmaxdis_Y - deta_Y;
//再算左肺
vector<float> nvect_rightratio;//左肺侧连通域的每个点的斜率值
GetHeartDiaphPoint_leftlung(Xdiaph_Rightorder, Ydiaph_Rightorder, rightmaxdisind, nvect_rightratio, leftmaxdis_Y, -1); //图像右侧传-1,叉乘为负
//根据垂距最大的那个点通过斜率找到心脏边缘位置和横隔肌边缘位置
Find_hartdiaphind_smallheart(Xdiaph_Leftorder, Ydiaph_Leftorder, nvect_leftratio, leftmaxdisind, hart_Leftind, diaph_Leftind, 1);//leftmaxdisind
//根据垂距最大的那个点通过斜率找到心脏边缘位置和横隔肌边缘位置
Find_hartdiaphind_largeheart(Xdiaph_Rightorder, Ydiaph_Rightorder, nvect_rightratio, rightmaxdisind, hart_Rightind, diaph_Rightind, -1);
}
else //if (matmasklung.pdata[index] == 2)//左侧对应左肺
{
//先算右肺
vector<float> nvect_rightratio;//右肺部每个点的斜率值,leftmaxdisind为右肺的斜率
GetHeartDiaphPoint_rightlung(Xdiaph_Rightorder, Ydiaph_Rightorder, rightmaxdisind, nvect_rightratio, -1);//图像右侧传-1
//右肺斜率对应的Y坐标
int rightmaxdis_Y = Ydiaph_Rightorder[rightmaxdisind];//左肺的拐点寻找就在此点的位置上下进行寻找
rightmaxdis_Y = rightmaxdis_Y - deta_Y;
//再算左肺
vector<float> nvect_leftratio;
GetHeartDiaphPoint_leftlung(Xdiaph_Leftorder, Ydiaph_Leftorder, leftmaxdisind, nvect_leftratio, rightmaxdis_Y, 1); //图像左侧传1,
//根据垂距最大的那个点通过斜率找到心脏边缘位置和横隔肌边缘位置
Find_hartdiaphind_smallheart(Xdiaph_Rightorder, Ydiaph_Rightorder, nvect_rightratio, rightmaxdisind, hart_Rightind, diaph_Rightind, -1);//leftmaxdisind
//根据垂距最大的那个点通过斜率找到心脏边缘位置和横隔肌边缘位置
Find_hartdiaphind_largeheart(Xdiaph_Leftorder, Ydiaph_Leftorder, nvect_leftratio, leftmaxdisind, hart_Leftind, diaph_Leftind, 1);
}
/*else
{
;
}*/
//左侧图像的心脏和横隔肌左侧位置:心脏点
int LefthartY = Ydiaph_Leftorder[hart_Leftind];
int LefthartX = Xdiaph_Leftorder[hart_Leftind];
//左侧图像横膈肌
int LeftdiaphY = Ydiaph_Leftorder[diaph_Leftind];
int LeftdiaphX = Xdiaph_Leftorder[diaph_Leftind];
//右侧图像的心脏和横膈肌的右侧位置:心脏
int RighthartY = Ydiaph_Rightorder[hart_Rightind];
int RighthartX = Xdiaph_Rightorder[hart_Rightind];
//右侧图像横膈肌
int RightdiaphY = Ydiaph_Rightorder[diaph_Rightind];
int RightdiaphX = Xdiaph_Rightorder[diaph_Rightind];
//左侧拐点
int LeftmaxdisY = Ydiaph_Leftorder[leftmaxdisind];
int LeftmaxdisX = Xdiaph_Leftorder[leftmaxdisind];
//右侧拐点
int RightmaxdisY = Ydiaph_Rightorder[rightmaxdisind];
int RightmaxdisX = Xdiaph_Rightorder[rightmaxdisind];
//左右两侧肋骨点的确认
if (LeftdisX < RightdisX)//(matmasklung.pdata[index] == 1)//左侧图像对应右肺
{
int coutnum = 0;
//左侧肋骨边界有效点检测
for (int i = 0; i < xvec_leftrib.size(); i++)
{
++coutnum;
if (yvec_leftrib[i] == Ydiaph_Leftorder[diaph_Leftind])//与右膈肌检测点的Y值相同的肋骨点
{
params[3] = yvec_leftrib[i];
params[4] = xvec_leftrib[i];
break;
}
}
if (coutnum == yvec_leftrib.size())
{
params[3] = yvec_leftrib.back();
params[4] = xvec_leftrib.back();
}
//右侧肋骨边界有效点检测
int leftribeffect = Ydiaph_Leftorder[diaph_Leftind];
vector<int> rightribX;
vector<int> rightribY;
for (int i = 0; i < yvec_rightrib.size(); i++)
{
if (yvec_rightrib[i] == leftribeffect)//与右膈肌检测点的Y值相同的肋骨点
{
rightribY.push_back(yvec_rightrib[i]);
rightribX.push_back(xvec_rightrib[i]);
}
}
if (rightribY.empty())
{
params[5] = yvec_rightrib.back();
params[6] = xvec_rightrib.back();
}
else
{
int maxind = 0;
maxind = max_element(rightribX.begin(), rightribX.end()) - rightribX.begin();
params[5] = rightribY[maxind];
params[6] = rightribX[maxind];
}
}
else//出现反向检测
{
//左侧肋骨边界有效点检测
vector<int> leftribX;
vector<int> leftribY;
for (int i = 0; i < yvec_leftrib.size(); i++)
{
if (yvec_leftrib[i] == Ydiaph_Rightorder[diaph_Rightind])//与右膈肌检测点的Y值相同的肋骨点
{
leftribY.push_back(yvec_leftrib[i]);
leftribX.push_back(xvec_leftrib[i]);
break;
}
}
if (leftribY.empty())
{
params[3] = yvec_leftrib.back();
params[4] = xvec_leftrib.back();
}
else
{
int maxind = 0;
maxind = max_element(leftribX.begin(), leftribX.end()) - leftribX.begin();
params[3] = leftribY[maxind];
params[4] = leftribX[maxind];
}
int coutnum = 0;
//右侧肋骨边界有效点检测
for (int i = 0; i < yvec_rightrib.size(); i++)
{
++coutnum;
if (yvec_rightrib[i] == Ydiaph_Rightorder[diaph_Rightind])//与右膈肌检测点的Y值相同的肋骨点
{
params[5] = yvec_rightrib[i];
params[6] = xvec_rightrib[i];
break;
}
}
if (coutnum == yvec_rightrib.size())
{
params[5] = yvec_rightrib.back();
params[6] = xvec_rightrib.back();
}
}
params[7] = LefthartY;//左心脏Y坐标
params[8] = LefthartX;//左心脏X坐标
params[9] = RighthartY;//右心脏Y坐标
params[10] = RighthartX;//右心脏X坐标
params[11] = LeftdiaphY;//左横膈肌Y坐标
params[12] = LeftdiaphX;//左横膈肌X坐标
params[13] = RightdiaphY;//右横膈肌Y坐标
params[14] = RightdiaphX;//右横膈肌X坐标
params[15] = LeftmaxdisY;//左侧拐点y坐标
params[16] = LeftmaxdisX;//左侧拐点x坐标
params[17] = RightmaxdisY;//右侧拐点Y坐标
params[18] = RightmaxdisX;//右侧拐点x坐标
params[19]= lefttop_Y;//左侧肺野顶点Y坐标
params[20]= lefttop_X;//左侧肺野顶点x坐标
params[21]= righttop_Y;//右侧肺野顶点Y坐标
params[22]= righttop_X;//右侧肺野顶点x坐标
params[23] = leftbot_Y;//左侧肺野底点Y坐标
params[24] = leftbot_X;//左侧肺野底点x坐标
params[25] = rightbot_Y;//右侧肺野底点Y坐标
params[26] = rightbot_X;//右侧肺野底点x坐标
// 映射原始图心胸比及各个特殊点的坐标。映射到原始图像
MaptoOrg_CTR(imgwidth, imgheight, downsampwidth, downsampheight, params);
return 1;
}
//左侧图像的膈肌点为128,右侧图像的膈肌点为255
int CTRUnet_Detection::Diaphragm_detect(unsigned char* presult_img, int downsampwidth, int downsampheight,int params[],
unsigned short* pdiaph_Line_imgorg)//downsampwidth, downsampheight,
{
int imgwidth = params[0];
int imgheight = params[1];
unsigned char* pdiaph_Line_img = new unsigned char[512 * 512](); //下采样的输出地址//512*512的图像淹模
//数据转换
Matrix<unsigned short> matmasklung(downsampwidth, downsampheight);
//构建左右肺野区域
Matrix<unsigned short> matleftlungmask(downsampwidth, downsampheight);
Matrix<unsigned short> matrightlungmask(downsampwidth, downsampheight);
for (int i = 0; i < matmasklung.Matrix_length(); i++)
{
if (presult_img[i] != 0)
{
matmasklung.pdata[i] = 1;
}
}
int signum = 1;
vector<int> xvec_left;
vector<int> yvec_left;
vector<int> xvec_right;
vector<int> yvec_right;
signum = LeftRightSegimg(matmasklung, matleftlungmask, matrightlungmask, xvec_left, yvec_left, xvec_right, yvec_right);//只是区别图像的左侧和右侧的肺野
if (signum != 1)
{
return 0;
}
//左侧轮廓的顶点位置
int lefttop_Y = *min_element(yvec_left.begin(), yvec_left.end());
int lefttop_X = xvec_left[min_element(yvec_left.begin(), yvec_left.end()) - yvec_left.begin()];
//右侧轮廓的顶点位置
int righttop_Y = *min_element(yvec_right.begin(), yvec_right.end());
int righttop_X = xvec_right[min_element(yvec_right.begin(), yvec_right.end()) - yvec_right.begin()];
vector<int> xvec_leftrib;
vector<int> yvec_leftrib;
vector<int> xvec_leftdiaph;
vector<int> yvec_leftdiaph;//左侧分割
signum = RibDiaphSeg_left(matleftlungmask, xvec_left, yvec_left, xvec_leftrib, yvec_leftrib, xvec_leftdiaph, yvec_leftdiaph);
if (signum != 1)
{
return 0;
}
vector<int> xvec_rightrib;
vector<int> yvec_rightrib;
vector<int> xvec_rightdiaph;
vector<int> yvec_rightdiaph;//右侧分割
signum = RibDiaphSeg_right(matrightlungmask, xvec_right, yvec_right, xvec_rightrib, yvec_rightrib, xvec_rightdiaph, yvec_rightdiaph);
if (signum != 1)
{
return 0;
}
//寻找中心分割线
int Rib_Leftind = *max_element(xvec_right.begin(), xvec_right.end());// 找到最左侧的肋骨位置
int Rib_Rightind = *min_element(xvec_left.begin(), xvec_left.end());// 找到最右侧的肋骨位置
//心胸比中间线的X方向分割线位置
int Midseg_X = (Rib_Leftind + Rib_Rightind) / 2;
//左侧横纵隔膜边界排序
vector<int> Xdiaph_Leftorder;
vector<int> Ydiaph_Leftorder;
Matrix<unsigned short> matLableimg_left(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_left, xvec_leftdiaph, yvec_leftdiaph, Xdiaph_Leftorder, Ydiaph_Leftorder);//
//右侧横纵隔膜边界排序
vector<int> Xdiaph_Rightorder;
vector<int> Ydiaph_Rightorder;
Matrix<unsigned short> matLableimg_right(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_right, xvec_rightdiaph, yvec_rightdiaph, Xdiaph_Rightorder, Ydiaph_Rightorder);//
//判断图像的左侧是左肺还是右肺,边界点包含于肺野点中,只需取其中一个点判断即可
int diaph_Leftind = 0; //左侧横隔肌有效点的位置 。 //计算获取拐点
int hart_Leftind = 0; //左侧心脏有效点的位置
int leftmaxdisind;//左侧拐点的最大距离的索引位置
int diaph_Rightind = 0; //右侧有效点的位置
int hart_Rightind = 0; //右侧有效点的位置
int rightmaxdisind;//右侧拐点的最大的索引位置
int deta_Y = 20;
//判断左右肺部区域
int LeftdisX = 0;
int RightdisX = 0;
Matrix<unsigned short> matLeftdis(matleftlungmask.width, matleftlungmask.height);
Matrix<unsigned short> matRightdis(matrightlungmask.width, matrightlungmask.height);
LeftRight_dist(LeftdisX, RightdisX, Midseg_X, matLeftdis, matRightdis,
xvec_leftrib, yvec_leftrib, xvec_rightrib, yvec_rightrib,
Xdiaph_Leftorder, Ydiaph_Leftorder, Xdiaph_Rightorder, Ydiaph_Rightorder);
if (LeftdisX <= RightdisX)//(matmasklung.pdata[index] == 1)//等于1为右肺,左侧对应右肺
{
//先算右肺
vector<float> nvect_leftratio;//右肺部每个点的斜率值,leftmaxdisind为右肺的斜率
GetHeartDiaphPoint_rightlung(Xdiaph_Leftorder, Ydiaph_Leftorder, leftmaxdisind, nvect_leftratio, 1);//图像左侧传1,叉乘为正
//右肺斜率对应的Y坐标
int leftmaxdis_Y = Ydiaph_Leftorder[leftmaxdisind];//左肺的拐点寻找就在此点的位置上下进行寻找
leftmaxdis_Y = leftmaxdis_Y - deta_Y;
//再算左肺
vector<float> nvect_rightratio;//左肺侧连通域的每个点的斜率值
GetHeartDiaphPoint_leftlung(Xdiaph_Rightorder, Ydiaph_Rightorder, rightmaxdisind, nvect_rightratio, leftmaxdis_Y, -1); //图像右侧传-1,叉乘为负
}
else //if (matmasklung.pdata[index] == 2)//左侧对应左肺
{
//先算右肺
vector<float> nvect_rightratio;//右肺部每个点的斜率值,leftmaxdisind为右肺的斜率
GetHeartDiaphPoint_rightlung(Xdiaph_Rightorder, Ydiaph_Rightorder, rightmaxdisind, nvect_rightratio, -1);//图像右侧传-1
//右肺斜率对应的Y坐标
int rightmaxdis_Y = Ydiaph_Rightorder[rightmaxdisind];//左肺的拐点寻找就在此点的位置上下进行寻找
rightmaxdis_Y = rightmaxdis_Y - deta_Y;
//再算左肺
vector<float> nvect_leftratio;
GetHeartDiaphPoint_leftlung(Xdiaph_Leftorder, Ydiaph_Leftorder, leftmaxdisind, nvect_leftratio, rightmaxdis_Y, 1); //图像左侧传1,
}
//---------------------------对Ydiaph_Leftorder和Ydiaph_Rightorder所代表的的曲线和拐点做处理,分别获取左侧和右侧的膈肌点----------------
//左侧检测
memset(matLableimg_right.pdata, 0, sizeof(unsigned short)* matLableimg_right.Matrix_length());
memset(matLableimg_left.pdata, 0, sizeof(unsigned short)* matLableimg_left.Matrix_length());
for (int i = 0; i < Ydiaph_Leftorder.size(); i++)
{
matLableimg_left.pdata[Ydiaph_Leftorder[i] * downsampwidth + Xdiaph_Leftorder[i]] = 1;
}
int leftmaxdisindlimit = leftmaxdisind;
if (leftmaxdisind + 10 < Ydiaph_Rightorder.size())
{
leftmaxdisindlimit = leftmaxdisind + 10;
}
for (int i = leftmaxdisind - 15; i < leftmaxdisindlimit; i++)
{
matLableimg_left.pdata[Ydiaph_Leftorder[i] * downsampwidth + Xdiaph_Leftorder[i]] = 0;
}
bool TF = 0;
vector <int> labval;
vector <int> labind;
float sum1 = 0;
float sum2 = 0;
int labindvalue = 1;
Conectchose(matLableimg_left, matLableimg_right, labval, labind, TF);
//连通域判断选择获取膈肌点
if (labind.size() == 1)
{
for (size_t i = leftmaxdisind - 10; i < Ydiaph_Leftorder.size(); i++)
{
cv::Point pointright;
pointright.y = Ydiaph_Leftorder[i];
pointright.x = Xdiaph_Leftorder[i];
pdiaph_Line_img[Ydiaph_Leftorder[i] * downsampwidth + Xdiaph_Leftorder[i]] = 128;
}
}
else
{
for (int i = 0, step1 = 0; i < matLableimg_right.height; i++, step1 += downsampwidth)
{
for (int j = 0; j < matLableimg_right.width; j++)
{
if (matLableimg_right.pdata[step1 + j] == labind[labind.size() - 2])
{
sum1 = sum1 + i;
}
else if (matLableimg_right.pdata[step1 + j] == labind.back())
{
sum2 = sum2 + i;
}
}
}
sum1 = sum1 / labval[labval.size() - 2];
sum2 = sum2 / labval.back();
//判断数据大的值为所需要的值
if (sum1 > sum2)
{
labindvalue = labind[labind.size() - 2];
}
else
{
labindvalue = labind.back();
}
for (int i = 0, step1 = 0; i < matLableimg_right.height; i++, step1 += downsampwidth)
{
for (int j = 0; j < downsampwidth; j++)
{
if (matLableimg_right.pdata[step1 + j] == labindvalue)
{
cv::Point pointleft;
pointleft.y = i;
pointleft.x = j;
pdiaph_Line_img[i * downsampwidth + j] = 128;
}
}
}
}
//右侧检测
memset(matLableimg_right.pdata, 0, sizeof(unsigned short)* matLableimg_right.Matrix_length());
memset(matLableimg_left.pdata, 0, sizeof(unsigned short)* matLableimg_left.Matrix_length());
for (int i = 0; i < Ydiaph_Rightorder.size(); i++)
{
matLableimg_right.pdata[Ydiaph_Rightorder[i] * downsampwidth + Xdiaph_Rightorder[i]] = 1;
}
int rightmaxdisindlimit = rightmaxdisind;
if (rightmaxdisind + 10 < Ydiaph_Rightorder.size())
{
rightmaxdisindlimit = rightmaxdisind + 10;
}
for (int i = rightmaxdisind - 15; i < rightmaxdisindlimit; i++)
{
matLableimg_right.pdata[Ydiaph_Rightorder[i] * downsampwidth + Xdiaph_Rightorder[i]] = 0;
}
labval.clear();
labind.clear();
sum1 = 0;
sum2 = 0;
TF = 0;
//右侧连通域判断选择,获取膈肌点
Conectchose(matLableimg_right, matLableimg_left, labval, labind, TF);
if (labind.size() == 1)
{
for (size_t i = rightmaxdisind - 10; i < Ydiaph_Rightorder.size(); i++)
{
cv::Point pointright;
pointright.y = Ydiaph_Rightorder[i];
pointright.x = Xdiaph_Rightorder[i];
pdiaph_Line_img[Ydiaph_Rightorder[i] * downsampwidth + Xdiaph_Rightorder[i]] = 255;
}
}
else
{
for (int i = 0, step1 = 0; i < matLableimg_left.height; i++, step1 += downsampwidth)
{
for (int j = 0; j < downsampwidth; j++)
{
if (matLableimg_left.pdata[step1 + j] == labind[labind.size() - 2])
{
sum1 = sum1 + i;
}
else if (matLableimg_left.pdata[step1 + j] == labind.back())
{
sum2 = sum2 + i;
}
}
}
sum1 = sum1 / labval[labval.size() - 2];
sum2 = sum2 / labval.back();
//判断数据大的值为所需要的值
if (sum1 > sum2)
{
labindvalue = labind[labind.size() - 2];
}
else
{
labindvalue = labind.back();
}
for (int i = 0, step1 = 0; i < matLableimg_left.height; i++, step1 += downsampwidth)
{
for (int j = 0; j < downsampwidth; j++)
{
if (matLableimg_left.pdata[step1 + j] == labindvalue)
{
cv::Point pointleft;
pointleft.y = i;
pointleft.x = j;
pdiaph_Line_img[i * downsampwidth + j] = 255;
}
}
}
}
//映射到原始图
MaptoOrg_Diaph(imgwidth, imgheight, downsampwidth, downsampheight, pdiaph_Line_img, pdiaph_Line_imgorg);//映射到原始图的横膈膜边界
delete[] pdiaph_Line_img;
pdiaph_Line_img = nullptr;
return 1;
}
//左右肺的区分和面积计算,pleftrightlung_imgmask输出掩模中右肺1,左肺2
int CTRUnet_Detection::Lung_Areacalculate(unsigned char* presult_img, int downsampwidth, int downsampheight,
unsigned short* pleftrightlung_imgmaskorg, int params[])
{
int imgwidth = params[0];
int imgheight = params[1];
unsigned char* pleftrightlung_imgmask = new unsigned char[512 * 512](); //下采样左右图的区别掩模512-*512
//数据转换
Matrix<unsigned short> matmasklung(downsampwidth, downsampheight);
//构建左右肺野区域
Matrix<unsigned short> matleftlungmask(downsampwidth, downsampheight);
Matrix<unsigned short> matrightlungmask(downsampwidth, downsampheight);
for (int i = 0; i < matmasklung.Matrix_length(); i++)
{
if (presult_img[i] != 0)
{
matmasklung.pdata[i] = 1;
}
}
int signum = 1;
vector<int> xvec_left;
vector<int> yvec_left;
vector<int> xvec_right;
vector<int> yvec_right;
//matmasklung数组可以作为使用面积大小来区域左右肺的输出,面积大的为右肺置为1,面积小的为左肺置为2
signum = LeftRightSegimg(matmasklung, matleftlungmask, matrightlungmask, xvec_left, yvec_left, xvec_right, yvec_right);//只是区别图像的左侧和右侧的肺野
if (signum != 1)
{
return 0;
}
vector<int> xvec_leftrib;
vector<int> yvec_leftrib;
vector<int> xvec_leftdiaph;
vector<int> yvec_leftdiaph;//左侧分割肋缘和横纵隔膜。
signum = RibDiaphSeg_left(matleftlungmask, xvec_left, yvec_left, xvec_leftrib, yvec_leftrib, xvec_leftdiaph, yvec_leftdiaph);
if (signum != 1)
{
return 0;
}
vector<int> xvec_rightrib;
vector<int> yvec_rightrib;
vector<int> xvec_rightdiaph;
vector<int> yvec_rightdiaph;//右侧分割肋缘和横纵隔膜。
signum = RibDiaphSeg_right(matrightlungmask, xvec_right, yvec_right, xvec_rightrib, yvec_rightrib, xvec_rightdiaph, yvec_rightdiaph);
if (signum != 1)
{
return 0;
}
//寻找中心分割线
int Rib_Leftind = *max_element(xvec_right.begin(), xvec_right.end());// 找到最左侧的肋骨位置
int Rib_Rightind = *min_element(xvec_left.begin(), xvec_left.end());// 找到最右侧的肋骨位置
//心胸比中间线的X方向分割线位置
int Midseg_X = (Rib_Leftind + Rib_Rightind) / 2;
//左侧横纵隔膜边界排序
vector<int> Xdiaph_Leftorder;
vector<int> Ydiaph_Leftorder;
Matrix<unsigned short> matLableimg_left(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_left, xvec_leftdiaph, yvec_leftdiaph, Xdiaph_Leftorder, Ydiaph_Leftorder);//
//右侧横纵隔膜边界排序
vector<int> Xdiaph_Rightorder;
vector<int> Ydiaph_Rightorder;
Matrix<unsigned short> matLableimg_right(downsampwidth, downsampheight);
GetorderDiaph(matLableimg_right, xvec_rightdiaph, yvec_rightdiaph, Xdiaph_Rightorder, Ydiaph_Rightorder);//
//判断图像的左侧是左肺还是右肺,边界点包含于肺野点中,只需取其中一个点判断即可
int diaph_Leftind = 0; //左侧横隔肌有效点的位置 。 //计算获取拐点
int hart_Leftind = 0; //左侧心脏有效点的位置
int leftmaxdisind; //左侧拐点的最大距离的索引位置
int diaph_Rightind = 0; //右侧有效点的位置
int hart_Rightind = 0; //右侧有效点的位置
int rightmaxdisind; //右侧拐点的最大的索引位置
int deta_Y = 20;
//判断左右肺部区域
int LeftdisX = 0;
int RightdisX = 0;
Matrix<unsigned short> matLeftdis(matleftlungmask.width, matleftlungmask.height);
Matrix<unsigned short> matRightdis(matrightlungmask.width, matrightlungmask.height);
LeftRight_dist(LeftdisX, RightdisX, Midseg_X, matLeftdis, matRightdis,
xvec_leftrib, yvec_leftrib, xvec_rightrib, yvec_rightrib,
Xdiaph_Leftorder, Ydiaph_Leftorder, Xdiaph_Rightorder, Ydiaph_Rightorder);
int rightlungarea = 0;
int leftlungarea = 0;
//复制留用
//构建左右肺野区域
Matrix<unsigned short> matleftlungmask_copy(downsampwidth, downsampheight);
Matrix<unsigned short> matrightlungmask_copy(downsampwidth, downsampheight);
signum = LeftRightsideimg(matmasklung, matleftlungmask_copy, matrightlungmask_copy);
if (signum != 1)
{
return 0;
}
if (LeftdisX <= RightdisX)////左侧对应右肺,右肺1,左肺2
{
//先赋值右肺1
for (int i = 0; i < matleftlungmask_copy.Matrix_length(); i++)
{
if (matleftlungmask_copy.pdata[i]!=0)
{
pleftrightlung_imgmask[i] = 1;
rightlungarea++;
}
}
//再赋值左肺2
for (int i = 0; i < matrightlungmask_copy.Matrix_length(); i++)
{
if (matrightlungmask_copy.pdata[i] != 0)
{
pleftrightlung_imgmask[i] = 2;
leftlungarea++;
}
}
params[27] = rightlungarea;//左侧对应右肺面积
params[28] = leftlungarea;//右侧对应左肺面积
params[29] = 1;//图像的左侧对应右肺,右侧对应左肺
}
else //左侧对应左肺,右肺1,左肺2
{
//先赋值左肺2
for (int i = 0; i < matleftlungmask_copy.Matrix_length(); i++)
{
if (matleftlungmask_copy.pdata[i] != 0)
{
pleftrightlung_imgmask[i] = 2;
leftlungarea++;
}
}
//再赋值右肺1
for (int i = 0; i < matrightlungmask_copy.Matrix_length(); i++)
{
if (matrightlungmask_copy.pdata[i] != 0)
{
pleftrightlung_imgmask[i] = 1;
rightlungarea++;
}
}
params[27] = leftlungarea;//左侧对应左肺面积
params[28] = rightlungarea;//右侧对应右肺面积
params[29] = 2;//图像的左侧对应左肺,右侧对应右肺
}
//左右肺野识别映射到原始图中
MaptoOrg_Areacal(imgwidth, imgheight, downsampwidth, downsampheight, params, pleftrightlung_imgmask, pleftrightlung_imgmaskorg);//左右肺识别和面积计算映射
delete[] pleftrightlung_imgmask;
pleftrightlung_imgmask = nullptr;
return 1; //
}
int CTRUnet_Detection::LeftRightSegimg(Matrix<unsigned short>& matmasklung, Matrix<unsigned short>& matLeftChest, Matrix<unsigned short>& matRightChest,
vector<int>& xvec_left, vector<int>& yvec_left, vector<int>& xvec_right, vector<int>& yvec_right)
{
int width = matmasklung.width;
int height = matmasklung.height;
int imglength = width * height;
Matrix<unsigned short> matmasklungcopy(width, height);
memcpy(matmasklungcopy.pdata, matmasklung.pdata, sizeof(unsigned short) * imglength);
//根据面积对肺野的左右进行判定。//
vector<int> labval;
vector<int> labind;
bool TF = 0;
Matrix<unsigned short> matLabelimg(width, height);
Conectchose(matmasklungcopy, matLabelimg, labval, labind, TF);//四联通
if (labind.size() <= 1)
{
return 0;
}
//对左右肺野的坐标进行统计
for (int i = 0, step1 = 0; i < matmasklungcopy.height; i++, step1 += matmasklungcopy.width)
{
for (int j = 0; j < matmasklungcopy.width; j++)
{
if (matLabelimg.pdata[step1 + j] == labind.back())//最大的
{
matmasklung.pdata[step1 + j] = 1;//面积大的右肺大,设置为1
}
else if (matLabelimg.pdata[step1 + j] == labind[labind.size() - 2])//倒数第二大的
{
matmasklung.pdata[step1 + j] = 2;//面积小的左肺,设置为2
}
else
{
matmasklung.pdata[step1 + j] = 0;//其他区域置为0
}
}
}
for (int i = 0; i < matmasklung.Matrix_length(); i++)
{
if (matmasklung.pdata[i] != 0)
{
matmasklungcopy.pdata[i] = 1;
}
else
{
matmasklungcopy.pdata[i] = 0;
}
}
//先膨胀
Matrix<unsigned short> strel(3, 3);
for (int i = 0; i < 9; i++)
{
strel.pdata[i] = 1;
}
Matrix<unsigned short> dilateimg(width, height);
Dilateimg(matmasklungcopy, strel, dilateimg);
for (int i = 0; i < dilateimg.Matrix_length(); i++)
{
dilateimg.pdata[i] = dilateimg.pdata[i] - matmasklungcopy.pdata[i];
}
//连通域选择
labval.clear();
labind.clear();
TF = 0;
memset(matLabelimg.pdata, 0, sizeof(unsigned short) * matLabelimg.Matrix_length());
Conectchose(dilateimg, matLabelimg, labval, labind, TF);//四联通
if (labind.size() <= 1)
{
return 0;
}
vector<int> xvec_labfirst;
vector<int> yvec_labfirst;
vector<int> xvec_labsec;
vector<int> yvec_labsec;
//对左右肺野的坐标进行统计
for (int i = 0, step1 = 0; i < matLabelimg.height; i++, step1 += matLabelimg.width)
{
for (int j = 0; j < matLabelimg.width; j++)
{
if (matLabelimg.pdata[step1 + j] == labind.back())//最大的
{
xvec_labfirst.push_back(j);
yvec_labfirst.push_back(i);
}
else if (matLabelimg.pdata[step1 + j] == labind[labind.size() - 2])//倒数第二大的
{
xvec_labsec.push_back(j);
yvec_labsec.push_back(i);
}
}
}
//根据坐标进行判断中心分割线和左右侧肺野
int sumfirst = accumulate(xvec_labfirst.begin(), xvec_labfirst.end(), 0);
sumfirst = sumfirst / xvec_labfirst.size();
int sumsec = accumulate(xvec_labsec.begin(), xvec_labsec.end(), 0);
sumsec = sumsec / xvec_labsec.size();
int xmidseg = 0;
if (sumfirst < sumsec)
{
//xvec_labfirst为左侧
//左右坐标填充
for (size_t i = 0; i < xvec_labfirst.size(); i++)//左侧坐标填充
{
matLeftChest.pdata[yvec_labfirst[i] * width + xvec_labfirst[i]] = 1;
}
for (size_t i = 0; i < xvec_labsec.size(); i++)//右侧坐标填充
{
matRightChest.pdata[yvec_labsec[i] * width + xvec_labsec[i]] = 1;
}
xvec_left.swap(xvec_labfirst);
yvec_left.swap(yvec_labfirst);
xvec_right.swap(xvec_labsec);
yvec_right.swap(yvec_labsec);
}
else
{
//xvec_labsec为左侧,左侧找最大,右侧找最小
//左右坐标填充
for (size_t i = 0; i < xvec_labsec.size(); i++)//左侧坐标填充
{
matLeftChest.pdata[yvec_labsec[i] * width + xvec_labsec[i]] = 1;
}
for (size_t i = 0; i < xvec_labfirst.size(); i++)//右侧坐标填充
{
matRightChest.pdata[yvec_labfirst[i] * width + xvec_labfirst[i]] = 1;
}
xvec_left.swap(xvec_labsec);
yvec_left.swap(yvec_labsec);
xvec_right.swap(xvec_labfirst);
yvec_right.swap(yvec_labfirst);
}
memset(matLeftChest.pdata, 0, sizeof(unsigned short) * imglength);
memset(matRightChest.pdata, 0, sizeof(unsigned short) * imglength);
for (int i = 0; i < xvec_left.size(); i++)
{
matLeftChest.pdata[yvec_left[i] * width + xvec_left[i]] = 1;
}
for (int i = 0; i < xvec_right.size(); i++)
{
matRightChest.pdata[yvec_right[i] * width + xvec_right[i]] = 1;
}
return 1;
}
void CTRUnet_Detection::Conectchose(Matrix<unsigned short>& matInputimg, Matrix<unsigned short>& matLabelimg, vector<int>& labval, vector<int>& labind, bool TF)
{
vector<int> labnum;
if (TF)
{
//八邻域
int width = matInputimg.width;
int height = matInputimg.height;
vector<int> nxvect;//连通域横坐标收纳盒
vector<int> nyvect;//连通域纵坐标收纳盒
int clasenumber = 0;//连通域表盒
//定义八邻域数组,从左上角开始,顺时针遍历八邻域
int neibx[8] = { -1, 0, 1, 1, 1, 0, -1, -1 };//八邻域x方向
int neiby[8] = { -1, -1, -1, 0, 1, 1, 1, 0 };//八邻域y方向