/
imageprocessor.cpp
executable file
·1059 lines (900 loc) · 34.6 KB
/
imageprocessor.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
/**********************************************************************
* This file is part of the GNU Dental Radiograph Image Program, also
* known as "gdrip."
*
* gdrip is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Foobar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gdrip. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include <QDebug>
#include <QTime>
#include <qmath.h>
#include "math.h"
#include "imageprocessor.h"
ImageProcessor::ImageProcessor(QObject *parent) :
QObject(parent)
{
}
//---------------Sane functions to use----------------------------------
QImage ImageProcessor::equalizeHistogram(QImage input) {
QImage returnMe(input.width(),input.height(),QImage::Format_ARGB32);
QPainter painter(&returnMe);
QVector<float> occ = ImageProcessor::findOccurrences(input);
QVector<float> pdf;
float currentpdf =0;
for(int i=0;i<occ.count();i++) {
currentpdf += occ.value(i);
pdf.append(currentpdf);
}
for(int x=0;x<input.width();x++) {
for(int y=0;y<input.height();y++) {
int originalVal = qRed(input.pixel(x,y));
float pdfVal = pdf.value(originalVal);
int newVal = (int)(pdfVal * 255);
painter.fillRect(x,y,1,1,QColor(newVal,newVal,newVal));
}
}
return returnMe;
}
QVector<float> ImageProcessor::findOccurrences(QImage input, int offset) {
QVector<float> returnMe;
returnMe.fill(0,256);
float single = 1.0 / (input.width() * input.height());
for(int x=0;x<input.width();x++) {
for(int y=0;y<input.height();y++) {
int pixelIndex = qRed(input.pixel(x,y)) + offset;
pixelIndex = qMax(pixelIndex,0);
pixelIndex = qMin(pixelIndex,255);
float newVal = returnMe.value(pixelIndex)+single;
returnMe.replace(pixelIndex,newVal);
}
}
return returnMe;
}
QImage ImageProcessor::invertImage(QImage input) {
QImage returnMe(input);
returnMe.invertPixels();
return returnMe;
}
QImage ImageProcessor::spreadHistogram(QImage input) {
//Really, this is normalization, http://en.wikipedia.org/wiki/Normalization_(image_processing)
QImage returnMe(input.width(),input.height(),QImage::Format_ARGB32);
QPainter painter(&returnMe);
QVector<float> occ = ImageProcessor::findOccurrences(input);
int lowest =0;
bool goOn = true;
for(int i=0;(i<occ.size()) && goOn;i++) {
if(occ.at(i) == 0.0) {
lowest = i;
} else {
goOn = false;
}
}
int highest = 255;
goOn =true;
for(int i=occ.size()-1;(i>0) && goOn;i--) {
if(occ.at(i) == 0.0) {
highest = i;
} else {
goOn = false;
}
}
int diff = highest - lowest;
for(int x=0;x<input.width();x++) {
for(int y=0;y<input.height();y++) {
int value = qRed(input.pixel(x,y));
int newValue =(int) ((value - lowest) * (255.0 / diff));
//TODO: faster way to fill those pixels
painter.fillRect(x,y,1,1,QColor(newValue,newValue,newValue));
}
}
return returnMe;
}
QImage ImageProcessor::constrastImage(QImage original, int amount) {
QImage returnMe(original.width(),original.height(),QImage::Format_RGB32);
//QPainter painter(&returnMe);
//Taken from Wikipedia / GIMP at http://en.wikipedia.org/wiki/Image_editing#Contrast_change_and_brightening
double frac = ((amount - 50)*2) / 100.0;
#pragma omp parallel for
for(int x=0;x<original.width();x++) {
for(int y=0;y<original.height();y++) {
qreal value = (qRed(original.pixel(x,y)))/255.0;
qreal newVal = (value - 0.5) * (tan ((frac + 1) * 0.78539816339) ) + 0.5;
newVal = qMin(newVal*255,255.0);
newVal = qMax(newVal,0.0);
int setVal = (int)newVal;
//qDebug()<<"("<<x<<","<<y<<") is now "<<setVal;
//painter.fillRect(x,y,1,1,QColor(setVal,setVal,setVal));
returnMe.setPixel(x,y,qRgb(setVal,setVal,setVal));
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::intersection(QVector<QPoint> a, QVector<QPoint> b) {
QVector<QPoint> returnMe;
foreach(QPoint check, a) {
if(b.contains(check)) {
returnMe.append(check);
}
}
return returnMe;
}
//---------------Experimental functions start here----------------------------------
QVector<QPoint> ImageProcessor::findOcculsionFaster(QImage input) {
int radius = (int) (.1 * input.height());
//qDebug()<<"radius: " <<radius;
//left side
int bestLeftY=0;
int bestLeftYval=INT_MAX;
for(int currentY=radius;currentY<input.height()-radius;currentY++) {
int sum =0;
foreach(int x,ImageProcessor::regionVals(0,currentY,radius,input)) {
sum+=x;
}
if(sum < bestLeftYval) {
bestLeftY = currentY;
bestLeftYval = sum;
}
}
//right side
int bestRightY=0;
int bestRightYval=INT_MAX;
for(int currentY=radius;currentY<input.height()-radius;currentY++) {
int sum =0;
foreach(int x,ImageProcessor::regionVals(input.width()-1,currentY,radius,input)) {
sum+=x;
}
//qDebug()<<"Sum was: " <<sum;
if(sum < bestRightYval) {
bestRightY = currentY;
bestRightYval = sum;
}
}
int yPen = bestLeftY;
QVector<QPoint> returnMe;
for(int x=0;x<input.width();x++) { //go from left to right
int diff = bestRightY - yPen;
if(qAbs(diff) == (input.width() - x)) {
int movementDir = qAbs(diff)/diff; //+1 or -1 to push it in the right direction
yPen += movementDir;
} else {
float penUpTotal= ImageProcessor::calculateCenterValue(input,x,yPen+1);
float penRightTotal=ImageProcessor::calculateCenterValue(input,x,yPen);
float penDownTotal=ImageProcessor::calculateCenterValue(input,x,yPen-1);
float lowest = qMin(qMin(penUpTotal,penRightTotal),penDownTotal);
if(penUpTotal == lowest) {
yPen++;
} else if(penDownTotal == lowest) {
yPen--;
}
}
returnMe.append(QPoint(x,yPen));
}
return returnMe;
}
qreal ImageProcessor::vectorSum(QImage input, QPoint start, int fakeangle) {
bool goOn = true;
QPoint currentPoint;
qreal returnMe =0.0;
int length=1;
qreal angle = (fakeangle/180.0) * M_PI;
while(goOn) {
int newX = (int)(start.x() + (length * qCos(angle) ));
int newY = (int)(start.y() + (length * qSin(angle) ));
currentPoint = QPoint(newX,newY);
if(input.valid(currentPoint)) {
//qreal value = regionAvg(currentPoint.x(),currentPoint.y(),1,1,input);
qreal value = qRed(input.pixel(currentPoint));
//qDebug()<<"Adding " <<(value/length);
returnMe += (value/sqrt(length));
//qDebug()<<"Return me is now " <<returnMe;
length++;
} else {
goOn = false;
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::findOcculsionSlower(QImage input) {
QVector<QPoint> returnMe;
int radius = (int) (.1 * input.height());
//qDebug()<<"radius: " <<radius;
//left side
int bestLeftY=0;
int bestLeftYval=INT_MAX;
for(int currentY=radius;currentY<input.height()-radius;currentY++) {
int sum =0;
foreach(int x,ImageProcessor::regionVals(0,currentY,radius,input)) {
sum+=x;
}
if(sum < bestLeftYval) {
bestLeftY = currentY;
bestLeftYval = sum;
}
}
QPoint currentPoint(0,bestLeftY);
returnMe.append(currentPoint);
for(int lookX =0;lookX<input.width();lookX++) {
qreal bestAngle=0;
qreal bestAngleValue=1024;
for(int angle=-89;angle<90;angle++) {
qreal angleValue = vectorSum(input,currentPoint,angle);
if(angleValue < bestAngleValue) {
bestAngle = angle;
bestAngleValue = angleValue;
}
}
qDebug()<<"Best angle was: " << bestAngle << " with "<< bestAngleValue;
if(bestAngle > 15) {
QPoint nextPoint(currentPoint.x() +1, currentPoint.y() +1);
currentPoint = nextPoint;
} else if(bestAngle < -15) {
QPoint nextPoint(currentPoint.x() +1, currentPoint.y() - 1);
currentPoint = nextPoint;
} else {
QPoint nextPoint(currentPoint.x() +1, currentPoint.y());
currentPoint = nextPoint;
}
returnMe.append(currentPoint);
}
return returnMe;
}
float ImageProcessor::calculateCenterValue(QImage input, int seeX, int seeY) {
float returnMe =0;
for(int y=0;y<input.height();y++) {
float distance = qMax((float)qAbs(y - seeY),(float).01);
float value = qRed(input.pixel(seeX,y));
returnMe+= value / distance;
}
return returnMe;
}
QVector<int> ImageProcessor::regionVals(int startX, int startY, int r, QImage img) {
QVector<int> vals;
for(int x= startX - r; x < (startX + r); x++) {
for(int y = startY-r; y< (startY+r); y++) {
if(img.valid(x,y)) {
//int val = qRed(img.pixel(x,y));
//vals.append(val*val*val);
vals.append(qRed(img.pixel(x,y)));
}
}
}
return vals;
}
qreal ImageProcessor::regionAvg(int startX, int startY, int r, QImage img) {
return regionAvg(startX,startY,r,r,img);
}
qreal ImageProcessor::regionAvg(int startX, int startY, int w, int h, QImage img) {
qreal sum=0;
int counter=0;
for(int x= startX - w; x <= (startX + w); x++) {
for(int y = startY-h; y<= (startY+h); y++) {
if(img.valid(x,y)) {
sum +=qRed(img.pixel(x,y));
counter++;
}
}
}
return sum/counter;
}
QVector<QPair<QPoint, QColor> > ImageProcessor::findTeeth(QImage input) {
QTime t;
t.start();
QImage useMe = constrastImage(input,65);
QVector<QPoint> points = ImageProcessor::findOcculsionFaster(useMe);
qDebug("1Time elapsed: %d ms", t.elapsed());
QVector<float> occValues = findAverageAndStd(points,input);
int average = (int) occValues.at(0);
float standardDev = occValues.at(1);
qDebug("2Time elapsed: %d ms", t.elapsed());
int cutoff = average + (0 * standardDev);
QPair<QVector<QPoint>,QVector<QPoint> > outlines = ImageProcessor::findOutline(input,cutoff,points);
qDebug("3Time elapsed: %d ms", t.elapsed());
QVector<QPoint> allOutlines = outlines.first + outlines.second;
qDebug("4Time elapsed: %d ms", t.elapsed());
QVector<QPoint> inter = ImageProcessor::findInterProximal(input,points,allOutlines,cutoff);
qDebug("5Time elapsed: %d ms", t.elapsed());
QList<QVector<QPoint> > interProxGroups = groupPoints(inter,input.width(),input.height(),1,1,750);
qDebug("6Time elapsed: %d ms", t.elapsed());
QVector<QPoint> enamelPoints = findInterProximalEnamel(input,interProxGroups,points);
qDebug("7Time elapsed: %d ms", t.elapsed());
QVector<QPoint> badTooth = findCloseDecay(interProxGroups,input);
qDebug("8Time elapsed: %d ms", t.elapsed());
QVector<QPoint> badEnamel = intersection(enamelPoints,badTooth);
qDebug("9Time elapsed: %d ms", t.elapsed());
//QList<QVector<QPoint> > groupedEnamel = groupPoints(badEnamel,input.width(),input.height(),2,2,10);
QVector<QPair<QPoint, QColor> > returnMe;
foreach(QPoint point ,points) {
QPair<QPoint, QColor> addMe(point,QColor(0,200,0,150));
returnMe.append(addMe);
}
foreach(QPoint point ,allOutlines) {
QPair<QPoint, QColor> addMe(point,QColor(200,115,115,150));
returnMe.append(addMe);
}
// int counter=0;
// foreach(QVector<QPoint> group, interProxGroups) {
// //QColor useColor = QColor(QColor::colorNames().at(counter));
// foreach(QPoint point, group) {
// QPair<QPoint, QColor> addMe(point,QColor(5,200,5,50));
// returnMe.append(addMe);
// }
// counter++;
// }
foreach(QPoint point, badEnamel) {
QPair<QPoint, QColor> addMe(point,QColor(200,5,5,150));
returnMe.append(addMe);
}
return returnMe;
}
qreal ImageProcessor::findStdevArea(QImage input, QPoint center, int radius) {
return findStdevArea(input,center,radius*2,radius*2);
}
qreal ImageProcessor::findStdevArea(QImage input, QPoint center, int width, int height) {
QVector<int> localVals;
int xStart = qMax(0,center.x()-width);
int xEnd = qMin(input.width()-1,center.x()+width);
int yStart = qMax(0,center.y()-height);
int yEnd = qMin(input.height()-1,center.y()+height);
for(int scanX=xStart;scanX<=xEnd;scanX++) {
for(int scanY=yStart;scanY<=yEnd;scanY++) {
localVals.append(qRed(input.pixel(scanX,scanY)));
}
}
//Get the average
qreal sum=0;
foreach(int val, localVals) {
sum+=val;
}
qreal average = sum / localVals.count();
//Now to get the stDEV
qreal variance=0;
foreach(int val, localVals) {
variance += pow(average-val,2);
}
return sqrt( variance / localVals.count());
}
qreal ImageProcessor::calcVerticalConstrast(QImage input, QPoint center, int radius) {
qreal sum=0;
int xStart = qMax(0,center.x()-radius);
int xEnd = qMin(input.width()-1,center.x()+radius);
// int xStart = qMax( center.x()-1,0);
// int xEnd = qMin( center.x()+1,input.width()-1);
int yStart = qMax(0,center.y()-radius);
int yEnd = qMin(input.height()-1,center.y()+radius);
int count = yEnd-yStart;
for(int scanX=xStart;scanX<=xEnd;scanX++) {
for(int scanY=yStart;scanY<=yEnd;scanY++) {
int value = qRed(input.pixel(scanX,scanY));
if(scanY < center.y()) { //white is good
if(value > 40) {
sum++;
}
} else if(scanY > center.y()) { //black is good
if(value < 40) {
sum++;
}
}
}
}
return sum/count;
}
QPair<QVector<QPoint>,QVector<QPoint> > ImageProcessor::findOutline(QImage input, int cutoff, QVector<QPoint> occlusion) {
//int radius = (int) (.0001 * input.width()*input.height());
int radius = 25;
//int radius = 50;
QImage constrastedImg = constrastImage(input,65);
int offSetAmount = 10;
int pastOccAllowance = 5;
//First, lets get the top left change point
qreal highestStDev =0;
int bestStartY=0;
QVector<QPoint> maxPoints;
QVector<QPoint> manPoints;
QPoint leftOcc = occlusion.first();
for(int currentY=radius;currentY<leftOcc.y();currentY++) {
//Scan the area
qreal stDev = calcVerticalConstrast(constrastedImg,QPoint(0,currentY),radius);
//qDebug()<<"stDev: " <<stDev;
if(stDev > highestStDev) {
bestStartY = currentY;
highestStDev = stDev;
}
}
qDebug()<<"Best start: " <<bestStartY;
maxPoints.append(QPoint(0,bestStartY));
int currentY = bestStartY;
//Now, move right
for(int currentX=1;currentX<input.width();currentX++) {
int bestOffset =-1*offSetAmount;
qreal bestOffsetValue=-1;
for(int offsetY=-1*offSetAmount;offsetY<=offSetAmount;offsetY++) {
qreal value = calcVerticalConstrast(constrastedImg,
QPoint(currentX,currentY+offsetY),radius);
if(value > bestOffsetValue) {
bestOffset = offsetY;
bestOffsetValue = value;
}
}
if((currentY+bestOffset) > (occlusion.at(currentX).y() - pastOccAllowance)) {
bestOffset = -1;
}
currentY+=bestOffset;
maxPoints.append(QPoint(currentX,currentY));
}
//Now, do the same exact thing but for the bottom part
//Remember, we want the LOWEST score since we want black on top
qreal lowestStDev =INT_MAX;
bestStartY=0;
for(int currentY=leftOcc.y()+1;currentY<input.height();currentY++) {
//Scan the area
qreal stDev = calcVerticalConstrast(constrastedImg,QPoint(0,currentY),radius);
if(stDev < lowestStDev) {
bestStartY = currentY;
lowestStDev = stDev;
}
}
manPoints.append(QPoint(0,bestStartY));
currentY = bestStartY;
//Now, move right
for(int currentX=1;currentX<input.width();currentX++) {
int bestOffset =-10;
qreal bestOffsetValue=INT_MAX;
for(int offsetY=-10;offsetY<=10;offsetY++) {
qreal value = calcVerticalConstrast(constrastedImg,
QPoint(currentX,currentY+offsetY),radius);
if(value < bestOffsetValue) {
bestOffset = offsetY;
bestOffsetValue = value;
}
}
if((currentY+bestOffset) < (occlusion.at(currentX).y() + pastOccAllowance)) {
bestOffset = 1;
}
currentY+=bestOffset;
manPoints.append(QPoint(currentX,currentY));
}
QPair<QVector<QPoint>,QVector<QPoint> > returnMe(maxPoints,manPoints);
return returnMe;
}
QVector<QPoint> ImageProcessor::findSameX(QPoint needle, QVector<QPoint> haystack) {
QVector<QPoint> returnMe;
foreach(QPoint hay, haystack) {
if(needle.x() == hay.x()) {
returnMe.append(hay);
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::findSameY(QPoint needle, QVector<QPoint> haystack) {
QVector<QPoint> returnMe;
foreach(QPoint hay, haystack) {
if(needle.y() == hay.y()) {
returnMe.append(hay);
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::findInterProximal(QImage input, QVector<QPoint> occPoints, QVector<QPoint> outlinePoints, int cutOff){
QVector<QPoint> returnMe;
QImage constrastedImg = constrastImage(input,60);
int lowYCutoff = (int) (.05 * input.height());
int highYCutoff = (int) (.9 * input.height());
for(int lookAtX=0;lookAtX<input.width();lookAtX++) {
for(int lookAtY=lowYCutoff;lookAtY<highYCutoff;lookAtY++) {
int value = qRed(constrastedImg.pixel(lookAtX,lookAtY));
if(value <= cutOff) {
QVector<QPoint> occSame = findSameX(QPoint(lookAtX,lookAtY),outlinePoints);
if(occSame.count()==2) {
qreal highLookAt = lookAtY - (.05 * input.height());
qreal lowLookAt = lookAtY + (.05 * input.height());
if( (lowLookAt < occSame.first().y()) && (lowLookAt < occSame.last().y()) ) { //maxillary interproximal
returnMe.append(QPoint(lookAtX,lookAtY));
} else if( (highLookAt > occSame.first().y()) && (highLookAt > occSame.last().y()) ) { //mandibular interproximal
returnMe.append(QPoint(lookAtX,lookAtY));
}
}
}
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::findValidNeighbors(QPoint point, int **quickMap, int width, int height, int hozJump=1, int verJump=1) {
QVector<QPoint> returnMe;
int xStart = qMax(0,point.x()-hozJump);
int xEnd = qMin(width-1,point.x()+hozJump);
int yStart = qMax(0,point.y()-verJump);
int yEnd = qMin(height-1,point.y()+verJump);
for(int currentX=xStart;currentX<=xEnd;currentX++) {
for(int currentY=yStart;currentY<=yEnd;currentY++) {
if(quickMap[currentX][currentY] != -1) {
returnMe.append(QPoint(currentX,currentY));
}
}
}
return returnMe;
}
QList<QVector<QPoint> > ImageProcessor::groupPoints(QVector<QPoint> points, int width, int height, int hozDiff, int verDiff, int minSize) {
//http://en.wikipedia.org/wiki/Connected-component_labeling
QList<QVector<QPoint> > returnMe; //same as "linked"
QVector<QPoint> emptySet;
int** map = new int*[width]; //same as "labels"
int** quickMap = new int*[width]; //just to make a fast lookup for points
for(int i=0;i<width;i++) {
map[i] = new int[height];
quickMap[i] = new int[height];
for(int b=0;b<height;b++) {
map[i][b] = -1; //"empty"
quickMap[i][b] = -1;
}
}
foreach(QPoint point,points) {
quickMap[point.x()][point.y()] = 1;
returnMe.append(emptySet);
}
int counter=0;//same as "NextLabel"
//First pass
for(int x=0;x<width;x++) { //yeah, its column then row, but it shouldn't matter
for(int y=0;y<height;y++) {
if(quickMap[x][y] != -1) { //if it is not background
QPoint currentPoint(x,y);
QVector<QPoint> neighbors = findValidNeighbors(currentPoint,map,width,height,hozDiff,verDiff);
if(neighbors.count() == 0) {
returnMe[counter].append(currentPoint);
map[x][y] = counter;
counter++;
} else {
int lowest= INT_MAX;
foreach(QPoint point, neighbors) {
if(map[point.x()][point.y()] < lowest) {
lowest = map[point.x()][point.y()];
}
}
map[x][y] = lowest;
foreach(QPoint point, neighbors) { //go though each neghbor and fix their values
int oldVal = map[point.x()][point.y()];
if(oldVal != lowest) { //force that point and its allies to the "low"
QVector<QPoint> oldVec = returnMe[oldVal];
foreach(QPoint changePoint, oldVec) { //make all of its allies a new value
map[changePoint.x()][changePoint.y()] = lowest;
}
returnMe[lowest] += oldVec;
map[point.x()][point.y()] = lowest;
returnMe[oldVal] = emptySet;
}
}
returnMe[lowest].append(currentPoint);
}
}
}
}
QList<QVector<QPoint> > realReturnMe;
foreach(QVector<QPoint> group, returnMe) {
if(group.count() > minSize) { //more than 750 continous points
//qDebug()<<group.count();
realReturnMe.append(group);
}
}
//Free up the memory we used for the arrays
for(int i=0;i<width;i++) {
delete[] map[i];
delete[] quickMap[i];
}
delete[] map;
delete[] quickMap;
return realReturnMe;
}
QVector<QPoint> ImageProcessor::findCloseDecay(QList<QVector<QPoint> > interProxGroups, QImage input) {
QVector<QPoint> returnMe;
int jumpAmount =2;
int counterLimit = input.width() / 50;
foreach(QVector<QPoint> group, interProxGroups) {
QVector<QPoint> leftPoints;
QVector<QPoint> rightPoints;
//first get the high and low bounds of the entire group
int highestY = -1;
int lowestY = INT_MAX;
foreach(QPoint point, group) {
if(point.y() > highestY) {
highestY = point.y();
}
if(point.y() < lowestY) {
lowestY = point.y();
}
}
for(int y=lowestY;y<=highestY;y++) {
QVector<QPoint> sameY = findSameY(QPoint(-1,y),group);
QPoint highestX =QPoint(-1,y);
QPoint lowestX = QPoint(INT_MAX,y);
foreach(QPoint point, sameY) {
if(point.x() > highestX.x()) {
highestX = point;
}
if(point.x() < lowestX.x()) {
lowestX = point;
}
}
QPoint leftAdd = QPoint(lowestX.x()-jumpAmount,lowestX.y());
if(input.valid(leftAdd)) {
leftPoints.append(leftAdd);
}
QPoint rightAdd = QPoint(highestX.x()+ jumpAmount,highestX.y());
if(input.valid(rightAdd)) {
rightPoints.append(rightAdd);
}
}
QVector<float> leftStats = findAverageAndStd(leftPoints,input);
QVector<float> rightStats = findAverageAndStd(rightPoints,input);
float leftCut = leftStats.at(0) ;
float rightCut = rightStats.at(0);
//now move away from the interproximal area
foreach(QPoint point, leftPoints) { //move left from interproximal
int value = qRed(input.pixel(point));
if(value < leftCut) {
returnMe.append(point);
bool goOn = true;
int stopAtMe = 2* value;
QPoint leftwardEye = point;
int counter=0;
while(goOn) {
counter++;
leftwardEye = QPoint(leftwardEye.x()-1,leftwardEye.y());
qreal avg = regionAvg(leftwardEye.x(),leftwardEye.y(),2,input);
if(input.valid(leftwardEye) && (avg < stopAtMe) && (counter<counterLimit)) {
returnMe.append(leftwardEye);
} else {
goOn= false;
}
}
}
}
foreach(QPoint point, rightPoints) { //move right from interproximal
int value = qRed(input.pixel(point));
if(value < rightCut) {
returnMe.append(point);
bool goOn = true;
int stopAtMe = 2* value;
QPoint rightwardEye = point;
int counter=0;
while(goOn) {
counter++;
rightwardEye = QPoint(rightwardEye.x()+1,rightwardEye.y());
qreal avg = regionAvg(rightwardEye.x(),rightwardEye.y(),2,input);
if(input.valid(rightwardEye) && (avg < stopAtMe) && (counter<counterLimit)) {
returnMe.append(rightwardEye);
} else {
goOn= false;
}
}
}
}
}
QVector<QPoint> realReturnMe;
foreach(QPoint point,returnMe) {
if(input.valid(point)) {
realReturnMe.append(point);
}
}
return realReturnMe;
}
QVector<float> ImageProcessor::findAverageAndStd(QVector<QPoint> values, QImage input) {
QVector<float> returnMe;
float sum=0;
float variance=0;
foreach(QPoint point, values) {
sum +=qRed(input.pixel(point));
}
float average = sum/values.count();
foreach(QPoint point, values) {
variance += pow(average-qRed(input.pixel(point)),2);
}
returnMe.append(average);
returnMe.append(sqrt(variance/values.count()));
return returnMe;
}
QPoint ImageProcessor::closestPoint(QPoint start, QVector<QPoint> ends) {
QPoint returnMe = ends.first();
int bestDistance = INT_MAX;
foreach(QPoint end, ends) {
int currentDistance = (int) (pow(end.x()-start.x(),2) + pow(end.y()-start.y(),2));
//don't bother with the sqrt since we are not returning the distance
if(currentDistance < bestDistance) {
bestDistance = currentDistance;
returnMe = end;
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::makeLine(QPoint start, QPoint end) {
//http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#Simplification
QVector<QPoint> returnMe;
int dx = qAbs(end.x() - start.x());
int dy = qAbs(end.y() - start.y());
int sx, sy;
if(start.x() < end.x()) {
sx = 1;
} else {
sx = -1;
}
if(start.y() < end.y()) {
sy = 1;
} else {
sy = -1;
}
int err = dx-dy;
bool goOn = true;
while(goOn) {
returnMe.append(start);
if(end == start) {
goOn = false;
} else {
int e2 = 2* err;
if(e2 > (-1 * dy)) {
err = err - dy;
start.setX(start.x() + sx);
}
if(e2 < dx) {
err = err + dx;
start.setY(start.y() + sy);
}
}
}
return returnMe;
}
QList<QVector<QPoint> > ImageProcessor::findEmbrasures(QList<QVector<QPoint> > interProxGroups,
QVector<QPoint> occu, QVector<QPoint> maxOutline, QVector<QPoint> manOutline) {
QList<QVector<QPoint> > returnMe;
foreach(QVector<QPoint> group, interProxGroups) {
//first figure out if it is max or man
QPoint aPoint = group.first();
QVector<QPoint> occXVal = findSameX(aPoint,occu);
if(occXVal.count() > 0) {
QVector<QPoint> addMe;
if(aPoint.y() < occXVal.first().y()) { //maxillary
//first find the lowest point
QPoint lowest(-1,-1);
foreach(QPoint point, group) {
if(point.y() > lowest.y()) {
lowest = point;
}
}
QPoint start(lowest);
QPoint stop = closestPoint(lowest,maxOutline);
addMe = makeLine(start,stop);
} else { //mandibular
//first find the highest point
QPoint highest(INT_MAX,INT_MAX);
foreach(QPoint point, group) {
if(point.y() < highest.y()) {
highest = point;
}
}
QPoint start(highest);
QPoint stop = closestPoint(highest,manOutline);
addMe = makeLine(start,stop);
}
returnMe.append(addMe);
}
}
return returnMe;
}
QVector<QPoint> ImageProcessor::findInterProximalEnamel(QImage input, QList<QVector<QPoint> > interProxGroups, QVector<QPoint> occPoints) {
QVector<QPoint> returnMe;
int starter = (int)(input.width() * 0.005);
int ender = (int)(input.width() * 0.05);
foreach(QVector<QPoint> group, interProxGroups) {
QList<QPoint> leftPoints;
QList<QPoint> rightPoints;
//first get the high and low bounds of the entire group
int highestY = -1;
int lowestY = INT_MAX;
foreach(QPoint point, group) {
if(point.y() > highestY) {
highestY = point.y();
}
if(point.y() < lowestY) {
lowestY = point.y();
}
}
for(int y=lowestY;y<=highestY;y++) {
QVector<QPoint> sameY = findSameY(QPoint(-1,y),group);
QPoint highestX =QPoint(-1,y);
QPoint lowestX = QPoint(INT_MAX,y);
foreach(QPoint point, sameY) {
if(point.x() > highestX.x()) {
highestX = point;
}
if(point.x() < lowestX.x()) {
lowestX = point;
}
}
leftPoints.append(lowestX);
rightPoints.append(highestX);
}
int jumpAmount = 4;
//now move away from the interproximal area
foreach(QPoint point, leftPoints) { //move left from interproximal
qreal currentAverage=0;
qreal nextAverage=0;
QPoint currentPoint(point.x() - starter, point.y());
QPoint nextPoint(currentPoint.x() - jumpAmount, point.y() );
int endX = point.x() - ender;
while ((currentAverage < (nextAverage +5) ) && (currentPoint.x() > endX)) {
currentAverage = regionAvg(currentPoint.x(),currentPoint.y(),jumpAmount,2,input);
nextAverage = regionAvg(nextPoint.x(),nextPoint.y(),jumpAmount,2,input);
currentPoint = nextPoint;
nextPoint= QPoint(currentPoint.x() - jumpAmount, point.y() );
}
if(input.valid(currentPoint) && (currentPoint.x() > endX)) {
returnMe.append(currentPoint);
while(currentPoint.x() != point.x()) {
currentPoint.setX(currentPoint.x()+1);
returnMe.append(currentPoint);
}
}
}
foreach(QPoint point, rightPoints) { //move right from interproximal
qreal currentAverage=0;
qreal nextAverage=0;
QPoint currentPoint(point.x() + starter, point.y());
QPoint nextPoint(currentPoint.x() + jumpAmount, point.y() );