-
Notifications
You must be signed in to change notification settings - Fork 0
/
breast.cpp
1272 lines (1164 loc) · 48 KB
/
breast.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
#include "breast.h"
#define OL_WIDTH 512
#define OL_EXT_LENGTH 4
#define OL_DRAW
#ifdef OL_DRAW
// #define OL_DRAW_HIST
#define OL_DRAW_THRESH
#define OL_DRAW_DIST
#define OL_DRAW_DISTMAP
/* #define OL_DRAW_CORNER */
#endif
typedef map< string, phantomCalibration> calibData;
#define _USE_MATH_DEFINES
breast::breast(std::string t_strFileName): mammography(t_strFileName){
this->strFileName = breast::fileNameErase(t_strFileName);
this->pixelVec2Mat();
this->mChenFatClass = cv::imread(this->strFileName+"Label.tiff");
cv::resize(mChenFatClass,mChenFatClass,this->mMammo.size(),0,0,cv::INTER_NEAREST);
cv::minMaxLoc(mMammo,&this->dMinPixelValue,&this->dMaxPixelValue);
this->getBreastROI();
this->getBreastDistMap();
this->getBreastEdge();
// NEED TO REPLACE THIS WITH LEFT/RIGHT FROM DCM HEADER
// Then just flip all images to be left.
this->leftOrRight(); // Needs to go after getBreastEdge
// We need to replace all these things with
// functions that automatically calculate
// the things they need when the are first
// accessed
if(bLeft){
this->getBreastBottom();
this->getRadialThickness();
this->makeXinROIMap();
}
this->getDensityROI();
}
void breast::pixelVec2Mat(){
this->mMammo = cv::Mat((int)this->Rows, (int)this->Columns, CV_16UC1, cv::Scalar(1));
for(int i = 0; i < this->Columns; i++){
for(int j = 0; j < this->Rows; j++){
this->mMammo.at<Uint16>(j,i) = this->pixelVec[i+(int)this->Columns*j];
}
}
if(this->ImageLaterality == 'R') cv::flip(mMammo,mMammo,1); // If right breast, flip.
cv::normalize(mMammo,mMammoNorm,0,65536, cv::NORM_MINMAX, -1, cv::Mat()); // NB: absolute pixel values meaningless in norm'd images
this->mMammo.convertTo(mMammo8Bit, CV_8U, 1./256); // Leaves pixel values proportional to 16 bit / 8 bit but loses a lot of resolution
this->mMammoNorm.convertTo(mMammo8BitNorm, CV_8U, 1./256);
this->pixelVec.clear();
this->getBitDepth();
}
void breast::getBreastROI(){
// Establish the number of bins
// Set the ranges (for B,G,R) )
float range[] = {static_cast<float>(dMinPixelValue), static_cast<float>(dMaxPixelValue)} ;
const float* fHistRange = {range};
// Set histogram behaviour.
bool bUniform = true; bool bAccumulate = false;
// Compute the histogram.
cv::calcHist(&mMammo, 1, 0, cv::Mat(), this->mHist, 1, &this->iHistSize, &fHistRange, bUniform, bAccumulate );
this->drawHist();
pair<float,float> pPeakValAndLoc = this->findHistPeak();
double dRangeActual = dMaxPixelValue - dMinPixelValue;
double dHistActualRangeRatio = dRangeActual/double(this->iHistSize);
int iPeakVal = dHistActualRangeRatio * pPeakValAndLoc.second + this-> dMinPixelValue;
int iPeakVal8Bit = iPeakVal/256;
/* cv::Mat mMammoROICopy = this->mMammoROI.clone(); */
cv::threshold(this->mMammo8Bit, this->mMammoROI, iPeakVal8Bit, 255, 1);
// Calculate the average intensity of the background
int iBackgroundSize = 0;
for(int i = 0; i < mMammoROI.cols; i++){
for(int j = 0; j < mMammoROI.rows; j++){
if(mMammoROI.at<uchar>(j,i) == 0){
dMeanBackgroundValue += mMammo.at<Uint16>(j,i);
iBackgroundSize++;
}
}
}
dMeanBackgroundValue/=iBackgroundSize;
}
void breast::getBreastDistMap(){
cv::distanceTransform(this->mMammoROI.clone(), this->mMammoDist, cv::DIST_L2, cv::DIST_MASK_PRECISE, CV_32F);
}
void breast::getBreastEdge(){
cv::Mat mMammoThreshedCont;
this->mMammoROI.convertTo(mMammoThreshedCont, CV_8U, 1./256);
std::vector<std::vector<cv::Point>> pEdgeContours;
std::vector<std::vector<cv::Point>> pEdgeContoursCompressed;
//cv::findContours(this->mMammoROI.clone(),pEdgeContours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);
// Use .clone of ROI as this function would otherwise write to ROI.
cv::findContours(this->mMammoROI.clone(),pEdgeContours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE); // This works with the density calculation but ruins the thresholded image...
cv::findContours(this->mMammoROI.clone(),pEdgeContoursCompressed,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE); // This works with the density calculation but ruins the thresholded image...
int iContSize = 0;
for(auto i:pEdgeContours){
if(iContSize < (int)i.size()){
iContSize = i.size();
this->pEdgeContour = i;
}
}
iContSize = 0;
for(auto i:pEdgeContoursCompressed){
if(iContSize < (int)i.size()){
iContSize = i.size();
this->pEdgeContourCompressed = i;
}
}
}
int breast::getBitDepth(){
// Find bit depth of image and store white value.
switch(mMammo.depth()){
case CV_8U:
this->iColourMAX = 255;
break;
case CV_8S:
this->iColourMAX = 127;
break;
case CV_16U:
this->iColourMAX = 65535;
break;
case CV_16S:
this->iColourMAX = 32767;
break;
case CV_32S:
this->iColourMAX = 2147483647;
break;
case CV_32F:
case CV_64F:
exit(1); // Can't cope with that many colours.
break;
}
return this->iColourMAX;
}
void breast::getBreastBottom(){
cv::cornerHarris(this->mMammoROI, this->mCorner, 10, 1, 0.01); // This used to work - now it doesn't seem to pick corners out at all.
cv::Mat mCornerThresh;
/* float iDivisor = 1.24; */
float iDivisor = 1;
double iMax,iMin;
cv::minMaxLoc(mCorner, &iMin, &iMax);
int iColourMax = this->getBitDepth();
vector<vector<cv::Point>> pContours = this->findCorners(iDivisor, iMax, iColourMax);
/* for (auto &i:pContours) { */
/* cout << i[0] << "\t"; */
/* } */
/* cout << endl; */
vector<cv::Point> vecContCents = this->findCornerCentre();
/* for (auto &i:vecContCents){ */
/* cout << i << "\t"; */
/* } */
/* cout << endl << endl; */
/* pair<int, int> iContPos = this->pickCornerCutOff(bLeft); */
/* cout << iContPos.first << "\t" << iContPos.second << endl; */
cv::Mat mMammoROITest = mMammoROI.clone();
/* this->deleteUnneeded(bLeft, mMammoROITest, pEdgeContour, iContPos.second); */
/*
*
* NEED TO TRANSFER ALL THESE FUNCTIONS TO OOO ONES, AND MAKE IT WORK...
*
*
*/
/*
*
*
* CHAIN TEST FROM CONTOURS
* NEED TO LOOK FOR CORNERS IN UPPER REGION OF IMAGE. SOME BREASTS BADLY BEHAVED..
*
*/
std::vector<cv::Point> pEdgePolyApprox;
cv::approxPolyDP(this->pEdgeContourCompressed, pEdgePolyApprox, 3, 0);
std::vector<double> pEdgeAngles; // The "angle" of the line between i and i-1
for(int i = 1; i < pEdgePolyApprox.size(); i++){
int x2 = pEdgePolyApprox[i].x;
int y2 = pEdgePolyApprox[i].y;
int x1 = pEdgePolyApprox[i-1].x;
int y1 = pEdgePolyApprox[i-1].y;
double dAngle = atan2(y2-y1,x2-x1) * 180 / CV_PI;
pEdgeAngles.push_back(dAngle);
}
std::vector<double> pEdgeAngleDeltas; // The change in angle between i and i-1
for(int i = 1; i < pEdgeAngles.size(); i++){
pEdgeAngleDeltas.push_back(abs(pEdgeAngles[i]-pEdgeAngles[i-1]));
/* cout << pEdgeAngleDeltas[i-1] << endl; */
}
// pEdgeAngleDeltas[i] = the angle of the corner at pEdgeCont...[i+1]
#ifdef OAB_DRAW_DEBUG
for(int i = 1; i < pEdgePolyApprox.size() ; i++){
double iRadius = pEdgeAngleDeltas[i-1];
cv::Point pTemp = pEdgePolyApprox[i];
if( (pTemp.x > 10) && (pTemp.x < mMammo.cols - 10) && (pTemp.y > 10) && (pTemp.y < mMammo.rows - 10)) {
cv::circle(mMammoROITest, pTemp, iRadius, 120, -1);
}
}
#endif
int iContPosY = mMammo.rows;
int iContPosX;
int iFinal = 0;
for(int i = 1; i < (int)pEdgePolyApprox.size(); i++){
if(pEdgeAngleDeltas[i-1] > 10){
if(pEdgePolyApprox[i].y > 2*float(mMammo.rows)/3){
if(pEdgePolyApprox[i].x < float(0.25*mMammo.cols)){
/* iContPosY = std::min(pEdgePolyApprox[i].y,iContPosY); */
iContPosY = pEdgePolyApprox[i].y;
iContPosX = pEdgePolyApprox[i].x;
iFinal = i;
}
}
}
}
vector<cv::Point> pEdgePointsToFill;
for(int i = iFinal; i > 0; i--){
pEdgePointsToFill.push_back(pEdgePolyApprox[i]);
}
pEdgePointsToFill.push_back(cv::Point(0,iContPosY));
vector<vector<cv::Point>> pEPTF;
pEPTF.push_back(pEdgePointsToFill);
cv::fillPoly(mMammoROITest,pEPTF,0);
/* this->deleteUnneeded(bLeft, mMammoROITest, pEdgePolyApprox, iContPosY); */
#ifdef OAB_DRAW_DEBUG
cv::circle(mMammoROITest, cv::Point(iContPosX,iContPosY), 10, 0, -1);
#endif
// Need to make "deleteUneeded" work from the polyApprox contour and/or pass it the whole point.
// Otherwise, this works reasonably well.
/* cv::imwrite(strFileName + "cornerTest.png", mMammoROITest); */
this->mMammoROISmaller = mMammoROITest.clone();
}
void breast::drawHist(){
// Draw it
int histSize = this->iHistSize;
int hist_w = histSize; int hist_h = 512;
cv::Mat histImage(hist_h, hist_w, CV_8UC3, cv::Scalar(0,0,0));
// Normalize the result to [ 0, histImage.rows ].
cv::normalize(this->mHist, mHist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat());
float imMax = 0;
int bin_w = cvRound(double(hist_w/histSize));
float iMax = 0;
for(int i = 1; i < histSize; i++){
line(histImage, cv::Point(bin_w*(i-1), hist_h - cvRound(mHist.at<float>(i-1))) ,
cv::Point(bin_w*(i), hist_h - cvRound(mHist.at<float>(i))),
cv::Scalar(255, 255, 255), 2, 8, 0);
if(imMax < mHist.at<float>(i)){
imMax = mHist.at<float>(i);
iMax = i;
}
}
/* cv::imwrite(strFileName+"_hist.jpg", histImage ); */
}
pair<float, float> breast::findHistPeak(){
float iNMax = 0;
float iBinMax = 0;
for(int i = this->iHistSize - 1; i > this->iHistSize*0.5; i--){
if(iNMax < mHist.at<float>(i)){
iNMax = mHist.at<float>(i);
iBinMax = i;
}
}
for(int i = iBinMax; i > this->iHistSize*0.5; i--){
if(mHist.at<float>(i) < 1){
iBinMax = i;
}
}
return make_pair(iNMax, iBinMax);
}
pair<float, float> breast::findHistPeakLeft(){
float iNMax = 0;
float iBinMax = 0;
for(int i = this->iHistSize*0.5; i > 0; i--){
if(iNMax < mHist.at<float>(i)){
iNMax = mHist.at<float>(i);
iBinMax = i;
}
}
return make_pair(iNMax, iBinMax);
}
pair<float, float> breast::findHistPeakRight(){
float iNMax = 0;
float iBinMax = 0;
for(int i = this->iHistSize*0.5; i < this->iHistSize; i++){
if(iNMax < mHist.at<float>(i)){
iNMax = mHist.at<float>(i);
iBinMax = i;
}
}
return make_pair(iNMax, iBinMax);
}
float breast::findWidth(const int iBinMax, const int iNMax){
// Find the width at the value with PEAK_VALUE/OL_WIDTH.
float iQuartMax = 0;
for(int i = iBinMax; i > 0; i--){
if(mHist.at<float>(i) < iNMax/OL_WIDTH){
iQuartMax = i;
break;
}
}
return iQuartMax;
}
bool breast::leftOrRight(){
/* std::vector<cv::Point> pEdgeContourCopy = pEdgeContour; */
sort(pEdgeContour.begin(),pEdgeContour.end(),[](const cv::Point &l, const cv::Point &r){return l.x < r.x;});
/* sort(pEdgeContourCopy.begin(),pEdgeContourCopy.end(),[](const cv::Point &l, const cv::Point &r){return l.y < r.y;}); */
/* int iXLast = -1; */
/* int iYLast = -1; */
/* int i2LastGap = 0; */
/* int iCurrGap = 0; */
/* int iTotalGap = 0; */
/* for(auto i:pEdgeContour){ */
/* if (i.x == iXLast){ */
/* iCurrGap = abs(iYLast-i.y); */
/* if(iCurrGap > 0.1*mMammo.rows){ */
/* if(i2LastGap != 0){ */
/* iTotalGap += i2LastGap - iCurrGap; */
/* } */
/* } */
/* } else { */
/* iXLast = i.x; */
/* iYLast = i.y; */
/* i2LastGap = iCurrGap; */
/* } */
/* } */
/* this->bLeft = iTotalGap < 0; */
/* this->bLeft = (this->ImageLaterality == 'L'); */
this->bLeft = true;
this->bLeftNEW = (this->ImageLaterality == 'L');
return bLeft;
}
std::vector<std::vector<cv::Point>> breast::findCorners(float iDivisor, const int iMax, const int iCOLOUR_MAX){
cv::Mat mCornerThresh;
int iCorners;
do{
cv::threshold(mCorner,mCornerThresh,iMax/iDivisor,iCOLOUR_MAX,0);
cv::Mat mCornerT8U;
mCornerThresh.convertTo(mCornerT8U, CV_8U);
cv::findContours(mCornerT8U,pContours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE); // This seems to yield many, many contours regardless of threshold.
iCorners = pContours.size();
/* cout << iCorners << "\t"; */
iDivisor+=0.05;
} while ((iCorners < 5) && (iDivisor < 10));
/* cout << endl; */
return pContours;
}
std::vector<cv::Point> breast::findCornerCentre(){
for(auto &i:pContours){
cv::Moments momCont = cv::moments(i);
if (momCont.m00 > 0) vecContCents.push_back(cv::Point(momCont.m10/momCont.m00,momCont.m01/momCont.m00));
}
return vecContCents;
}
pair<int, int> breast::pickCornerCutOff(const bool bLeft){
int iContPosY = mMammo.rows;
int iContPosX;
for(int i = 0; i < (int)pContours.size(); i++){
if(vecContCents[i].y > 2*float(mMammo.rows)/3){
if(bLeft){
if(vecContCents[i].x < float(0.25*mMammo.cols)){
iContPosY = std::min(vecContCents[i].y,iContPosY);
iContPosX = vecContCents[i].x;
}
} else {
if(vecContCents[i].x > float(0.75*mMammo.cols)){
iContPosY = std::min(vecContCents[i].y,iContPosY);
iContPosX = vecContCents[i].x;
}
}
}
}
return make_pair(iContPosX, iContPosY);
}
void breast::deleteUnneeded(const bool bLeft, cv::Mat mMammoThreshedCopy, const std::vector<cv::Point> pEdgeContourCopy, const int iContPosY){
if (iContPosY < mMammo.rows){
int extremalX=pEdgeContourCopy[0].x;
int lastY=pEdgeContourCopy[0].y;
//int lastX=pEdgeContourCopy[0].x;
std::vector<cv::Point> pEdgeThrowAway;
for(auto i:pEdgeContourCopy){
if(lastY == i.y){
extremalX = bLeft?
((i.x>1)?std::min(extremalX,i.x):extremalX)
:
((i.x<mMammo.cols-5)?std::max(extremalX,i.x):extremalX);
} else {
if(lastY >= iContPosY){
pEdgeThrowAway.push_back(cv::Point(extremalX,lastY));
}
extremalX = bLeft?((i.x>1)?i.x:extremalX):((i.x<mMammo.cols-5)?i.x:extremalX);
}
lastY = i.y;
}
/* cv::cvtColor(mMammoThreshedCopy, mMammoThreshedCopy, cv::COLOR_BGR2GRAY); */
for(auto i:pEdgeThrowAway){
if(bLeft){
for(int x = 0; x <= i.x+2; x++){
mMammoThreshedCopy.at<uchar>(cv::Point(x, i.y)) = 0;
}
} else {
for(int x = i.x-2; x < mMammoROI.cols; x++){
mMammoThreshedCopy.at<uchar>(i.y, x) = 0;
}
}
}
}
}
std::vector<float> breast::getDistBright(){ // Find the average brightness of pixels a certain distance from the breast
std::vector<int> vecDistAv; // Number of pixels at distance from black.
cv::Mat_<uchar> mMammoDistChar = mMammoDist; // At one point this was a matrix of ints - don't know why.
vecDistBright.resize(256);
vecDistAv.resize(256);
cv::Mat mMammoCopy = mMammo8BitNorm;
for(int i = 0; i < (int)vecDistBright.size(); ++i){ vecDistBright[i] = uchar(0);} // Initialise vectors
for(int i = 0; i < (int)vecDistAv.size(); ++i){ vecDistAv[i] = 0;} // ...continued.
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
int iDist = int(mMammoDistChar(j,i));
vecDistBright[iDist]+=float(mMammoCopy.at<uchar>(j,i));
vecDistAv[iDist]++;
}
}
for(int i = 0; i < (int)vecDistBright.size(); ++i){ if(vecDistAv[i] != 0){vecDistBright[i]/= float(vecDistAv[i]);}}
return vecDistBright;
}
std::vector<float> breast::breastThickness(const int histSize, const cv::Mat_<int> mMammoDistChar, const cv::Mat mMammoCopy){ // Find the average brightness of the brightest half of pixels at a certain distance from the breast
std::vector<int> vecDistAvBrightest; // Number of pixels at distance from black.
this->vecDistBrightBrightest.resize(256);
vecDistAvBrightest.resize(256);
// This could well be the problem - need to find a more sensible way of sorting out duff values; maybe not all of vector is populated?
for(int i = 0; i < (int)vecDistBrightBrightest.size(); ++i){ vecDistBrightBrightest[i] = uchar(0);}
for(int i = 0; i < (int)vecDistAvBrightest.size(); ++i){ vecDistAvBrightest[i] = 0;}
// Find the total brightness of the brightest half of pixels at each distance,
// and draw the fat ROI for the image.
/* this->mMammoFatROI = cv::Mat((int)mMammo8BitNorm.rows, (int)mMammo8BitNorm.cols, CV_8UC1, cv::Scalar(1)); */
this->mMammoFatROI = this->mMammoROI.clone();
for(int i = 0; i < mMammo8Bit.cols; i++){
for(int j = 0; j < mMammo8BitNorm.rows; j++){
if(mMammoROI.at<uchar>(j,i) != 0){
int iDist = int(mMammoDistChar(j,i));
if(int(mMammo8BitNorm.at<uchar>(j,i)) >= this->vecDistBright[iDist]){
mMammoFatROI.at<uchar>(j,i) = 128;
vecDistBrightBrightest[iDist]+=float(mMammo8BitNorm.at<uchar>(j,i));
vecDistAvBrightest[iDist]++;}
}
}
}
/* cv::imwrite(this->strFileName+"FatROI.png", mMammoFatROI); */
// Find the average brightness by dividing the total brightness by the number of pixels.
for(int i = 0; i < (int)vecDistBrightBrightest.size(); ++i){
if(vecDistAvBrightest[i] != 0){
vecDistBrightBrightest[i]/= float(vecDistAvBrightest[i]);
}
}
return vecDistBrightBrightest;
}
// It would be a very good idea to make this into a more general thing, i.e stop hard coding bin sizes....
void breast::getRadialThickness(){
cv::normalize(this->mMammoDist, this->mMammoDist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());
vector<float> vecDistBright = this->getDistBright();
int dist_w = 255*2; int dist_h = 256;
cv::Mat distImage(dist_h, dist_w, CV_8UC1, cv::Scalar(0));
cv::Mat_<int> mMammoDistChar = this->mMammoDist;
cv::Mat mMammoCopy;
int histSize = 255;
vector<float> vecDistBrightBrightest = this->breastThickness(histSize, mMammoDistChar, mMammoCopy);
for(int i = 1; i < vecDistBrightBrightest.size(); i++){
if(vecDistBrightBrightest[i-1] == 0){
vecDistBrightBrightest[i-1] = vecDistBrightBrightest[i];
}
}
for(auto &i:vecDistBrightBrightest){
i = i*256*256;
i = log(i/this->dMeanBackgroundValue);
}
cv::normalize(vecDistBrightBrightest, vecDistBrightBrightest, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());
for(auto &i:vecDistBrightBrightest){
i = 256 - i;
}
int bin_w = cvRound(double(dist_w/histSize));
for(int i = 1; i < histSize; i++){
line(distImage, cv::Point(bin_w*(i-1), dist_h - cvRound(vecDistBrightBrightest[i-1])) ,
cv::Point(bin_w*(i), dist_h - cvRound(vecDistBrightBrightest[i])),
cv::Scalar(255, 255, 255), 1, 8, 0);
}
this->mMammoDistImage = distImage;
}
// What the dickens is this? It appears to be unused.
std::vector<float> breast::normalBreastThickness(std::vector<float> vecDistBrightBrightest, const cv::Mat distImage){
// Normalize the result to [ 0, histImage.rows ].
vecDistBrightBrightest[255]=0;
vecDistBrightBrightest[0]=0;
cv::normalize(vecDistBrightBrightest, vecDistBrightBrightest, 0, distImage.rows, cv::NORM_MINMAX, -1, cv::Mat());
return vecDistBrightBrightest;
}
void breast::getDensityROI(){
for(int i = 0; i < this->mMammo.cols; i++){
for(int j = 0; j < this->mMammo.rows; j++){
int t = this->getPixelType(i,j);
if((t == XIN_BACKGROUND) /*|| (t == XIN_NIPPLE) */|| (t == XIN_PECTORAL_MUSCLE)){
this->mMammoROISmaller.at<uchar>(j,i) = 0;
}
}
}
/* cv::imwrite(this->strFileName+"FinalROI.png",this->mMammoROISmaller); */
}
void breast::drawImages(string fileName, const cv::Mat mCornerTresh, const cv::Mat mMammoThreshedCopy, const int histSize){
/* fileName.pop_back(); */
/* int dist_w = histSize*2; */
/* int dist_h = 512; */
#ifdef OL_DRAW_DIST
/* int bin_w = cvRound(double(dist_w/histSize)); */
/* for(int i = 1; i < histSize; i++){ */
/* line(distImage, cv::Point(bin_w*(i), dist_h - cvRound(vecDistBrightBrightest[i])) , */
/* cv::Point(bin_w*(i), dist_h - cvRound(vecDistBrightBrightest[i])), */
/* cv::Scalar(255, 255, 255), 2, 8, 0); */
/* } */
/* cv::imwrite(fileName+"_dist.png", this->mMammoDistImage ); */
#endif
#ifdef OL_DRAW_CORNER
/* cv::imwrite(fileName+"_corner.png", mCornerThresh); */
#endif
#ifdef OL_DRAW_DISTMAP
/* cv::imwrite(fileName+"_distmap.png", mMammoDist); */
#endif
#ifdef OL_DRAW_THRESH
//cv::imwrite(fileName+"_thresh.png", mMammoThreshedCopy);
#endif
#ifdef OL_DRAW_ALTERED
/* cv::imwrite(fileName+"_alt.png", mMammo); */
#endif
}
double breast::totalBreast(const string filTar){
double breastThick(0);
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
if(isBreast(i,j)){
double pixThick = breast::glandpercentInverse(double(this->getHeight(i,j)), filTar, this->strKVP, this->numExposure);
breastThick += pixThick;
#ifdef OAB_DEBUG
if(pixThick > numThickness){
this->mMammo8BitNorm.at<uchar>(j,i) = uchar(255);
}
#endif
}
}
}
return breastThick;
}
pair<double,double> breast::glandpercent(const phantomCalibration calib, const string filTar, const string kV, const double t){
div_t divresult;
divresult = div(t,10);
pair<int,int> thick = {divresult.quot*10,divresult.quot*10+10};
double a = (calib.calibSet.find(thick.first)->second.first+calib.calibSet.find(thick.second)->second.first)/2;
string lookFor = filTar+kV;
double a_calc = this->b_a.find(lookFor)->second;
double b_calc = this->b_b.find(lookFor)->second;
double b = a_calc*t+b_calc;
pair<double,double> ret = {a,b};
return ret;
}
double breast::glandpercentInverse(const double MPV, const string filTar, const string kV, const double exposure){
string lookFor = filTar+kV;
double a_calc = this->b_a.find(lookFor)->second;
double b_calc = this->b_b.find(lookFor)->second;
double t = (log(MPV/exposure)-b_calc)/a_calc;
if(t < 0)
t = 0;
return t;
}
void breast::thicknessMapRedVal(const pair<double,double> coeff3, const int exposure){
cv::Mat tg = cv::Mat(mMammo.rows, mMammo.cols, CV_8UC1, cvScalar(0));
string bodyThickness = various::ToString<OFString>(this->BodyPartThickness);
int thickness = atoi(bodyThickness.c_str());
double tgTemp;
double maxPixValCurve = exp(coeff3.second)*exposure;
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
if(mMammoROI.at<Uint8>(j,i) != 0){
if(int(mMammo.at<Uint16>(j,i)) == 0){
tgTemp = thickness;
} else{
tgTemp = (log(double(mMammo.at<Uint16>(j,i))/exposure)-coeff3.second)/coeff3.first;
}
if(tgTemp >= 0 && tgTemp <= thickness){
tg.at<Uint8>(j,i) = tgTemp*double(255/thickness);
} else if(tgTemp > thickness){
tg.at<Uint8>(j,i) = thickness*double(255/thickness);
} else{
tg.at<Uint8>(j,i) = 0;
}
}
}
}
cv::Mat dst;
cvtColor(tg,dst,CV_GRAY2RGB);
cv::Vec3b color;
color.val[0] = 0;
color.val[1] = 0;
color.val[2] = 255;
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
if(mMammoROI.at<Uint8>(j,i) != 0){
if(int(mMammo.at<Uint16>(j,i)) > maxPixValCurve){
dst.at<cv::Vec3b>(cv::Point(i,j)) = color;
}
}
}
}
/* cv::imwrite("test_thickMapRed.png",dst); */
}
Uint16 breast::getHeight(int x, int y){
// Equivalent-log-fat model
return this->mHeightMap16.at<Uint16>(y,x);
}
bool breast::isFat(int x, int y){
return (mMammoFatROI.at<uchar>(y,x) == 128);
}
void breast::makeXinROIMap(){
cv::Mat HRROIMap = this->mChenFatClass.clone();
cv::Mat HeightMap(HRROIMap.rows,HRROIMap.cols,CV_32F,cv::Scalar(0));
for(int i = 0; i < HRROIMap.cols; i++){
for(int j = 0; j < HRROIMap.rows; j++){
HeightMap.at<float>(j,i) = -1*(log(float(mMammo.at<Uint16>(j,i))/float(this->dMeanBackgroundValue)));
}
}
double minVal;
double maxVal;
// Rescale the image to make it lie between 0 and 255
cv::minMaxLoc(HeightMap, &minVal, &maxVal);
HeightMap-=minVal;
HeightMap.convertTo(HeightMap,CV_8U,255.0/(maxVal-minVal));
/* cv::minMaxLoc(HeightMap, &minVal, &maxVal); */
/* HeightMap = HeightMap*256/maxVal; */
/* HRROIMap = mChenFatClass*(256/5); // Convert between our 14 bit mammograms and 256 */
/* cv::imwrite(strFileName+"FatLogTransform.png",HeightMap); */
// FILLING IN HOLES IN FAT MAP
// Morphological opening: remove noise, fill in small holes
cv::Mat mCircSE = cv::getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(25,25));
cv::Mat HeightMapFilled = HeightMap.clone();
cv::morphologyEx(HeightMap,HeightMapFilled,cv::MORPH_OPEN,mCircSE);
for(int i = 0; i < HeightMap.cols; i++){
for(int j = 0; j < HeightMap.rows; j++){
if(this->getPixelType(i,j)!=XIN_FAT){ // 2 = fat
HeightMapFilled.at<uchar>(j,i) = 0;
}
}
}
/* cv::GaussianBlur(HeightMapFilled,HeightMapFilled,cv::Size(5,5),10); */
/* cv::imwrite(strFileName+"FatLogFilled.png",HeightMapFilled); */
// Next step: identify remaining holes and fill them in, one by one.
// Proposed Procedure:
// For each "zero height" glandular pixel:
// Take weighted average of neighbouring true non-zero pixels lying on distance transform.
// Can find true non-zero via making copy of image and writing to original
//
cv::Mat HeightMapCopy = HeightMapFilled.clone();
/* mCircSE = cv::getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(25,25)); */
/* cv::morphologyEx(HeightMapFilled,HeightMapFilled,cv::MORPH_CLOSE,mCircSE); */
/* cv::medianBlur(HeightMapFilled,HeightMapFilled,25); */
/* cv::imwrite(strFileName+"FatLogClosed.png",HeightMapFilled); */
/* this->mHeightMap = HeightMapFilled; */
/* Next step: Distance transform stuff */
/* Want to create a vector of points at each distance i.e. vecDist[100] is all the points 100 away */
/*
*
* AVERAGING PARALLEL TO THE BREAST EDGE
*
*
*/
cv::Mat_<uchar> mMammoDistChar = mMammoDist.clone(); // At one point this was a matrix of ints - don't know why.
vector<vector<cv::Point>> pDistContours;
cv::Mat_<uchar> mMammoDistThresh;
for(int k = 0; k < 256; k++){
cv::threshold(mMammoDistChar, mMammoDistThresh, k, 255, 1);
cv::findContours(mMammoDistThresh.clone(),pDistContours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);
for(auto &vPatD:pDistContours){
bool bEmpty;
uchar lastFilled = 0;
for(auto &p:vPatD){
int pType = this->getPixelType(p.x,p.y);
if((pType != XIN_BACKGROUND) && (pType != XIN_PECTORAL_MUSCLE)){
bEmpty = (HeightMapCopy.at<uchar>(p) == 0);
if(bEmpty){
HeightMapFilled.at<uchar>(p) = lastFilled;
} else {
/* lastFilled = HeightMapCopy.at<uchar>(p); */
lastFilled = breast::getAvNhood8(HeightMapCopy, p, 2);
}
}
}
lastFilled = 0;
for(auto it = vPatD.rbegin(); it != vPatD.rend(); it++){
auto p = *it;
bEmpty = (HeightMapCopy.at<uchar>(p) == 0);
int pType = this->getPixelType(p.x,p.y);
if((pType != XIN_BACKGROUND) && (pType != XIN_PECTORAL_MUSCLE)){
if(bEmpty){
uchar cCurrent = HeightMapFilled.at<uchar>(p);
if(cCurrent != 255){
HeightMapFilled.at<uchar>(p) = (lastFilled+HeightMapFilled.at<uchar>(p))/2;
} else {
HeightMapFilled.at<uchar>(p) = lastFilled;
}
} else {
lastFilled = breast::getAvNhood8(HeightMapCopy, p, 2);
}
}
}
/* cout << float(100*k/255) << "%\r" << flush; */
}
}
HeightMapCopy = HeightMapFilled.clone();
/* Linear interpolation along x-axis */
for(int j = 0; j < HeightMap.rows; j++){
uchar lastFilled = 0;
float distFromHole = 0;
for(int i = 0; i < HeightMap.cols; i++){
int pType = this->getPixelType(i,j);
/* if(pType == XIN_DENSER_GLAND){ */
if((pType != XIN_BACKGROUND) && (pType != XIN_PECTORAL_MUSCLE)){
uchar cCurrent = HeightMapFilled.at<uchar>(j,i);
if(cCurrent == 0){
distFromHole++;
} else {
uchar thisSide = cCurrent;
for(int ii = distFromHole; ii > 0; ii--){
HeightMapFilled.at<uchar>(j,i-ii) = uchar(float(lastFilled*(ii/distFromHole)+(1-(ii/distFromHole))*thisSide));
}
lastFilled = cCurrent;
distFromHole = 0;
}
}
}
}
cv::Mat mCircSE2 = cv::getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(2,2));
/* cv::morphologyEx(HeightMapFilled,HeightMapFilled,cv::MORPH_CLOSE,mCircSE2); */
cv::medianBlur(HeightMapFilled,HeightMapFilled,25);
cv::medianBlur(HeightMapFilled,HeightMapFilled,25);
cv::medianBlur(HeightMapFilled,HeightMapFilled,25);
/* cv::imwrite(strFileName+"FatLogFilled.png",HeightMapFilled); */
this->mHeightMap = HeightMapFilled.clone();
/* Undoing all the transformations */
HeightMapFilled.convertTo(HeightMapFilled,CV_16U);
for(int i = 0; i < HeightMap.cols; i++){
for(int j = 0; j < HeightMap.rows; j++){
float fCurrentShade = float(HeightMapFilled.at<Uint16>(j,i));
fCurrentShade = fCurrentShade*(float(maxVal-minVal)/255.0) + minVal;
fCurrentShade = exp(-fCurrentShade)*this->dMeanBackgroundValue;
HeightMapFilled.at<Uint16>(j,i) = Uint16(fCurrentShade);
}
}
this->mHeightMap16 = HeightMapFilled.clone();
}
uchar breast::getAvNhood8(cv::Mat &mat, cv::Point &p, int nhood){
float av=0;
int n = 0;
for(int i = 0; i < nhood; i++){
for(int j = 0; j < nhood; j++){
float cC = float(mat.at<uchar>(p.y-1+j,p.x-1+i));
if(cC > 0.5){
n++;
av+=cC;
}
}
}
av = av / n;
return uchar(av);
}
int breast::getPixelType(int x, int y){
int iType = this->mChenFatClass.at<Uint8>(y,x,0);
if (iType == 43) return XIN_BACKGROUND;
if (iType == 85) return XIN_FAT;
if (iType == 128) return XIN_PECTORAL_MUSCLE;
if (iType == 170) return XIN_GLAND;
if (iType == 213) return XIN_NIPPLE;
if (iType == 255) return XIN_DENSER_GLAND;
return 0;
}
bool breast::isBreast(int x, int y){
return (mMammoROISmaller.at<uchar>(y,x) > 0);
}
vector<pair<int,int>> breast::pixelOfInterestExposure(){
vector<pair<int,int>> pixelOfInterestExposureVec;
/* CHOOSE OVEREXPOSED PIXELS FROM THE HISTOGRAM */
pair<float, float> rightPeak = this->findHistPeakRight();
pair<float, float> leftPeak = this->findHistPeakLeft();
float minVal = leftPeak.first;
int rightLim = 0;
for(int i = rightPeak.second; i > leftPeak.second*3.5; i--){
if(this->mHist.at<float>(i) < minVal)
minVal = this->mHist.at<float>(i);
}
for(int i = rightPeak.second; i > leftPeak.second*3.5; i--){
if(this->mHist.at<float>(i) == minVal){
rightLim = i;
break;
}
}
int MPVRangeUpperLimit = (this->LargestImagePixelValue/512)*(rightLim);
int MPVRangeLowerLimit = (this->LargestImagePixelValue/512)*(leftPeak.second*3.5);
for(int i = 0; i < this->mMammo.cols; i++){
for(int j = 0; j < this->mMammo.rows; j++){
if(this->mMammo.at<Uint16>(j,i) < MPVRangeUpperLimit && this->mMammo.at<Uint16>(j,i) > MPVRangeLowerLimit)
pixelOfInterestExposureVec.push_back(make_pair(j,i));
}
}
return pixelOfInterestExposureVec;
}
map<int,vector<pair<double,pair<int,int>>>> breast::distMap(vector<pair<int,int>> pixelOfInterestExposureVec){
map<int,vector<pair<double,pair<int,int>>>> breastDistMap;
int distVal, vecLength, leftBorder, rightBorder, difference;
double MPVsum, countPix;
for(vector<pair<int,int>>::iterator it = pixelOfInterestExposureVec.begin() ; it != pixelOfInterestExposureVec.end(); ++it){
distVal = float(this->mMammoDist.at<uchar>(it->first,it->second));
if(!breastDistMap.count(distVal)){
vector<pair<double,pair<int,int>>> breastDistMapVec;
breastDistMap[distVal] = breastDistMapVec;
}
breastDistMap[distVal].push_back(make_pair(0,make_pair(it->first,it->second)));
}
for(map<int,vector<pair<double,pair<int,int>>>>::iterator it = breastDistMap.begin() ; it != breastDistMap.end(); ++it){
vecLength = it->second.size();
for(int i = 0; i < vecLength; i++){
MPVsum = 0; countPix = 0;
difference = i-100;
if(difference < 0){
leftBorder = 0;
} else{
leftBorder = i - 100;
}
difference = i+100;
if(difference > vecLength){
rightBorder = vecLength;
} else{
rightBorder = i + 100;
}
for(int j = leftBorder; j < rightBorder; j++){
MPVsum += this->mMammo.at<Uint16>(it->second[j].second.first,it->second[j].second.second);
countPix++;
}
it->second[i].first = MPVsum/countPix;
}
}
return breastDistMap;
}
void breast::applyExposureCorrection(map<int,vector<pair<double,pair<int,int>>>> breastDistMap){
int vecLength1, vecLength2, pixVal;
map<int,vector<pair<double,pair<int,int>>>>::iterator it;
vector<pair<pair<int,int>,double>> correctedMPVs;
it = breastDistMap.begin();
int minKey = it->first;
it = breastDistMap.end();
int maxKey = it->first;
for(int i = (maxKey-1); i >= minKey; i--){
if(i != 255){
vecLength1 = breastDistMap[i].size();
vecLength2 = breastDistMap[i+1].size();
for(int j = 0; j < vecLength1; j++){
if(vecLength2 != 0){
pixVal = this->mMammo.at<Uint16>(breastDistMap[i][j].second.first, breastDistMap[i][j].second.second);
if(j < vecLength2){
if(breastDistMap[i+1][j].first != 0)
this->mMammo.at<Uint16>(breastDistMap[i][j].second.first, breastDistMap[i][j].second.second) =
pixVal*(breastDistMap[i+1][j].first/breastDistMap[i][j].first);
} else{
if(breastDistMap[i+1][vecLength2].first != 0)
this->mMammo.at<Uint16>(breastDistMap[i][j].second.first, breastDistMap[i][j].second.second) =
pixVal*(breastDistMap[i+1][vecLength2].first/breastDistMap[i][vecLength2].first);
}
}
}
}
}
}
cv::Mat breast::thicknessMap(const string bodyThickness, const pair<double,double> coeff3, const int exposure){
cv::Mat tg = cv::Mat(mMammo.rows, mMammo.cols, CV_8UC1, cvScalar(0));
int thickness = atoi(bodyThickness.c_str());
double tgTemp;
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
if(mMammoROI.at<Uint8>(j,i) != 0){
if(int(mMammo.at<Uint16>(j,i)) == 0){
tgTemp = thickness;
} else{
tgTemp = (log(double(mMammo.at<Uint16>(j,i))/exposure)-coeff3.second)/coeff3.first;
}
if(tgTemp >= 0 && tgTemp <= thickness){
tg.at<Uint8>(j,i) = tgTemp*double(255/thickness);
} else if(tgTemp > thickness){
tg.at<Uint8>(j,i) = thickness*double(255/thickness);
} else{
tg.at<Uint8>(j,i) = 0;
}
}
}
}
return tg;
}
void breast::exposureMap(const pair<double,double> coeff3, const int exposure, const vector<pair<int,int>> pixelOfInterestExposureVec){
cv::Mat tg = cv::Mat(mMammo.rows, mMammo.cols, CV_8UC1, cvScalar(0));
string bodyThickness = various::ToString<OFString>(this->BodyPartThickness);
tg = this->thicknessMap(bodyThickness, coeff3, exposure);
various::redMap(tg, pixelOfInterestExposureVec);
}
pair<cv::Point,float> breast::straightLevel(const int row){
pair<cv::Point,float> straightLevelPoint;
int counter(0);
for(int i = 0; i < this->mHeightMap16.cols; i++){
if(this->getPixelType(i,row) == XIN_FAT){
if(double(this->mHeightMap16.at<Uint16>(row,i)) != 0){
counter++;
straightLevelPoint = make_pair(cv::Point(i,row),float(this->mHeightMap16.at<Uint16>(row,i)));
if(counter >1 )
break;