-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoCrop.cpp
More file actions
729 lines (666 loc) · 21.1 KB
/
Copy pathAutoCrop.cpp
File metadata and controls
729 lines (666 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
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
#include "AutoCrop.h"
// C++ includes
#include <cmath>
#include <cfloat>
// Qt includes.
#include <QTextStream>
#include <QFile>
#include <QDebug>
#include <QApplication>
#include <QPoint>
#include <QRect>
// OpenCV includes
#include <highgui.h>
#include <cvaux.h>
#include <opencv/cv.h>
class AutoCrop::Private
{
public:
QString path;
QString output;
};
AutoCrop::AutoCrop(QObject* const parent)
: QObject(parent), d(new Private)
{
}
AutoCrop::~AutoCrop()
{
delete d;
}
void AutoCrop::setImagePath(const QString& path)
{
d->path=path;
}
QString AutoCrop::output() const
{
return d->output;
}
void AutoCrop::postProgress(int p, const QString& t)
{
qDebug() << "\n" << p << "% - " << t << "\n";
emit signalProgress(p);
qApp->processEvents();
}
QRect AutoCrop::autoOuterCrop()
{
IplImage* img = cvLoadImage(d->path.toStdString().c_str());
int i,j;
int breakflag=0;
float channel0,channel1, channel2;
int topRow=-1, topColumn=-1;
int bottomRow=-1, bottomColumn=-1;
int leftRow=-1,leftColumn=-1;
int rightRow=-1,rightColumn=-1;
postProgress(10,"Data Init.");
//----------------------Finding the outer boundaries of the image (i.e. with black portions)
/*
This would be done in 4 steps
1. Search column wise:
(a) From the left to the right, this is to get the left boundary
(b) From the right to the left, this is to get the right boundary
2. Search row wise :
(a) From the top to the bottom, this is to get the top boundary
(b) From the bottom to the top, this is to get the bottom boundary
*/
//1(a) Traversing the image from top to bottom, left to right, to get left boundary
breakflag=0;
for(i=0;i<img->width;i++)
{
for(j=0;j<img->height;j++)
{
channel0=cvGet2D(img,j,i).val[0];
channel1=cvGet2D(img,j,i).val[1];
channel2=cvGet2D(img,j,i).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
leftRow=j;
leftColumn=i;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug("Done Till step 1(a)");
postProgress(30,"Left extracted");
//1(b) Traversing the image from top to bottom, right to left, to get right boundary
breakflag=0;
for(i=(img->width-1);i>=0;i--)
{
for(j=0;j<img->height;j++)
{
channel0=cvGet2D(img,j,i).val[0];
channel1=cvGet2D(img,j,i).val[1];
channel2=cvGet2D(img,j,i).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
rightRow=j;
rightColumn=i;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 1(b)";
postProgress(50,"Right Extracted");
//2(a) Traversing the image left to right, top to down, to get top boundary
breakflag=0;
for(i=0;i<img->height;i++)
{
for(j=0;j<img->width;j++)
{
//qDebug()<<"Pixel : ("<<i<<","<<j<<")\n";
channel0=cvGet2D(img,i,j).val[0];
channel1=cvGet2D(img,i,j).val[1];
channel2=cvGet2D(img,i,j).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
topRow=i;
topColumn=j;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 2(a)";
postProgress(70,"Top Extracted");
//2(b) Traversing the image from left to right, bottom up, to get lower boundary
breakflag=0;
for(i=(img->height-1);i>=0;i--)
{
for(j=0;j<img->width;j++)
{
//qDebug()<<"BPixel : ("<<i<<","<<j<<")\n";
channel0=cvGet2D(img,i,j).val[0];
channel1=cvGet2D(img,i,j).val[1];
channel2=cvGet2D(img,i,j).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
bottomRow=i;
bottomColumn=j;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 2(b)";
postProgress(90,"Bottom Extracted");
//------making the required output--------------------
d->output.append("TopMost Pixel : ( ");
d->output.append(QString::number(topRow));
d->output.append(", ");
d->output.append(QString::number(topColumn));
d->output.append(")\nBottomMost Pixel : ( ");
d->output.append(QString::number(bottomRow));
d->output.append(", ");
d->output.append(QString::number(bottomColumn));
d->output.append(")\nLeftMost Pixel : ( ");
d->output.append(QString::number(leftRow));
d->output.append(", ");
d->output.append(QString::number(leftColumn));
d->output.append(")\nRightMost Pixel : ( ");
d->output.append(QString::number(rightRow));
d->output.append(", ");
d->output.append(QString::number(rightColumn));
d->output.append(")\nDONE");
postProgress(91,"Verbose Output done");
QPoint p1;
p1.setX(leftColumn);
p1.setY(topRow);
QPoint p2;
p2.setX(rightColumn);
p2.setY(bottomRow);
QRect cropArea;
cropArea.setTopLeft(p1);
cropArea.setBottomRight(p2);
//----releasing Images---------------------------------
cvReleaseImage(&img);
return cropArea;
}
QRect AutoCrop::autoInnerCrop()
{
CvMat* img = cvLoadImageM(d->path.toStdString().c_str());
//CvMat* img = cvLoadImage(d->path.toStdString().c_str());
int i,j;
int breakflag=0;
float channel0,channel1, channel2;
int topRow=-1, topColumn=-1;
int bottomRow=-1, bottomColumn=-1;
int leftRow=-1,leftColumn=-1;
int rightRow=-1,rightColumn=-1;
postProgress(10,"Data Init.");
//----------------------Finding the outer boundaries of the image (i.e. with black portions)
/*
This would be done in 4 steps
1. Search column wise:
(a) From the left to the right, this is to get the left boundary
(b) From the right to the left, this is to get the right boundary
2. Search row wise :
(a) From the top to the bottom, this is to get the top boundary
(b) From the bottom to the top, this is to get the bottom boundary
*/
//1(a) Traversing the image from top to bottom, left to right, to get left boundary
breakflag=0;
for(i=0;i<img->cols;i++)
{
for(j=0;j<img->rows;j++)
{
channel0=cvGet2D(img,j,i).val[0];
channel1=cvGet2D(img,j,i).val[1];
channel2=cvGet2D(img,j,i).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
leftRow=j;
leftColumn=i;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug("Done Till step 1(a)");
postProgress(30,"Left extracted");
//1(b) Traversing the image from top to bottom, right to left, to get right boundary
breakflag=0;
for(i=(img->cols-1);i>=0;i--)
{
for(j=0;j<img->rows;j++)
{
channel0=cvGet2D(img,j,i).val[0];
channel1=cvGet2D(img,j,i).val[1];
channel2=cvGet2D(img,j,i).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
rightRow=j;
rightColumn=i;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 1(b)";
postProgress(50,"Right Extracted");
//2(a) Traversing the image left to right, top to down, to get top boundary
breakflag=0;
for(i=0;i<img->rows;i++)
{
for(j=0;j<img->cols;j++)
{
//qDebug()<<"Pixel : ("<<i<<","<<j<<")\n";
channel0=cvGet2D(img,i,j).val[0];
channel1=cvGet2D(img,i,j).val[1];
channel2=cvGet2D(img,i,j).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
topRow=i;
topColumn=j;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 2(a)";
postProgress(70,"Top Extracted");
//2(b) Traversing the image from left to right, bottom up, to get lower boundary
breakflag=0;
for(i=(img->rows-1);i>=0;i--)
{
for(j=0;j<img->cols;j++)
{
//qDebug()<<"BPixel : ("<<i<<","<<j<<")\n";
channel0=cvGet2D(img,i,j).val[0];
channel1=cvGet2D(img,i,j).val[1];
channel2=cvGet2D(img,i,j).val[2];
if(channel0!=0 || channel1!=0 || channel2!=0)
{
//we have found our pixel
bottomRow=i;
bottomColumn=j;
breakflag=1;
}
}
if(breakflag==1)
break;
}
qDebug()<<"Done Till step 2(b)";
postProgress(90,"Bottom Extracted");
//------making the required output--------------------
d->output.append("TopMost Pixel : ( ");
d->output.append(QString::number(topRow));
d->output.append(", ");
d->output.append(QString::number(topColumn));
d->output.append(")\nBottomMost Pixel : ( ");
d->output.append(QString::number(bottomRow));
d->output.append(", ");
d->output.append(QString::number(bottomColumn));
d->output.append(")\nLeftMost Pixel : ( ");
d->output.append(QString::number(leftRow));
d->output.append(", ");
d->output.append(QString::number(leftColumn));
d->output.append(")\nRightMost Pixel : ( ");
d->output.append(QString::number(rightRow));
d->output.append(", ");
d->output.append(QString::number(rightColumn));
d->output.append(")\nDONE");
postProgress(91,"Verbose Output done");
QPoint p1;
p1.setX(leftColumn);
p1.setY(topRow);
QPoint p2;
p2.setX(rightColumn);
p2.setY(bottomRow);
QRect crop;
crop.setTopLeft(p1);
crop.setBottomRight(p2);
//crop Image to outerCrop
IplImage* result = cvCreateImage(cvSize(crop.width(),crop.height()),IPL_DEPTH_32F,3);
int ni,nj;
qDebug() << "From "<<crop.top()<<" to "<<crop.bottom()<<" & "<<crop.left()<<" to "<<crop.right();
for(i=crop.top(),ni=0;i<=crop.bottom();i++,ni++)
{
for(j=crop.left(),nj=0;j<=crop.right();j++,nj++)
{
cvSet2D(result,ni,nj,cvGet2D(img,i,j));
}
}
cvReleaseMat(&img);
//work for innerCrop
//---------------------threshold the image
IplImage* threshold = cvCreateImage(cvSize(crop.width(),crop.height()),IPL_DEPTH_8U,1);
float sum=0.0;
// for(i=0;i<result->height;i++)
// {
// for(j=0;j<result->width;j++)
// {
// sum=cvGet2D(result,i,j).val[0] + cvGet2D(result,i,j).val[1] + cvGet2D(result,i,j).val[2];
// if(sum==0)
// cvSet2D(threshold,i,j,cvScalar(0));
// else
// cvSet2D(threshold,i,j,cvScalar(255));
// }
// }
//----------------------new threshold image option
int toggleflag1=0,toggleflag2=0;
//-----initialize
for(i=0;i<result->height;i++)
for(j=0;j<result->width;j++)
cvSet2D(threshold,i,j,cvScalar(0));
//----------fill white points on horizontal scan
for(i=0;i<result->height;i++)
{
toggleflag1=-1;
toggleflag2=-1;
for(j=0;j<result->width;j++)
{
sum=cvGet2D(result,i,j).val[0] + cvGet2D(result,i,j).val[1] + cvGet2D(result,i,j).val[2];
if(sum>0)
{
toggleflag1=j;
break;
}
}
for(j=(result->width-1);j>=0;j--)
{
sum=cvGet2D(result,i,j).val[0] + cvGet2D(result,i,j).val[1] + cvGet2D(result,i,j).val[2];
if(sum>0)
{
toggleflag2=j;
break;
}
}
if(toggleflag1>=0)
for(j=toggleflag1;j<=toggleflag2;j++)
cvSet2D(threshold,i,j,cvScalar(255));
}
//----------fill black points on vertical scan
for(j=0;j<result->width;j++)
{
toggleflag1=-1;
toggleflag2=-2;
for(i=0;i<result->height;i++)
{
sum=cvGet2D(result,i,j).val[0] + cvGet2D(result,i,j).val[1] + cvGet2D(result,i,j).val[2];
if(sum>0)
{
toggleflag1=i;
break;
}
}
for(i=(result->height-1);i>=0;i--)
{
sum=cvGet2D(result,i,j).val[0] + cvGet2D(result,i,j).val[1] + cvGet2D(result,i,j).val[2];
if(sum>0)
{
toggleflag2=i;
break;
}
}
if(toggleflag1>=0)
for(i=0;i<toggleflag1;i++)
cvSet2D(threshold,i,j,cvScalar(0));
if(toggleflag2>=0)
for(i=(toggleflag2+1);i<(result->height);i++)
cvSet2D(threshold,i,j,cvScalar(0));
}
qDebug() << "Thresholding Complete\n";
//---------work on the whitespace
cvShowImage("Thresholded Image",threshold);
qDebug() << "Image showed successfully";
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 100;
p[2] = 0;
cvSaveImage("thesholdedfile.jpg", threshold,p);
// return (crop);
//---------------------inner crop
int limitcolumn = threshold->width;
int limitrow = threshold->height;
int centeri=((limitrow/2)-1);
int centerj=((limitcolumn/2)-1);
int startrighti=0, startrightj=0, endrighti=0, endrightj=0;
int startlefti=0, startleftj=0, endlefti=0, endleftj=0;
int startupi=0, startupj=0, endupi=0, endupj=0;
int startdowni=0, startdownj=0, enddowni=0, enddownj=0;
int travelright=0, travelleft=0, travelup=0, traveldown=0;
int rightmargin=0, leftmargin=0, bottommargin=0, topmargin=0;
int counter=0;
bool fixtopmargin = false;
bool fixrightmargin = false;
bool fixleftmargin = false;
bool fixbottommargin = false;
int count=0;
endupi = centeri;
endupj = centerj;
travelright = traveldown = -1;
travelleft = travelup = 0;
while(true)
{
switch((counter%4))
{
case 0 : //travelling right
travelright += 2;
if(fixrightmargin==true)
travelright--;
if(fixleftmargin==true)
travelright--;
if(fixtopmargin==true)
{
if(fixrightmargin==false)
endrightj++;
break;
}
startrighti=endupi;
startrightj=endupj;
endrightj=startrightj+travelright;
if(endrightj >= limitcolumn)
{
endrightj = limitcolumn-1;
fixrightmargin=true;
rightmargin=limitcolumn-1;
counter++;
travelright--;
}
i=startrighti;
j=startrightj;
for(j=startrightj+1;j<=endrightj;j++)
{
if(cvGet2D(threshold,i,j).val[0]==0)
{
//we have found an empty space
fixtopmargin=true;
topmargin=i;
endupi++;
travelup--;
break;
}
}
endrighti=startrighti;
break;
case 1 : //travelling down
traveldown += 2;
if(fixbottommargin==true)
traveldown--;
if(fixtopmargin==true)
traveldown--;
if(fixrightmargin==true)
{
if(fixbottommargin==false)
enddowni++;
//endrightj--;
// qDebug() << "Travelling down : Case Skipped\n";
break;
}
startdowni=endrighti;
startdownj=endrightj;
enddowni = startdowni + traveldown;
if(enddowni >= limitrow)
{
enddowni = limitrow-1;
counter++;
bottommargin=limitrow-1;
fixbottommargin=true;
traveldown--;
}
i=startdowni;
j=startdownj;
for(i=startdowni+1; i<=enddowni; i++)
{
if(cvGet2D(threshold,i,j).val[0]==0)
{
//we have found an empty space
fixrightmargin=true;
rightmargin=j;
endrightj--;
travelright--;
break;
}
}
enddownj=startdownj;
break;
case 2 : //travelling left
travelleft += 2;
if(fixleftmargin==true)
travelleft--;
if(fixrightmargin==true)
travelleft--;
if(fixbottommargin==true)
{
if(fixleftmargin==false)
endleftj--;
break;
}
startlefti=enddowni;
startleftj=enddownj;
endleftj = startleftj - travelleft;
if(endleftj < 0)
{
endleftj = 0;
counter++;
leftmargin=0;
fixleftmargin=true;
travelleft--;
}
i=startlefti;
j=startleftj;
for(j=startleftj-1;j>=endleftj;j--)
{
if(cvGet2D(threshold,i,j).val[0]==0)
{
//we have found an empty space
fixbottommargin=true;
bottommargin=i;
enddowni--;
traveldown--;
break;
}
}
endlefti=startlefti;
break;
case 3 : //travelling up
travelup += 2;
if(fixbottommargin==true)
travelup--;
if(fixtopmargin==true)
travelup--;
if(fixleftmargin==true)
{
if(fixtopmargin==false)
endupi--;
break;
}
startupi=endlefti;
startupj=endleftj;
endupi = startupi - travelup;
// // qDebug() << "Travel : "<<travelup<<"\n";
if(endupi < 0)
{
endupi = 0;
counter++;
fixtopmargin=true;
topmargin=0;
travelup--;
}
i=startupi;
j=startupj;
for(i=startupi-1; i>=endupi; i--)
{
if(cvGet2D(threshold,i,j).val[0]==0)
{
//we have found an empty space
fixleftmargin=true;
leftmargin=j;
endleftj++;
travelleft--;
break;
}
}
endupj=startupj;
break;
}
counter++;
if ( fixbottommargin==true && fixtopmargin==true && fixleftmargin==true && fixrightmargin==true)
break;
}
qDebug() << "Endupi : "<<endupi<<"\n";
qDebug() << "Endupj : "<<endupj<<"\n";
qDebug() << "Endrighti : "<<endrighti<<"\n";
qDebug() << "Endrightj : "<<endrightj<<"\n";
qDebug() << "Enddowni : "<<enddowni<<"\n";
qDebug() << "Enddownj : "<<enddownj<<"\n";
qDebug() << "Endlefti : "<<endlefti<<"\n";
qDebug() << "Endleftj : "<<endleftj<<"\n";
qDebug() << "Done\n";
qDebug() << "\n";
qDebug() << "Left Margin : "<<leftmargin<<"\n";
qDebug() << "Right Margin : "<<rightmargin<<"\n";
qDebug() << "Top Margin : "<<topmargin<<"\n";
qDebug() << "Bottom Margin : "<<bottommargin<<"\n";
//----------------------releasing images
cvReleaseImage(&threshold);
cvReleaseImage(&result);
QPoint icp1;
icp1.setX(leftColumn+leftmargin+1);
icp1.setY(topRow+topmargin+1);
QPoint icp2;
icp2.setX(leftColumn+rightmargin-1);
icp2.setY(topRow+bottommargin-1);
QRect cropArea;
cropArea.setTopLeft(icp1);
cropArea.setBottomRight(icp2);
return(cropArea);
}
void AutoCrop::ShowOutput(QRect crop)
{
IplImage* img = cvLoadImage(d->path.toStdString().c_str());
IplImage* result = cvCreateImage(cvSize(crop.width(),crop.height()),img->depth,img->nChannels);
int i,j;
int ni,nj;
qDebug() << "From "<<crop.top()<<" to "<<crop.bottom()<<" & "<<crop.left()<<" to "<<crop.right();
for(i=crop.top(),ni=0;i<=crop.bottom();i++,ni++)
{
for(j=crop.left(),nj=0;j<=crop.right();j++,nj++)
{
cvSet2D(result,ni,nj,cvGet2D(img,i,j));
}
}
cvShowImage("Result", result);
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 100;
p[2] = 0;
cvSaveImage("croppedfile.jpg", result,p);
cvReleaseImage(&img);
cvReleaseImage(&result);
}