-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindcountour-new.h
1551 lines (1208 loc) · 46.5 KB
/
findcountour-new.h
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 <stdio.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
using namespace std;
using namespace cv;
class FindCountour
{
private:
string save_path_;
string output_file_;
int main_count_;
ofstream file_handler_;
public:
FindCountour(char *save_path):
save_path_(save_path), output_file_(""), main_count_(0)
{}
FindCountour(char *save_path, char *output_file, int start_count):
save_path_(save_path), output_file_(output_file), main_count_(start_count)
{
file_handler_.open(output_file, ios::app);
}
~FindCountour()
{
if(file_handler_)
{
file_handler_.close();
}
}
IplImage * translation(IplImage *src,int dix,int diy)//平移
{
IplImage *pSrcImage = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
for(int i=0;i<src->height;i++)
{
for(int j=0;j<src->width;j++)
{
CvScalar color=cvGet2D(src,i,j);
cvSet2D(pSrcImage,(i+diy)%src->height,(j+dix)%src->width,color);
}
}
//cvNamedWindow("dst");
//cvShowImage("dst",pSrcImage);
//cvWaitKey(0);
return pSrcImage;
//cvReleaseImage(&pSrcImage);
//cvReleaseImage(&src);
cvDestroyWindow("dst");
}
IplImage* transform(IplImage *src,double scale)//缩放
{
IplImage* dst=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U, src->nChannels);
cvCopy(src,dst,0);
cvSetImageROI(dst,cvRect(121,45,137,208));
cvNamedWindow("dst", CV_WINDOW_AUTOSIZE);
cvShowImage("dst", dst); //dst 是剪裁后的图片
cvWaitKey(0);
//double scale=0.5;
CvSize czSize;
czSize.width = src->width * scale;
czSize.height = src->height * scale;
IplImage *pSrcImage = cvCreateImage(czSize, src->depth, src->nChannels);//缩放后的照片
cvResize(dst, pSrcImage, CV_INTER_AREA); //缩放图片
cvNamedWindow("pSrcImage", CV_WINDOW_AUTOSIZE);
cvShowImage("pSrcImage", pSrcImage);
cvWaitKey(0);
cvResetImageROI(dst);
return dst;
}
IplImage* rotateImage2(IplImage* src,int degree)
{
double angle = degree * CV_PI / 180.;
double a = sin(angle), b = cos(angle);
int width=src->width, height=src->height;
//旋转后的新图尺寸
int width_rotate= int(height * fabs(a) + width * fabs(b));
int height_rotate=int(width * fabs(a) + height * fabs(b));
IplImage* img_rotate = cvCreateImage(cvSize(width_rotate, height_rotate), src->depth, src->nChannels);
//cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength = sqrt((double)width * width + (double)height *height) + 10;
int tempX = (tempLength + 1) / 2 - width / 2;
int tempY = (tempLength + 1) / 2 - height / 2;
//int tempX=0;
//int tempY=0;
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), src->depth, src->nChannels);
//cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(src, temp, NULL);
cvResetImageROI(temp);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = b;
m[1] = a;
m[3] = -m[1];
m[4] = m[0];
// 将旋转中心移至图像中间
m[2] = w * 0.5f;
m[5] = h * 0.5f;
CvMat M = cvMat(2, 3, CV_32F, m);
cvGetQuadrangleSubPix(temp, img_rotate, &M);
cvReleaseImage(&temp);
return img_rotate;//旋转后的图片
}
void rotateImage1(IplImage* img, IplImage *img_rotate,int degree)
{
//旋转中心为图像中心
CvPoint2D32f center;
center.x=float (img->width/2.0+0.5);
center.y=float (img->height/2.0+0.5);
//计算二维旋转的仿射变换矩阵
float m[6];
CvMat M = cvMat( 2, 3, CV_32F, m );
cv2DRotationMatrix( center, degree,1, &M);
//变换图像,并用黑色填充其余值
cvWarpAffine(img,img_rotate, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0) );
}
//
// IplImage *
void originalpicture(IplImage *src,int Threshold,double scale,float degree,int dix,int diy)
{
IplImage *dst = cvCreateImage(cvGetSize(src), src->depth, src->nChannels);
cvCopy(src,dst,0);
// cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
// cvShowImage("src", dst); //灰度图
//cvWaitKey(0);
IplImage *gray = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvCvtColor(dst, gray, CV_BGR2GRAY);
IplImage *binary = cvCreateImage(cvGetSize(gray), IPL_DEPTH_8U, 1);
cvThreshold(gray, binary, Threshold, 255, CV_THRESH_BINARY);
//cvNamedWindow("gray", CV_WINDOW_AUTOSIZE);
//cvShowImage("gray", binary); //灰度图
//cvWaitKey(0);
CvMemStorage *pcvMStorage = cvCreateMemStorage();
CvSeq *pcvSeq = NULL;
cvFindContours(binary, pcvMStorage, &pcvSeq, sizeof(CvContour), CV_RETR_LIST , CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));//找出轮廓
IplImage *pOutlineImage = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
//IplImage *pOutlineImage = cvCreateImage(cvGetSize(dst), IPL_DEPTH_8U, 3);
cvDrawContours(pOutlineImage, pcvSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), 1, 2);//画出轮廓
//cvNamedWindow("grapcvSeqy", CV_WINDOW_AUTOSIZE);
//cvShowImage("grapcvSeqy", pOutlineImage); //灰度图
//cvWaitKey(0);
IplImage *pOutImage = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
// IplImage *pOutImage = cvCreateImage(cvGetSize(dst), IPL_DEPTH_8U, 3);
//................................过滤出物体的轮廓.................................................//
double maxarea=0;
double max=0;
CvSeq* tmppcvseq =pcvSeq;
for(;tmppcvseq !=0; tmppcvseq = tmppcvseq->h_next)
{
// area=fabs(cvContourArea(pcvSeq,CV_WHOLE_SEQ));
double area=fabs(cvContourArea(tmppcvseq));
if(area >maxarea)
{
maxarea=area;
}
}
//cout<<"maxarea"<<maxarea<<endl;
CvSeq *cPrev = pcvSeq;
CvSeq *cNext = NULL;
bool first= true;
for(CvSeq *c = pcvSeq;c!= NULL;c= cNext)
{
double area1 = fabs(cvContourArea(c) );
if(area1 <maxarea *0.01||area1==maxarea)
{
cNext = c->h_next;
cvClearSeq(c);
//cvClearMemStorage(c->storage);//回收内存
continue;
}//if
else
{
if(first)
pcvSeq = c;
first = false;
cvDrawContours(pOutlineImage, c, CV_RGB(255,0,0), CV_RGB(0,255,0), 0,2,8);
}
cNext= c->h_next;
}//for first_constours;
//cvNamedWindow("pOutlineImage1", CV_WINDOW_AUTOSIZE);
//cvShowImage("pOutlineImage1", pOutlineImage); //轮廓图
//cvWaitKey(0);
CvSeq* tmpseq =pcvSeq;
for(;tmpseq !=0; tmpseq = tmpseq->h_next)
{
double Area=fabs(cvContourArea(tmpseq));
//cout<<"Area===="<<Area<<endl;
}//判断是否只有一个area
//。。。。。。。。。。。。。。。。。。。。。最小外接矩形。。。。。。。。。。。。。。。。。。。。。。。。。。。//
IplImage *dst_img = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
//IplImage *dst_img = cvCreateImage(cvGetSize(dst), IPL_DEPTH_8U, 3);
CvBox2D rect = cvMinAreaRect2(pcvSeq);
//cout<<"center. "<<rect.center.x<<" "<<rect.center.y<<endl;// 盒子的中心
//cout<<"rect1.size.height=="<<rect.size.height<<" "<<"rect1.size.width=="<<rect.size.width<<endl;//盒子的长和宽
//cout<<"rect1.angle=="<<rect.angle<<endl;//注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角
//。。。。。。。。。。。。。。。。。。。。。。。。如果最小外接矩形是正的。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。//
if(rect.angle==0)
{
anglee=degree;
CvPoint2D32f rect_pts0[4];
cvBoxPoints(rect, rect_pts0);
int npts = 4;
CvPoint rect_pts[4], *pt = rect_pts;
for (int rp=0; rp<4; rp++)
{
rect_pts[rp]= cvPointFrom32f(rect_pts0[rp]);
//cout<<rp<<" "<<".x"<<rect_pts[rp].x<<" "<<".y"<<rect_pts[rp].y<<endl;//最小外接矩形的顶点坐标
}
cvPolyLine(dst_img, &pt, &npts, 1, 1, CV_RGB(0,255,0), 2);
//cvNamedWindow("dst_img", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst_img", dst_img); //dst_img最小外接矩形的轮廓
//cvWaitKey(0);
cvSetImageROI(dst,cvRect(rect_pts[1].x,rect_pts[1].y,rect.size.width,rect.size.height));////剪裁
//cvNamedWindow("dst", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst", dst); //dst 是剪裁后的图片
//cvWaitKey(0);
CvSize czSize;
czSize.width = rect.size.width * scale;
czSize.height = rect.size.height * scale;
IplImage *pSrcImage = cvCreateImage(czSize, src->depth, src->nChannels);//缩放后的照片
cvResize(dst, pSrcImage, CV_INTER_AREA); //pSrcImage是缩放后的图片
Mat image2=pSrcImage;
//cvNamedWindow("suofang", CV_WINDOW_AUTOSIZE);
//cvShowImage("suofang", pSrcImage); //pSrcImage 是缩放后的图片
//cvWaitKey(0);
// CvSize czSize1;
czSize1.width =src->width *2;
czSize1.height = src->height *2 ;
// czSize1.width =dst->width *2;
//czSize1.height = dst->height *2 ;
//cout<<"====="<<endl;
//cout<<"czSize.width==="<<czSize.width<<" "<<"czSize.height "<<czSize.height <<endl;
//cout<<"czSize1.width ==="<<czSize1.width <<" "<<"czSize1.height "<<czSize1.height <<endl;
Mat write1;
write1= Mat(czSize1,CV_8UC3);
//Rect rect0(50,50,czSize.width,czSize.height) ;
Rect rect1(rect_pts[1].x,rect_pts[1].y,czSize.width,czSize.height);
image2.copyTo(write1(rect1));
//imshow("image2",image2);
IplImage *write=&IplImage(write1);
//
// IplImage *Image = cvCreateImage(czSize1, src->depth, src->nChannels);
double angle = degree * CV_PI / 180.;
double a = sin(angle), b = cos(angle);
int width=write->width, height=write->height;
//旋转后的新图尺寸
int width_rotate= int(height * fabs(a) + width * fabs(b));
int height_rotate=int(width * fabs(a) + height * fabs(b));
IplImage* Image = cvCreateImage(cvSize(width_rotate, height_rotate), write->depth, write->nChannels);
//cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength = sqrt((double)width * width + (double)height *height) + 10;
int tempX = (tempLength + 1) / 2 - width / 2;
int tempY = (tempLength + 1) / 2 - height / 2;
//int tempX=0;
//int tempY=0;
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), write->depth, write->nChannels);
//cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(write, temp, NULL);
cvResetImageROI(temp);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = b;
m[1] = a;
m[3] = -m[1];
m[4] = m[0];
// 将旋转中心移至图像中间
m[2] = w * 0.5f;
m[5] = h * 0.5f;
CvMat M = cvMat(2, 3, CV_32F, m);
cvGetQuadrangleSubPix(temp, Image, &M);
cvReleaseImage(&temp);
//
// Image=rotateImage2(write,degree);//Image是旋转后的图片
// cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
//cvShowImage("Image", Image); //dst 是剪裁后的图片
//cvWaitKey(0);
//
// //。。。。。。。。。。。。deichuzuobiao 。。。。。。。。。。。。。。。。。。。。
IplImage *gray2 = cvCreateImage(cvGetSize(Image), IPL_DEPTH_8U, 1);
cvCvtColor(Image, gray2, CV_BGR2GRAY);
IplImage *binary2 = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 1);
cvThreshold(gray2, binary2, 200, 255, CV_THRESH_BINARY);
// cvNamedWindow("binary", CV_WINDOW_AUTOSIZE);
//cvShowImage("binary", binary2); //binary 是二值化后的图片
//cvWaitKey(0);
IplImage *out = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 3);
IplImage *out2 = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 3);
CvMemStorage *pcvMStorage2 = cvCreateMemStorage();
CvSeq *pcvSeq2 = NULL;
cvFindContours(binary2, pcvMStorage2, &pcvSeq2, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvDrawContours(out, pcvSeq2, CV_RGB(255,0,0), CV_RGB(0,255,0), 1,2);
//cvNamedWindow("out1", CV_WINDOW_AUTOSIZE);
//cvShowImage("out1", out); //lunkuo
//cvWaitKey(0);
double aa=czSize.height*czSize.width;
double bb=czSize1.height*czSize1.width*3;
r=aa/bb;
//cout<<"r==="<<r<<endl;
double maxarea2=0;
CvSeq* tmp =pcvSeq2;
for(;tmp !=0; tmp = tmp->h_next)
{
// area=fabs(cvContourArea(pcvSeq,CV_WHOLE_SEQ));
double area3=fabs(cvContourArea(tmp));
//if(area3!=0)
//{cout<<"area3"<<area3<<endl;}
if(area3 >maxarea2)
{
maxarea2=area3;
}
}
// cout<<"pcvSeq->"<<pcvSeq->elem_size<<endl;
//cout<<"maxarea2"<<maxarea2<<endl;
CvSeq *fPrev = pcvSeq2;
CvSeq *fNext = NULL;
bool firstt= true;
for(CvSeq *f = pcvSeq2;f!= NULL;f= fNext)
{
double area3 = fabs(cvContourArea(f) );
if(area3<maxarea2 *r||area3==maxarea2)
{
fNext = f->h_next;
cvClearSeq(f);
//cvClearMemStorage(c->storage);//回收内存
continue;
}//if
else
{
if(firstt)
pcvSeq2 = f;
firstt = false;
cvDrawContours(out2, f, CV_RGB(255,0,0), CV_RGB(0,255,0), 0,2,8);
}
fNext= f->h_next;
}//for first_constours;
//cout<<"pcvSeq1"<<pcvSeq2->total<<endl;
CvSeq* tmq =pcvSeq2;
for(;tmq !=0; tmq = tmq->h_next)
{
double Area1=fabs(cvContourArea(tmq));
//cout<<"Area1===="<<Area1<<endl;
}
//cvNamedWindow("out2", CV_WINDOW_AUTOSIZE);
//cvShowImage("out2", out2); //灰度图
//cvWaitKey(0);
IplImage *dst_img2 = cvCreateImage(cvGetSize(Image), IPL_DEPTH_8U, 3);
CvBox2D rect3 = cvMinAreaRect2(pcvSeq2);
cout<<endl<<endl;
//cout<<"center3. "<<rect3.center.x<<" "<<rect3.center.y<<endl;// 盒子的中心
//cout<<"rect3.size.height=="<<rect3.size.height<<" "<<"rect3.size.width=="<<rect3.size.width<<endl;//盒子的长和宽
//cout<<"rect3.angle=="<<rect3.angle<<endl;//注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角
CvPoint2D32f rect3_pts0[4];
cvBoxPoints(rect3, rect3_pts0);
int npt3 = 4;
CvPoint rect3_pts[4], *ptt = rect3_pts;
for (int rp=0; rp<4; rp++)
{
rect3_pts[rp]= cvPointFrom32f(rect3_pts0[rp]);
//cout<<rp<<" "<<".x"<<rect3_pts[rp].x<<" "<<".y"<<rect3_pts[rp].y<<endl;//最小外接矩形的顶点坐标
}
cvPolyLine(dst_img2, &ptt, &npt3, 1, 1, CV_RGB(0,255,0), 2);
//cvNamedWindow("dst_img2", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst_img2", dst_img2); //dst_img最小外接矩形的轮廓
//cvWaitKey(0);
IplImage* dst4=cvCreateImage(cvGetSize(Image),IPL_DEPTH_8U, Image->nChannels);
cvCopy(Image,dst4,0);
CvPoint p0,p1,p2,p3;
int x0=rect3_pts[0].x;
int y0=rect3_pts[0].y;
int x1=rect3_pts[1].x;
int y1=rect3_pts[1].y;
int x2=rect3_pts[2].x;
int y2=rect3_pts[2].y;
int x3=rect3_pts[3].x;
int y3=rect3_pts[3].y;
p0.x=x1;
p0.y=y2;
p1.x=x3;
p1.y=y2;
p2.x=x1;
p2.y=y0;
p3.x=x3;
p3.y=y0;
//cout<<"p0.x"<<p0.x<<"p0.y"<<p0.y<<endl;
//cout<<"p1.x"<<p1.x<<"p1.y"<<p1.y<<endl;
//cout<<"p2.x"<<p2.x<<"p2.y"<<p2.y<<endl;
//cout<<"p3.x"<<p3.x<<"p3.y"<<p3.y<<endl;
cvLine( dst4, p0, p1,CV_RGB(0,255,0), 1, 8,0 );
cvLine( dst4, p1, p3,CV_RGB(0,255,0), 1, 8,0 );
cvLine( dst4, p3, p2,CV_RGB(0,255,0), 1, 8,0 );
cvLine( dst4, p2, p0,CV_RGB(0,255,0), 1, 8,0 );
//cvNamedWindow("dst4", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst4", dst4); //dst4是画了方框的图片
//cvWaitKey(0);
//..........................................................................................//
Mat write2,image3;
IplImage* dst33=cvCreateImage(cvGetSize(Image), src->depth, src->nChannels);
//IplImage *image11=cvCreateImage(cvGetSize(Image), src->depth, src->nChannels);
// IplImage *image=cvCreateImage(cvGetSize(Image), src->depth, src->nChannels);
cvCopy(Image,dst33,0);
write2= Mat(czSize1,CV_8UC3);
// CvSize cvSize;
cvSize4.height=abs(p3.x-p2.x);
cvSize4.width=abs(p0.y-p2.y);
cvSetImageROI(dst33,cvRect(p0.x,p0.y,cvSize4.height, cvSize4.width));
image3=dst33;
Rect rect11(10,10,cvSize4.height, cvSize4.width);
image3.copyTo(write2(rect11));
IplImage * image11=&IplImage(write2);
//
//
////.............................................................
double angle1 =0 * CV_PI / 180.;
double a1 = sin(angle1), b1 = cos(angle1);
int width1=image11->width, height1=image11->height;
//旋转后的新图尺寸
int width_rotate1= int(height1 * fabs(a1) + width1 * fabs(b1));
int height_rotate1=int(width1 * fabs(a1) + height1 * fabs(b1));
//IplImage* image = cvCreateImage(cvSize(width_rotate1, height_rotate1), image11->depth, image11->nChannels);
imagee= cvCreateImage(cvSize(width_rotate1, height_rotate1), image11->depth, image11->nChannels);
//cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength1 = sqrt((double)width1 * width1 + (double)height1 *height1) + 10;
int tempX1 = (tempLength1 + 1) / 2 - width1 / 2;
int tempY1 = (tempLength1 + 1) / 2 - height1 / 2;
IplImage* temp1 = cvCreateImage(cvSize(tempLength1, tempLength1), image11->depth, image11->nChannels);
//cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp1, cvRect(tempX1, tempY1, width1, height1));
cvCopy(image11, temp1, NULL);
cvResetImageROI(temp1);
//旋转数组map
//[ m0 m1 m2 ] ===> [ A11 A12 b1 ]
//[ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m1[6];
int w1 = temp1->width;
int h1 = temp1->height;
m1[0] = b1;
m1[1] = a1;
m1[3] = -m1[1];
m1[4] = m1[0];
// 将旋转中心移至图像中间
m1[2] = w1 * 0.5f;
m1[5] = h1 * 0.5f;
CvMat M1 = cvMat(2, 3, CV_32F, m1);
cvGetQuadrangleSubPix(temp1, imagee, &M1);
cvReleaseImage(&temp1);
//.....................................
//image=rotateImage2(image11,0);
// cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
//cvShowImage("Image", image); //Image 是剪裁,旋转,缩放的图片
//cvWaitKey(0);
//cout<<"rect.height==="<<cvSize4.height<<" "<<"cvSize.width==="<<cvSize4.width<<endl;
//cout<<"pic.height==="<<czSize1.height<<" "<<"pic.width==="<< czSize1.width <<endl;
// cout<<"能移动y距离==="<<czSize1.height-cvSize4.width-12<<" "<<"能移动x距离"<<czSize1.width-cvSize4.height-12<<endl;
//.......................................................................//
//cout<<"p0.x"<<" "<<10<<"p0.y"<<" "<<10<<endl;
//cout<<"p1.x"<<p1.x-p0.x+10<<" "<<"p1.y"<<p1.y-p0.y+10<<endl;
//cout<<"p2.x"<<p2.x-p0.x+10<<" "<<"p2.y"<<p2.y-p0.y+10<<endl;
//cout<<"p3.x"<<p3.x-p0.x+10<<" "<<"p3.y"<<p3.y-p0.y+10<<endl;
{
p00.x=0+10;
p00.y=0+10;
p11.x=p1.x-p0.x+10;
p11.y=p1.y-p0.y+10;
p22.x=p2.x-p0.x+10;
p22.y=p2.y-p0.y+10;
p33.x=p3.x-p0.x+10;
p33.y=p3.y-p0.y+10;
//cvLine( image, p00, p11,CV_RGB(0,255,0), 1, 8,0 );
//cvLine( image, p11, p33,CV_RGB(0,255,0), 1, 8,0 );
//cvLine( image, p33, p22,CV_RGB(0,255,0), 1, 8,0 );
//cvLine( image, p22, p00,CV_RGB(0,255,0), 1, 8,0 );
//cout<<endl<<endl;
//cout<<"平移后坐标"<<endl<<endl;
//cout<<"p0.x==="<<" "<<10+dix<<" "<<"p0.y==="<<10+diy<<endl;
//cout<<"p1.x==="<<p1.x-p0.x+dix+10<<" "<<"p1.y==="<<p1.y-p0.y+diy+10<<endl;
//cout<<"p2.x==="<<p2.x-p0.x+dix+10<<" "<<"p2.y==="<<p2.y-p0.y+diy+10<<endl;
//cout<<"p3.x==="<<p3.x-p0.x+dix+10<<" "<<"p3.y==="<<p3.y-p0.y+diy+10<<endl;
//cout<<"转动角度==="<< degree<<endl;
}
//cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
//cvShowImage("Image", image); //Image 是剪裁,旋转,缩放的图片
//cvWaitKey(0);
cvResetImageROI(dst);
cvResetImageROI(dst33);
cvReleaseImage(&dst);
cvReleaseImage(&gray);
cvReleaseImage(&binary);
cvReleaseImage(&pOutlineImage);
cvReleaseImage(&pOutImage);
cvReleaseImage(&dst_img);
cvReleaseImage(&pSrcImage);
cvReleaseImage(&Image);
cvReleaseImage(&gray2);
cvReleaseImage(&binary2);
cvReleaseImage(&out);
cvReleaseImage(&out2);
cvReleaseMemStorage(&pcvMStorage);
cvReleaseMemStorage(&pcvMStorage2);
cvReleaseImage(&dst_img2);
cvReleaseImage(&dst4);
cvReleaseImage(&dst33);
//return image;
//............................................................................................................................//
}
if(rect.angle!=0)//如果最小外接矩形是倾斜的
{
double angle = -rect.angle * CV_PI / 180.;
double a = sin(angle), b = cos(angle);
int width=dst->width, height=dst->height;
//旋转后的新图尺寸
int width_rotate= int(height * fabs(a) + width * fabs(b));
int height_rotate=int(width * fabs(a) + height * fabs(b));
IplImage* zhuan = cvCreateImage(cvSize(width_rotate, height_rotate), dst->depth, dst->nChannels);
//cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength = sqrt((double)width * width + (double)height *height) + 10;
int tempX = (tempLength + 1) / 2 - width / 2;
int tempY = (tempLength + 1) / 2 - height / 2;
//int tempX=0;
//int tempY=0;
IplImage* temp = cvCreateImage(cvSize(tempLength, tempLength), dst->depth, dst->nChannels);
//cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp, cvRect(tempX, tempY, width, height));
cvCopy(dst, temp, NULL);
cvResetImageROI(temp);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m[6];
int w = temp->width;
int h = temp->height;
m[0] = b;
m[1] = a;
m[3] = -m[1];
m[4] = m[0];
// 将旋转中心移至图像中间
m[2] = w * 0.5f;
m[5] = h * 0.5f;
CvMat M = cvMat(2, 3, CV_32F, m);
cvGetQuadrangleSubPix(temp, zhuan, &M);
cvReleaseImage(&temp);
//IplImage *zhuan = cvCreateImage(cvGetSize(dst_img),dst_img->depth, dst_img->nChannels);
//IplImage * img_rotate = cvCreateImage(cvSize(width_rotate,height_rotate),dst->depth, dst->nChannels);
//zhuan=rotateImage2(dst,-rect.angle);//旋转后的图片
//zhuan=rotateImage2(dst,img_rotate,-rect.angle);
anglee=-rect.angle-degree;
//cvNamedWindow("zhuan", CV_WINDOW_AUTOSIZE);
//cvShowImage("zhuan", img_rotate); //binary 是二值化后的图片
//cvWaitKey(0);
IplImage* dst2=cvCreateImage(cvGetSize(zhuan),IPL_DEPTH_8U, src->nChannels);
cvCopy(zhuan,dst2,0);
IplImage *gray1 = cvCreateImage(cvGetSize(zhuan), IPL_DEPTH_8U, 1);
cvCvtColor(zhuan, gray1, CV_BGR2GRAY);
IplImage *binary1 = cvCreateImage(cvGetSize(gray1), IPL_DEPTH_8U, 1);
cvThreshold(gray1, binary1, 200, 255, CV_THRESH_BINARY);
//// cvNamedWindow("binary", CV_WINDOW_AUTOSIZE);
////cvShowImage("binary", binary1); //binary 是二值化后的图片
////cvWaitKey(0);
CvMemStorage *pcvMStorage1 = cvCreateMemStorage();
CvSeq *pcvSeq1 = NULL;
cvFindContours(binary1, pcvMStorage1, &pcvSeq1, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
// cvNamedWindow("binary1", CV_WINDOW_AUTOSIZE);
//cvShowImage("binary1", binary1); //binary 是二值化后的图片
//cvWaitKey(0);
//cvFindContours(binary,pcvMStorage,&pcvSeq,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
IplImage *dst_img1 = cvCreateImage(cvGetSize(zhuan), IPL_DEPTH_8U, 3);
CvBox2D rect1 = cvMinAreaRect2(pcvSeq1);
cout<<endl<<endl;
//cout<<"center2. "<<rect1.center.x<<" "<<rect1.center.y<<endl;// 盒子的中心
//cout<<"rect2.size.height=="<<rect1.size.height<<" "<<"rect2.size.width=="<<rect1.size.width<<endl;//盒子的长和宽
//cout<<"rect2.angle=="<<rect1.angle<<endl;//注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角
CvPoint2D32f rect_pts1[4];
cvBoxPoints(rect1, rect_pts1);
int npt = 4;
CvPoint rect_ptss[4], *pt1 = rect_ptss;
for (int rp=0; rp<4; rp++)
{
rect_ptss[rp]= cvPointFrom32f(rect_pts1[rp]);
//cout<<rp<<" "<<".x"<<rect_ptss[rp].x<<" "<<".y"<<rect_ptss[rp].y<<endl;//最小外接矩形的顶点坐标
}
cvPolyLine(dst_img1, &pt1, &npt, 1, 1, CV_RGB(0,255,0), 2);
//cvNamedWindow("dst_img1", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst_img1", dst_img1); //dst_img1t 是最小外接矩形的轮廓图
//cvWaitKey(0);
// CvSize czSize;
// czSize.width =rect.size.width;
// czSize.height =rect.size.height;
//IplImage* dst=cvCreateImage(czSize,IPL_DEPTH_8U, src->nChannels);
cvSetImageROI(dst2,cvRect(rect_ptss[1].x,rect_ptss[1].y,rect1.size.width,rect1.size.height));////剪裁
//cvNamedWindow("dst2", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst2", dst2); //dst 是剪裁后的图片
//cvWaitKey(0);
CvSize czSize;
czSize.width = rect.size.width * scale;
czSize.height = rect.size.height * scale;
IplImage *pSrcImage = cvCreateImage(czSize, src->depth, src->nChannels);//缩放后的照片
cvResize(dst2, pSrcImage, CV_INTER_AREA); //缩放后的图片
Mat image2=pSrcImage;
//cvNamedWindow("pSrcImage", CV_WINDOW_AUTOSIZE);
//cvShowImage("pSrcImage", pSrcImage); //dst 是剪裁后的图片
//cvWaitKey(0);
// CvSize czSize1;
czSize1.width = src->width *2;
czSize1.height =src->height *2;
//cout<<"czSize.width==="<<czSize.width<<" "<<"czSize.height "<<czSize.height <<endl;
//cout<<"czSize1.width ==="<<czSize1.width <<" "<<"czSize1.height "<<czSize1.height <<endl;
if(czSize.width>czSize1.width||czSize.height>czSize1.height)
{
cout<<"放大尺寸超出图片尺寸"<<endl;
}
Mat write1;
write1= Mat(czSize1,CV_8UC3);
Rect rect(rect_ptss[1].x,rect_ptss[1].y,czSize.width,czSize.height);
image2.copyTo(write1(rect));
IplImage *write=&IplImage(write1);
//IplImage *Image = cvCreateImage(czSize1, src->depth, src->nChannels);
//........................................................................................
double angle1 =degree * CV_PI / 180.;
double a1 = sin(angle), b1 = cos(angle);
int width1=write->width, height1=write->height;
//旋转后的新图尺寸
int width_rotate1= int(height1 * fabs(a1) + width1 * fabs(b1));
int height_rotate1=int(width1 * fabs(a1) + height1 * fabs(b1));
IplImage* Image = cvCreateImage(cvSize(width_rotate1, height_rotate1), write->depth, write->nChannels);
//cvZero(img_rotate);
//保证原图可以任意角度旋转的最小尺寸
int tempLength1 = sqrt((double)width1 * width1 + (double)height1 *height1) + 10;
int tempX1 = (tempLength1 + 1) / 2 - width1 / 2;
int tempY1 = (tempLength1 + 1) / 2 - height1 / 2;
//int tempX=0;
//int tempY=0;
IplImage* temp1 = cvCreateImage(cvSize(tempLength1, tempLength1), write->depth, write->nChannels);
//cvZero(temp);
//将原图复制到临时图像tmp中心
cvSetImageROI(temp1, cvRect(tempX1, tempY1, width1, height1));
cvCopy(write, temp1, NULL);
cvResetImageROI(temp1);
//旋转数组map
// [ m0 m1 m2 ] ===> [ A11 A12 b1 ]
// [ m3 m4 m5 ] ===> [ A21 A22 b2 ]
float m1[6];
int w1 = temp1->width;
int h1 = temp1->height;
m1[0] = b;
m1[1] = a;
m1[3] = -m1[1];
m1[4] = m1[0];
// 将旋转中心移至图像中间
m1[2] = w1 * 0.5f;
m1[5] = h1 * 0.5f;
CvMat M1 = cvMat(2, 3, CV_32F, m1);
cvGetQuadrangleSubPix(temp1, Image, &M1);
cvReleaseImage(&temp1);
////......................................................................................................
//
// //Image=rotateImage2(write,degree);//旋转后的图片
// // cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
// //cvShowImage("Image", Image); //灰度图
// //cvWaitKey(0);
//
//
////...........................得出处理后的目标图片的坐标..........................................................................
//
IplImage *gray2 = cvCreateImage(cvGetSize(Image), IPL_DEPTH_8U, 1);
cvCvtColor(Image, gray2, CV_BGR2GRAY);
IplImage *binary2 = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 1);
cvThreshold(gray2, binary2, 200, 255, CV_THRESH_BINARY);
// cvNamedWindow("binary", CV_WINDOW_AUTOSIZE);
//cvShowImage("binary", binary2); //binary 是二值化后的图片
//cvWaitKey(0);
IplImage *out = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 3);
IplImage *out2 = cvCreateImage(cvGetSize(gray2), IPL_DEPTH_8U, 3);
CvMemStorage *pcvMStorage2 = cvCreateMemStorage();
CvSeq *pcvSeq2 = NULL;
cvFindContours(binary2, pcvMStorage2, &pcvSeq2, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
cvDrawContours(out, pcvSeq2, CV_RGB(255,0,0), CV_RGB(0,255,0), 1,2);
//cvNamedWindow("out1", CV_WINDOW_AUTOSIZE);
//cvShowImage("out1", out); //灰度图
//cvWaitKey(0);
double maxarea2=0;
CvSeq* tmp =pcvSeq2;
for(;tmp !=0; tmp = tmp->h_next)
{
// area=fabs(cvContourArea(pcvSeq,CV_WHOLE_SEQ));
double area3=fabs(cvContourArea(tmp));
//if(area3!=0)
//{cout<<"area3"<<area3<<endl;}
if(area3 >maxarea2)
{
maxarea2=area3;
}
}
// cout<<"pcvSeq->"<<pcvSeq->elem_size<<endl;
//cout<<"maxarea2"<<maxarea2<<endl;
double aa=czSize.height*czSize.width;
double bb=czSize1.height*czSize1.width*3;
r=aa/bb;
//cout<<"r==="<<r<<endl;
CvSeq *fPrev = pcvSeq2;
CvSeq *fNext = NULL;
bool firstt= true;
for(CvSeq *f = pcvSeq2;f!= NULL;f= fNext)
{
double area3 = fabs(cvContourArea(f) );
if(area3<maxarea2 *r||area3==maxarea2)
{
fNext = f->h_next;
cvClearSeq(f);
//cvClearMemStorage(c->storage);//回收内存
continue;
}//if
else
{
if(firstt)
pcvSeq2 = f;
firstt = false;
cvDrawContours(out2, f, CV_RGB(255,0,0), CV_RGB(0,255,0), 0,2,8);
}
fNext= f->h_next;
}//for first_constours;
CvSeq* tmq =pcvSeq2;
for(;tmq !=0; tmq = tmq->h_next)
{
double Area1=fabs(cvContourArea(tmq));
//cout<<"Area1===="<<Area1<<endl;
}
//cvNamedWindow("out2", CV_WINDOW_AUTOSIZE);
//cvShowImage("out2", out2);
//cvWaitKey(0);
//
//
//
IplImage *dst_img2 = cvCreateImage(cvGetSize(Image), IPL_DEPTH_8U, 3);
CvBox2D rect3 = cvMinAreaRect2(pcvSeq2);
cout<<endl<<endl;
//cout<<"center3. "<<rect3.center.x<<" "<<rect3.center.y<<endl;// 盒子的中心
//cout<<"rect3.size.height=="<<rect3.size.width<<" "<<"rect3.size.width=="<<rect3.size.height<<endl;//盒子的长和宽
//cout<<"rect3.angle=="<<rect3.angle<<endl;//注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角
CvPoint2D32f rect3_pts0[4];
cvBoxPoints(rect3, rect3_pts0);
int npt3 = 4;
CvPoint rect3_pts[4], *ptt = rect3_pts;
for (int rp=0; rp<4; rp++)
{
rect3_pts[rp]= cvPointFrom32f(rect3_pts0[rp]);
//cout<<rp<<" "<<".x"<<rect3_pts[rp].x<<" "<<".y"<<rect3_pts[rp].y<<endl;//最小外接矩形的顶点坐标
}
cvPolyLine(dst_img2, &ptt, &npt3, 1, 1, CV_RGB(0,255,0), 2);
//cvNamedWindow("dst_img2", CV_WINDOW_AUTOSIZE);
//cvShowImage("dst_img2", dst_img2); //dst_img最小外接矩形的轮廓
//cvWaitKey(0);
//..........................求出正矩形坐标.......................................//
IplImage* dst4=cvCreateImage(cvGetSize(Image),IPL_DEPTH_8U, Image->nChannels);
cvCopy(Image,dst4,0);
CvPoint p0,p1,p2,p3;
int x0=rect3_pts[0].x;
int y0=rect3_pts[0].y;
int x1=rect3_pts[1].x;
int y1=rect3_pts[1].y;
int x2=rect3_pts[2].x;
int y2=rect3_pts[2].y;
int x3=rect3_pts[3].x;
int y3=rect3_pts[3].y;