-
Notifications
You must be signed in to change notification settings - Fork 0
/
breast_distvsbright.cpp
435 lines (381 loc) · 10.9 KB
/
breast_distvsbright.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
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <string>
#include <algorithm>
#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
//using namespace cv;
using namespace std;
int main(int argc, char** argv){
string strFilename = argv[1];
// Load image supplied through command line.
cv::Mat mMammo = cv::imread(strFilename, 1);
if(!mMammo.data){
return -1;
}
strFilename.erase(strFilename.length()-OL_EXT_LENGTH, strFilename.length()); // Remove file extension.
// Find bit depth of image and store white value.
int iCOLOUR_MAX;
switch(mMammo.depth()){
case CV_8U:
iCOLOUR_MAX = 255;
break;
case CV_8S:
iCOLOUR_MAX = 127;
break;
case CV_16U:
iCOLOUR_MAX = 65535;
break;
case CV_16S:
iCOLOUR_MAX = 32767;
break;
case CV_32S:
iCOLOUR_MAX = 2147483647;
break;
case CV_32F:
case CV_64F:
exit(1); // Can't cope with that many colours.
break;
}
/*
*
*
*
* SEPERATING THE BREAST FROM THE BACKGROUND
*
*
*
*/
// Separate the image into 3 channels.
vector<cv::Mat> bgr_planes;
cv::split(mMammo, bgr_planes);
// Establish the number of bins
int histSize = iCOLOUR_MAX;
// Set the ranges (for B,G,R) )
float range[] = {0, float(iCOLOUR_MAX)+1} ;
const float* histRange = {range};
// Set histogram behaviour.
bool uniform = true; bool accumulate = false;
cv::Mat mHistB;
// Compute the histogram.
cv::calcHist(&bgr_planes[0], 1, 0, cv::Mat(), mHistB, 1, &histSize, &histRange, uniform, accumulate );
// Draw it.
#ifdef OL_DRAW_HIST
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(mHistB, mHistB, 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(mHistB.at<float>(i-1))) ,
cv::Point(bin_w*(i), hist_h - cvRound(mHistB.at<float>(i))),
cv::Scalar(255, 255, 255), 2, 8, 0);
if(imMax < mHistB.at<float>(i)){
imMax = mHistB.at<float>(i);
iMax = i;
}
}
cv::imwrite(strFilename+"_hist.jpg", histImage );
#endif
// Find the peak in the brightest half of the image.
float iNMax = 0;
float iBinMax = 0;
for(int i = histSize - 1; i > histSize*0.5; i--){
if(iNMax < mHistB.at<float>(i)){
iNMax = mHistB.at<float>(i);
iBinMax = i;
}
}
// Find the width at the value with PEAK_VALUE/OL_WIDTH.
float iQuartMax = 0;
for(int i = iBinMax; i > 0; i--){
if(mHistB.at<float>(i) < iNMax/OL_WIDTH){
iQuartMax = i;
break;
}
}
/*
*
*
*
* FINDING THE CONTOUR WHICH THE DESRIBES THE EDGE OF THE BREAST
*
*
*
*/
cv::Mat mMammoThreshed;
// Threshold the image to 'cut off' the brighter peak from the histogram.
cv::threshold(mMammo, mMammoThreshed, iCOLOUR_MAX*(iQuartMax/histSize), iCOLOUR_MAX, 1);
// MAGIC
cv::Mat mMammoThreshedCopy = mMammoThreshed;
/*
*
*
*
*
* DISTANCE TRANSFORM
* (need to do it here to stop it thinking edge of image is an edge)
*
*
*
*/
// Convert the threshold into greyscale to stop the distance transform complaining. Consider moving this to the start of the programme.
cv::cvtColor(mMammoThreshed,mMammoThreshed, cv::COLOR_BGR2GRAY);
// Use a less accurate but smoother looking distance transform. More research needed here
cv::Mat mMammoDist;
cv::distanceTransform(mMammoThreshed, mMammoDist, cv::DIST_L2, cv::DIST_MASK_PRECISE, CV_32F);
cv::Mat mMammoThreshedCont;
mMammoThreshed.convertTo(mMammoThreshedCont, CV_8U);
vector<vector<cv::Point>> pEdgeContours;
cv::findContours(mMammoThreshed,pEdgeContours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_NONE);
int iContSize = 0;
vector<cv::Point> pEdgeContour;
for(auto i:pEdgeContours){
if(iContSize < i.size()){
iContSize = i.size();
pEdgeContour = i;
}
}
/*
*
*
*
*
* FIGURE OUT WHETHER WE ARE LOOKING AT A LEFT OR RIGHT BREAST
*
*
*
*
*/
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;
}
}
bool bLeft = iTotalGap < 0;
/*
*
*
*
* TRY TO FIND A CORNER NEAR THE BOTTOM OF THE BREAST
* (needs improvement; struggles to find extremely concave corners)
*
*
*/
// Make a probability map of likely corners in the mammogram.
cv::Mat mCorner;
cv::cornerHarris(mMammoThreshed, mCorner, 40, 3, 0.04);
float iMax = 0;
float iiMax = 0;
float ijMax = 0;
for(int i = 0; i < mCorner.cols; i++){
for(int j = 0; j < mCorner.rows; j++){
if (mCorner.at<float>(i,j) != 0){
//iMax = max(iMax,mCorner.at<float>(i,j));
if (iMax < mCorner.at<float>(i,j)){
iMax = mCorner.at<float>(i,j);
iiMax = i;
ijMax = j;
}
}
}
}
// Threshold this map repeatedly until we find at least three probable regions.
// NEED TO FIND A BETTER WAY OF FINDING CORNERS
cv::Mat mCornerThresh;
int iCorners;
float iDivisor = 1.25;
vector<vector<cv::Point>> pContours;
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);
iCorners = pContours.size();
iDivisor+=0.05;
} while ((iCorners < 5) && (iDivisor < 10));
// Find the most spatially spread out corner. Unused, currently - what does it mean?
sort(pContours.begin(),pContours.end(),[]
(const vector<cv::Point> &l, const vector<cv::Point> &r){
return cv::contourArea(l) > cv::contourArea(r);
});
// Find the centers of the corners.
vector<cv::Point> vecContCents;
for(auto i:pContours){
cv::Moments momCont = cv::moments(i);
vecContCents.push_back(cv::Point(momCont.m10/momCont.m00,momCont.m01/momCont.m00));
}
// Pick a corner to cut off.
int iContPosY = mMammo.rows;
int iContPosX;
for(int i = 0; i < pContours.size(); i++){
if(vecContCents[i].y > 2*float(mMammo.rows)/3){
if(bLeft){
if(vecContCents[i].x < float(0.25*mMammo.cols)){
iContPosY = min(vecContCents[i].y,iContPosY);
iContPosX = vecContCents[i].x;
}
} else {
if(vecContCents[i].x > float(0.75*mMammo.cols)){
iContPosY = min(vecContCents[i].y,iContPosY);
iContPosX = vecContCents[i].x;
}
}
}
}
/*
*
*
*
* PAINT OVER UNINTERESTING PARTS OF THE BREAST ON THE THRESHOLDED IMAGE
*
*
*
*/
if (iContPosY < mMammo.rows){
int extremalX=pEdgeContourCopy[0].x;
int lastY=pEdgeContourCopy[0].y;
//int lastX=pEdgeContourCopy[0].x;
vector<cv::Point> pEdgeThrowAway;
for(auto i:pEdgeContourCopy){
if(lastY == i.y){
extremalX = bLeft?
((i.x>1)?min(extremalX,i.x):extremalX)
:
((i.x<mMammo.cols-5)?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 < mMammoThreshed.cols; x++){
mMammoThreshedCopy.at<uchar>(i.y, x) = 0;
}
}
}
}
/*
*
*
*
*
* FINDING THE BREAST THICKNESS
* (need to stop taking fatty part under breast into account)
*
*
*
*/
// Normalise the distance map to fit onto our graph.
cv::normalize(mMammoDist, mMammoDist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());
//mMammoDist.convertTo(mMammoDist,CV_8U);
vector<float> vecDistBright; // Average brightness at distance from black.
vector<int> vecDistAv; // Number of pixels at distance from black.
cv::Mat_<int> mMammoDistChar = mMammoDist;
vecDistBright.resize(256);
vecDistAv.resize(256);
cv::Mat mMammoCopy;
cv::cvtColor(mMammo, mMammoCopy, cv::COLOR_BGR2GRAY);
for(int i = 0; i < vecDistBright.size(); ++i){ vecDistBright[i] = uchar(0);}
for(int i = 0; i < vecDistAv.size(); ++i){ vecDistAv[i] = 0;}
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 < vecDistBright.size(); ++i){ if(vecDistAv[i] != 0){vecDistBright[i]/= float(vecDistAv[i]);}}
vector<float> vecDistBrightBrightest; // Average brightness at distance from black.
vector<int> vecDistAvBrightest; // Number of pixels at distance from black.
vecDistBrightBrightest.resize(256);
vecDistAvBrightest.resize(256);
for(int i = 0; i < vecDistBrightBrightest.size(); ++i){ vecDistBrightBrightest[i] = uchar(0);}
for(int i = 0; i < vecDistAvBrightest.size(); ++i){ vecDistAvBrightest[i] = 0;}
for(int i = 0; i < mMammo.cols; i++){
for(int j = 0; j < mMammo.rows; j++){
int iDist = int(mMammoDistChar(j,i));
if(int(mMammoCopy.at<uchar>(j,i)) >= vecDistBright[iDist]){
vecDistBrightBrightest[iDist]+=float(mMammoCopy.at<uchar>(j,i));
vecDistAvBrightest[iDist]++;}
}
}
for(int i = 0; i < vecDistBrightBrightest.size(); ++i){ if(vecDistAvBrightest[i] != 0){vecDistBrightBrightest[i]/= float(vecDistAvBrightest[i]);}}
int dist_w = histSize*2; int dist_h = 512;
cv::Mat distImage(dist_h, dist_w, CV_8UC3, cv::Scalar(0,0,0));
// 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());
/*
*
*
*
*
* DRAWING THE PICTURES
*
*
*
*
*/
#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(strFilename+"_dist.jpg", distImage );
#endif
#ifdef OL_DRAW_CORNER
cv::imwrite(strFilename+"_corner.jpg", mCornerThresh);
#endif
#ifdef OL_DRAW_DISTMAP
cv::imwrite(strFilename+"_distmap.jpg", mMammoDist);
#endif
#ifdef OL_DRAW_THRESH
cv::imwrite(strFilename+"_thresh.jpg", mMammoThreshedCopy);
#endif
#ifdef OL_DRAW_ALTERED
cv::imwrite(strFilename+"_alt.jpg", mMammo);
#endif
return 0;
}