-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureCompletion.cpp
927 lines (829 loc) · 26.2 KB
/
TextureCompletion.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
#include "TextureCompletion.h"
void mergeImg(Mat & dst, Mat &src1, Mat &src2)
{
int rows = src1.rows;
int cols = src1.cols + 5 + src2.cols;
CV_Assert(src1.type() == src2.type());
dst.create(rows, cols, src1.type());
src1.copyTo(dst(Rect(0, 0, src1.cols, src1.rows)));
src2.copyTo(dst(Rect(src1.cols + 5, 0, src2.cols, src2.rows)));
}
int sqr(int x)
{
return x * x;
}
int dist(Vec3b V1, Vec3b V2)
{
return sqr(int(V1[0]) - int(V2[0])) + sqr(int(V1[1]) - int(V2[1])) + sqr(int(V1[2]) - int(V2[2]));
/*double pr = (V1[0] + V2[0]) * 0.5;
return sqr(V1[0] - V2[0]) * (2 + (255 - pr) / 256)
+ sqr(V1[1] - V2[1]) * 4
+ sqr(V1[2] - V2[2]) * (2 + pr / 256);*/
}
//全黑色是0,全白色是255
// mask: 二值化的mask图像
// Linemask:暂时理解为结构线
// mat:是之前带有mask的没有进行纹理补全的结果
// result:最后输出的结果
void TextureCompletion2(Mat1b _mask, Mat1b LineMask, const Mat &mat, Mat &result)
{
int N = _mask.rows;
int M = _mask.cols;
int* test_mask;
int knowncount = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
knowncount += (_mask.at<uchar>(i, j) == 255);
//统计输入mask中纯白色像素点的个数
}
//做了一种优化处理,判断是黑色点多还是白色点多,从而进行后面的操作
// mask部分是0白色??
if (knowncount * 2< N * M)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
_mask.at<uchar>(i, j) = 255 - _mask.at<uchar>(i, j);
}
//新建一个my_mask和sum_diff
vector<vector<int> >my_mask(N, vector<int>(M, 0)), sum_diff(N, vector<int>(M, 0));
//Linemask扩大这后面白色的255*100变成灰色,黑色依旧是0
/*for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
LineMask.at<uchar>(i, j) = LineMask.at<uchar>(i, j) * 100;*/
//result = mat.clone();
/*imshow("mask", _mask);
imshow("linemask", LineMask);*/
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//mymask对应于mask(mask中的黑色遮挡部分mymask为0,mask白色部分mymask为1)
my_mask[i][j] = (_mask.at<uchar>(i, j) == 255);
//如果mymask中的一个位置坐标既是遮挡,又是LineMask中的灰色部分,则标注为2
if (my_mask[i][j] == 0 && LineMask.at<uchar>(i, j) > 0)
{
my_mask[i][j] = 2;
}
}
/*
my_mask的结构
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 2 0 1 ---结构线
1 0 2 2 2 0 1 ---结构线
1 0 0 0 0 0 1
1 1 1 1 1 1 1
*/
int bs = 3;
int step = 6 * bs;
auto usable(my_mask); //自动生成了一个和mymask相同类型的变量
int to_fill = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
int filled = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
to_fill += (my_mask[i][j] == 0);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//遍历全图,如果my_mask[i][j] == 1说明不需要填充则继续
if (my_mask[i][j] == 1)
continue;
//对于mymask中需要被填充的地方
//在一个step的矩形邻域内,需要把usable标记为2
//usable[k][l] == 2说明需要被填充
//(我的理解是在原来的mask周围扩大了需要补全纹理的范围,缩小了可用的纹理的范围)
int k0 = max(0, i - bs), k1 = min(N - 1, i + bs);
int l0 = max(0, j - bs), l1 = min(M - 1, j + bs);
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
usable[k][l] = 2;
}
//按照usable中2的地方生成一个黑白图,其中白色是需要填充的地方值为2
//也就是说实际要填充的部分比正常的黑白图要大
Mat use = _mask.clone();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (usable[i][j] == 2)
use.at<uchar>(i, j) = 255;
else use.at<uchar>(i, j) = 0;
int itertime = 0;
Mat match;
match = result.clone();
while (true)
{
itertime++;
int x, y, cnt = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//略过不需要填充的地方以及轮廓线部分
if (my_mask[i][j] != 0) continue;
//此时my_mask[i][j]==0
//首先要找到需要填充的区域的边界点
//edge用于判断这个点是不是边界
bool edge = false;
int k0 = max(0, i - 1), k1 = min(N - 1, i + 1);
int l0 = max(0, j - 1), l1 = min(M - 1, j + 1);
//取到像素点的一个小邻域8个像素点,如果这个邻域内的点有一个是1则最后edge==true
/*
1 1 1
1 0 1
1 1 1
*/
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
edge |= (my_mask[k][l] == 1); //或等于 edge = edge | (my_mask==1);
if (!edge) continue;
//如果edge==true说明当前像素点是边界点
//------猜测后面需要对这个像素点进行融合运算!-------
k0 = max(0, i - bs), k1 = min(N - 1, i + bs);
l0 = max(0, j - bs), l1 = min(M - 1, j + bs);
int tmpcnt = 0;
//此时取到当前像素点周围的一个step大小的矩形邻域
//tmpcnt计算了这个矩形邻域内不需要填充的像素点的个数
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
tmpcnt += (my_mask[k][l] == 1);
if (tmpcnt > cnt)
{
cnt = tmpcnt;
x = i;
y = j;
}
//结束for循环的时候xy记录了边界点
}
//如果cnt==-1说明所有edge都是false,也就是说所有mymask[i,j]都是1都是不需要填充,跳出while
if (cnt == -1) break;
bool debug = false;
bool debug2 = false;
//这部分再次遍历全图;比较一个邻域内和整张图片其他邻域内是否有相似的块
int k0 = min(x, bs), k1 = min(N - 1 - x, bs);
int l0 = min(y, bs), l1 = min(M - 1 - y, bs);
//这里使用p0q0使得本身就在对应点的邻域寻找
int p0 = max(x - step, bs), p1 = max(N - 1 - x - step, bs);
int q0 = max(y - step, bs), q1 = max(M - 1 - y - step, bs);
int p2 = min(x + step, N);
int q2 = min(y + step, M);
int sx = 1000000;
int sy = 1000000;
int min_diff = 1000000; //最大的int值
for (int j = q0; j + bs < M-q1; j += bs)
for (int i = p0; i + bs < N-p1; i += bs)
{
//printf("%d\n", tmp);
//通过usable找到最近的不需要填充的像素点
//如果==2说明这里没有纹理
//match.at<Vec3b>(i, j) = Vec3b(255, 0, 0);
if (my_mask[i][j] == 2) {
break;
}
if (usable[i][j] == 2) continue;
int tmp_diff = 0;
//取到xy和ij周围step的矩形邻域
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
{
//printf("%d %d %d %d %d %d\n", i + k, j + l, x + k, y + l, N, M);
//ij表示可以用来比较的不需要填充纹理的坐标点
//xy表示当前需要被填充的点,由之前的for循环生成
//[x + k][y + l]表示xy的step邻域内的某点
//[i + k][j + l]表示ij的step邻域内的某点
if (my_mask[x + k][y + l] != 0)
tmp_diff += dist(result.at<Vec3b>(i + k, j + l), result.at<Vec3b>(x + k, y + l));
//tmp_diff计算了这两个对应点之间,RGB值的差异;显然需要全图搜索找到一个最小的tmpdiff,这说明这两块邻域最像
}
//printf("tmp_diff = %d", tmp_diff);
sum_diff[i][j] = tmp_diff;
if (min_diff > tmp_diff)
{
sx = i;
sy = j;
min_diff = tmp_diff;
}
sum_diff[i][j] = tmp_diff;
//结束循环的时候,得到的是对比xy有最小tmpdiff的点的坐标sx,sy
}
imshow("iii", match);
waitKey(10);
cout << "当前的点是xy:" << x << y << endl;
if (sx == 1000000 && sy == 1000000) {
//这种点实际上特别多!!!要保证可以获取到能用的texture!!
//这里已经是触发异常的点,进行全局搜索,
cout << "处罚异常xy" << endl;
match.at<Vec3b>(x, y) = Vec3b(0, 0, 255);
for (int j = M - step; j - bs > step; j -= bs)
for (int i = N - step; i - bs > step; i -= bs)
{
int tmp_diff = 0;
/*if (my_mask[i][j] == 2) {
cout << i << " , " << j << endl;
break;
}*/
if (usable[i][j] == 2) continue;
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
if (my_mask[x + k][y + l] != 0)
tmp_diff += dist(result.at<Vec3b>(i + k, j + l), result.at<Vec3b>(x + k, y + l));
sum_diff[i][j] = tmp_diff;
if (min_diff > tmp_diff)
{
sx = i;
sy = j;
min_diff = tmp_diff;
}
sum_diff[i][j] = tmp_diff;
}
if (usable[sx][sy] == -1) {
printf("------当前对应点是一个曾经被填充过的点-----");
}
}
if (sx == 1000000 && sy == 1000000) {
sx = x;
sy = y;
printf("【仍旧找不到】");
}
cout << "对应的点是xy:" << sx << sy << endl;
usable[x][y] = -1;
//用(sx,sy)周围的点的RGB值填充xy周围需要被填充的点
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
if (my_mask[x + k][y + l] == 0)
{
result.at<Vec3b>(x + k, y + l) = result.at<Vec3b>(sx + k, sy + l);
my_mask[x + k][y + l] = 1;
//usable[x + k][y + l] = 1;
filled++;
if (debug)
{
result.at<Vec3b>(x + k, y + l) = Vec3b(255, 0, 0);
result.at<Vec3b>(sx + k, sy + l) = Vec3b(0, 255, 0);
}
if (debug2)
{
match.at<Vec3b>(x + k, y + l) = Vec3b(255, 0, 0);
match.at<Vec3b>(sx + k, sy + l) = Vec3b(0, 255, 0);
}
}
else
{
if (debug)
{
printf("(%d,%d,%d) matches (%d,%d,%d)\n", result.at<Vec3b>(x + k, y + l)[0], result.at<Vec3b>(x + k, y + l)[1], result.at<Vec3b>(x + k, y + l)[2], result.at<Vec3b>(sx + k, sy + l)[0], result.at<Vec3b>(sx + k, sy + l)[1], result.at<Vec3b>(sx + k, sy + l)[2]);
}
}
if (debug2)
{
imshow("match", match);
}
if (debug) return;
printf("done :%.2lf%%\n", 100.0 * filled / to_fill);
imwrite("final.png", result);
imshow("final", result);
waitKey(0);
}
}
Mat1b getContous(string a, Mat1b linemask) {
//M是高度
//N是长度
int M, N;
int safe_distence = 18; //距离结构线的安全距离
M = linemask.rows;
N = linemask.cols;
ifstream infile;
Mat1b myMap = Mat::zeros(cv::Size(N, M), CV_8UC1);
Mat1b contousMap = Mat::zeros(cv::Size(N, M), CV_8UC1);
infile.open(a.data()); //将文件流对象与文件连接起来
assert(infile.is_open()); //若失败,则输出错误消息,并终止程序运行
string s;
while (getline(infile, s))
{
//cout << s << endl;
std::string::size_type pos = s.find(" ");
std::string firstStr = s.substr(0, pos);
std::string laterStr = s.substr(pos + strlen(" "));
/*cout << firstStr << endl;
cout << laterStr << endl;*/
int p = atoi(firstStr.c_str());
int q = atoi(laterStr.c_str());
myMap[q][p] = 255; //左上角是0,0;;前者是纵坐标,后者是横坐标
}
/*cout << M << " " << N << endl;
cout << myMap[M][N] << endl;*/
int areaIndex = 1;
int flag = 0; //判读是否是line
int threshold = 0;
for (int i = 15; i < N - 15; i++) {
for (int j = 15; j < M - 15; j++) {
if (myMap[j][i] == 0) {
threshold++;
if (threshold < safe_distence) {
continue;
}
if (flag == 1) {
areaIndex++;
}
flag = 0;
contousMap[j][i] = areaIndex;
//cout << contousMap[j][i]
/*myMap[j][i] = 255;
cout << areaIndex << endl;
imshow("window", myMap);
waitKey(10);*/
}
else {
for (int back = 1; back < safe_distence; back++) {
//myMap[j-back][i] = 0;
if (j-back > 0)
contousMap[j-back][i] = 0;
}
flag = 1;
threshold = 0;
}
}
areaIndex = 1;
}
//cout << contousMap << endl;
infile.close();
return contousMap;
}
//全黑色是0,全白色是255
// mask: 二值化的mask图像
// Linemask:暂时理解为结构线
// mat:是之前带有mask的没有进行纹理补全的结果
// result:最后输出的结果
void TextureCompletion3(Mat3b img, Mat1b map, Mat1b _mask, Mat1b LineMask, const Mat3b &mat, Mat &result)
{
int N = _mask.rows;
int M = _mask.cols;
int* test_mask;
int knowncount = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
knowncount += (_mask.at<uchar>(i, j) == 255);
//统计输入mask中纯白色像素点的个数
}
//做了一种优化处理,判断是黑色点多还是白色点多,从而进行后面的操作
// mask部分是0白色??
if (knowncount * 2< N * M)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
_mask.at<uchar>(i, j) = 255 - _mask.at<uchar>(i, j);
}
//新建一个my_mask和sum_diff
vector<vector<int> >my_mask(N, vector<int>(M, 0)), sum_diff(N, vector<int>(M, 0));
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//mymask对应于mask(mask中的黑色遮挡部分mymask为0,mask白色部分mymask为1)
my_mask[i][j] = (_mask.at<uchar>(i, j) == 255);
//如果mymask中的一个位置坐标既是遮挡,又是LineMask中的灰色部分,则标注为2
if (my_mask[i][j] == 0 && LineMask.at<uchar>(i, j) > 0)
{
my_mask[i][j] = 2;
}
}
/*
my_mask的结构
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 2 0 1 ---结构线
1 0 2 2 2 0 1 ---结构线
1 0 0 0 0 0 1
1 1 1 1 1 1 1
*/
int bs = 5;
int step = 6;
auto usable(my_mask); //自动生成了一个和mymask相同类型的变量
int to_fill = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
int filled = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
to_fill += (my_mask[i][j] == 0);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//遍历全图,如果my_mask[i][j] == 1说明不需要填充则继续
if (my_mask[i][j] == 1)
continue;
//对于mymask中需要被填充的地方
//在一个step的矩形邻域内,需要把usable标记为2
//usable[k][l] == 2说明需要被填充
//(我的理解是在原来的mask周围扩大了需要补全纹理的范围,缩小了可用的纹理的范围)
int k0 = max(0, i - bs), k1 = min(N - 1, i + bs);
int l0 = max(0, j - bs), l1 = min(M - 1, j + bs);
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
usable[k][l] = 2;
}
//按照usable中2的地方生成一个黑白图,其中白色是需要填充的地方值为2
//也就是说实际要填充的部分比正常的黑白图要大
Mat use = _mask.clone();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (usable[i][j] == 2)
use.at<uchar>(i, j) = 255;
else use.at<uchar>(i, j) = 0;
int itertime = 0;
Mat match;
Mat output;
match = result.clone();
while (true)
{
itertime++;
int x, y, cnt = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//略过不需要填充的地方以及轮廓线部分
if (my_mask[i][j] != 0) continue;
//此时my_mask[i][j]==0
//首先要找到需要填充的区域的边界点
//edge用于判断这个点是不是边界
bool edge = false;
int k0 = max(0, i - 1), k1 = min(N - 1, i + 1);
int l0 = max(0, j - 1), l1 = min(M - 1, j + 1);
//取到像素点的一个小邻域8个像素点,如果这个邻域内的点有一个是1则最后edge==true
/*
1 1 1
1 0 1
1 1 1
*/
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
edge |= (my_mask[k][l] == 1); //或等于 edge = edge | (my_mask==1);
if (!edge) continue;
//如果edge==true说明当前像素点是边界点
//------猜测后面需要对这个像素点进行融合运算!-------
k0 = max(0, i - bs), k1 = min(N - 1, i + bs);
l0 = max(0, j - bs), l1 = min(M - 1, j + bs);
int tmpcnt = 0;
//此时取到当前像素点周围的一个step大小的矩形邻域
//tmpcnt计算了这个矩形邻域内不需要填充的像素点的个数
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
tmpcnt += (my_mask[k][l] == 1);
if (tmpcnt > cnt)
{
cnt = tmpcnt;
x = i;
y = j;
}
//结束for循环的时候xy记录了边界点
}
//如果cnt==-1说明所有edge都是false,也就是说所有mymask[i,j]都是1都是不需要填充,跳出while
if (cnt == -1) break;
bool debug = false;
bool debug2 = false;
//这部分再次遍历全图;比较一个邻域内和整张图片其他邻域内是否有相似的块
int k0 = min(x, bs), k1 = min(N - 1 - x, bs);
int l0 = min(y, bs), l1 = min(M - 1 - y, bs);
//这里使用p0q0使得本身就在对应点的邻域寻找
int p0 = max(x - step, bs), p1 = max(N - 1 - x - step, bs);
int q0 = max(y - step, bs), q1 = max(M - 1 - y - step, bs);
int p2 = min(x + step, N);
int q2 = min(y + step, M);
int sx = 1000000;
int sy = 1000000;
int min_diff = 1000000; //最大的int值
for (int i = 50; i + 50 < N; i += step)
for (int j = 50; j + 50 < M; j += step)
{
//通过usable找到最近的不需要填充的像素点
//如果==2说明这里的纹理不可用
if (usable[i][j] == 2)continue;
//判断两者是否实在同一个area
//cout << "-属于的区域是: " << map[i][j] << endl;
if (map[i][j] != map[x][y]) continue;
int tmp_diff = 0;
//取到xy周围step的矩形邻域
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
{
if (my_mask[x + k][y + l] != 0)
tmp_diff += dist(result.at<Vec3b>(i + k, j + l), result.at<Vec3b>(x + k, y + l));
}
sum_diff[i][j] = tmp_diff;
if (min_diff > tmp_diff)
{
sx = i;
sy = j;
min_diff = tmp_diff;
}
//结束循环的时候,得到的是对比xy有最小tmpdiff的点的坐标sx,sy
}
// cout << "对应的点是xy:" << sx << sy << endl;
if (sx == 1000000 && sy == 1000000) {
for (int i = step; i + step < N; i += step)
for (int j = step; j + step < M; j += step)
{
//通过usable找到最近的不需要填充的像素点
//如果==2说明这里的纹理不可用
//if (usable[i][j] == 2)continue;
if (map[i][j] != map[x][y]) continue;
int tmp_diff = 0;
//取到xy周围step的矩形邻域
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
{
if (my_mask[x + k][y + l] != 0)
tmp_diff += dist(result.at<Vec3b>(i + k, j + l), result.at<Vec3b>(x + k, y + l));
}
sum_diff[i][j] = tmp_diff;
if (min_diff > tmp_diff)
{
sx = i;
sy = j;
min_diff = tmp_diff;
}
}
}
//usable[x][y] = -1;
//用(sx,sy)周围的点的RGB值填充xy周围需要被填充的点
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
if (my_mask[x + k][y + l] == 0)
{
result.at<Vec3b>(x + k, y + l) = result.at<Vec3b>(sx + k, sy + l);
my_mask[x + k][y + l] = 1;
//usable[x + k][y + l] = 1;
filled++;
img.at<Vec3b>(x, y) = Vec3b(0, 0, 255);
}
// mergeImg(output, img, result);
// imshow("Output", output);
// waitKey(10);
printf("done :%.2lf%%\n", 100.0 * filled / to_fill);
//imwrite("final.png", result);
imshow("run", result);
waitKey(10);
}
mergeImg(output, img, result);
// imwrite("final.png", result);
// imwrite("Output.png", output);
// imshow("Output", output);
// waitKey(0);
}
//全黑色是0,全白色是255
// mask: 二值化的mask图像
// Linemask:暂时理解为结构线
// mat:是之前带有mask的没有进行纹理补全的结果
// result:最后输出的结果
void TextureCompletion1(Mat1b _mask, Mat1b LineMask, const Mat &mat, Mat &result)
{
int N = _mask.rows;
int M = _mask.cols;
int knowncount = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
knowncount += (_mask.at<uchar>(i, j) == 255);
//统计输入mask中纯白色像素点的个数
}
//做了一种优化处理,判断是黑色点多还是白色点多,从而进行后面的操作
// mask部分是0白色??
if (knowncount * 2< N * M)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
_mask.at<uchar>(i, j) = 255 - _mask.at<uchar>(i, j);
}
//新建一个my_mask和sum_diff
vector<vector<int> >my_mask(N, vector<int>(M, 0)), sum_diff(N, vector<int>(M, 0));
//Linemask扩大这后面白色的255*100变成灰色,黑色依旧是0
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
LineMask.at<uchar>(i, j) = LineMask.at<uchar>(i, j) * 100;
result = mat.clone();
/*imshow("mask", _mask);
imshow("linemask", LineMask);*/
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//mymask对应于mask(mask中的黑色遮挡部分mymask为0,mask白色部分mymask为1)
my_mask[i][j] = (_mask.at<uchar>(i, j) == 255);
//如果mymask中的一个位置坐标既是遮挡,又是LineMask中的灰色部分,则标注为2
if (my_mask[i][j] == 0 && LineMask.at<uchar>(i, j) > 0)
{
my_mask[i][j] = 2;
}
}
/*
my_mask的结构
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 0 0 2 0 1 ---结构线
1 0 2 2 2 0 1 ---结构线
1 0 0 0 0 0 1
1 1 1 1 1 1 1
*/
int bs = 5;
int step = 1 * bs;
auto usable(my_mask); //自动生成了一个和mymask相同类型的变量
int to_fill = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
int filled = 0; //mymask中未被填充的阴影遮挡的部分(非结构线)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
to_fill += (my_mask[i][j] == 0);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//如果my_mask[i][j] == 1说明不需要填充则继续
if (my_mask[i][j] == 1)
continue;
//对于mymask中需要被填充的地方
//在一个step的矩形邻域内,需要把usable标记为2
//usable[k][l] == 2说明需要被填充
//(我的理解是在原来的mask周围扩大了需要补全纹理的范围)
int k0 = max(0, i - step), k1 = min(N - 1, i + step);
int l0 = max(0, j - step), l1 = min(M - 1, j + step);
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
usable[k][l] = 2;
}
//按照usable中2的地方生成一个黑白图,其中白色是需要填充的地方值为2
Mat use = _mask.clone();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (usable[i][j] == 2)
use.at<uchar>(i, j) = 255;
else use.at<uchar>(i, j) = 0;
//imshow("usable", use);
int itertime = 0;
Mat match;
while (true)
{
itertime++;
int x, y, cnt = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
//略过不需要填充的地方以及轮廓线部分
if (my_mask[i][j] != 0) continue;
//此时my_mask[i][j]==0
bool edge = false;
int k0 = max(0, i - 1), k1 = min(N - 1, i + 1);
int l0 = max(0, j - 1), l1 = min(M - 1, j + 1);
//取到像素点的一个小邻域8个像素点,如果这个邻域内的点有一个是1则最后edge==true
/*
1 1 1
1 0 1
1 1 1
*/
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
edge |= (my_mask[k][l] == 1); //或等于 edge = edge | (my_mask==1);
if (!edge) continue;
//如果edge==true说明当前像素点是边界点
//------猜测后面需要对这个像素点进行融合运算!-------
k0 = max(0, i - bs), k1 = min(N - 1, i + bs);
l0 = max(0, j - bs), l1 = min(M - 1, j + bs);
int tmpcnt = 0;
//此时取到当前像素点周围的一个step大小的矩形邻域
//tmpcnt计算了这个矩形邻域内不需要填充的像素点的个数
for (int k = k0; k <= k1; k++)
for (int l = l0; l <= l1; l++)
tmpcnt += (my_mask[k][l] == 1);
if (tmpcnt > cnt)
{
cnt = tmpcnt;
x = i;
y = j;
}
//结束for循环的时候xy记录了周围不需要填充像素最多的那个像素点的坐标
}
//如果cnt==-1说明所有edge都是false,也就是说所有mymask[i,j]都是1都是不需要填充,跳出while
if (cnt == -1) break;
bool debug = false;
bool debug2 = false;
//这部分再次遍历全图;比较一个邻域内和整张图片其他邻域内是否有相似的块
int k0 = min(x, bs), k1 = min(N - 1 - x, bs);
int l0 = min(y, bs), l1 = min(M - 1 - y, bs);
int sx, sy, min_diff = INT_MAX; //最大的int值
for (int i = step; i + step < N; i += step)
for (int j = step; j + step < M; j += step)
{
//通过usable找到最近的不需要填充的像素点
//如果==2说明这里的纹理不可用
if (usable[i][j] == 2)continue;
int tmp_diff = 0;
//取到xy周围step的矩形邻域
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
{
//printf("%d %d %d %d %d %d\n", i + k, j + l, x + k, y + l, N, M);
//ij表示可以用来比较的不需要填充纹理的坐标点
//xy表示当前需要被填充的点,由之前的for循环生成
//[x + k][y + l]表示xy的step邻域内的某点
//[i + k][j + l]表示ij的step邻域内的某点
if (my_mask[x + k][y + l] != 0)
tmp_diff += dist(result.at<Vec3b>(i + k, j + l), result.at<Vec3b>(x + k, y + l));
//tmp_diff计算了这两个对应点之间,RGB值的差异;显然需要全图搜索找到一个最小的tmpdiff,这说明这两块邻域最像
//--------------------------这里似乎有文章???没有规定这个对应点的范围,没有考虑轮廓线--------------------//
}
sum_diff[i][j] = tmp_diff;
if (min_diff > tmp_diff)
{
sx = i;
sy = j;
min_diff = tmp_diff;
}
//结束循环的时候,得到的是对比xy有最小tmpdiff的点的坐标sx,sy
}
if (debug)
{
printf("x = %d y = %d\n", x, y);
printf("sx = %d sy = %d\n", sx, sy);
printf("mindiff = %d\n", min_diff);
}
if (debug2)
{
match = result.clone();
}
//用(sx,sy)周围的点的RGB值填充xy周围需要被填充的点
for (int k = -k0; k <= k1; k++)
for (int l = -l0; l <= l1; l++)
if (my_mask[x + k][y + l] == 0)
{
result.at<Vec3b>(x + k, y + l) = result.at<Vec3b>(sx + k, sy + l);
my_mask[x + k][y + l] = 1;
filled++;
if (debug)
{
result.at<Vec3b>(x + k, y + l) = Vec3b(255, 0, 0);
result.at<Vec3b>(sx + k, sy + l) = Vec3b(0, 255, 0);
}
if (debug2)
{
match.at<Vec3b>(x + k, y + l) = Vec3b(255, 0, 0);
match.at<Vec3b>(sx + k, sy + l) = Vec3b(0, 255, 0);
}
}
else
{
if (debug)
{
printf("(%d,%d,%d) matches (%d,%d,%d)\n", result.at<Vec3b>(x + k, y + l)[0], result.at<Vec3b>(x + k, y + l)[1], result.at<Vec3b>(x + k, y + l)[2], result.at<Vec3b>(sx + k, sy + l)[0], result.at<Vec3b>(sx + k, sy + l)[1], result.at<Vec3b>(sx + k, sy + l)[2]);
}
}
if (debug2)
{
imshow("match", match);
}
if (debug) return;
printf("done :%.2lf%%\n", 100.0 * filled / to_fill);
imwrite("final1.png", result);
imshow("final1", result);
waitKey(0);
}
}
void texture(Mat origin, Mat img, Mat mask, Mat &finalResult2, Mat Linemask, string listpath)
{
//四个输入:mask,line,
int m, n;
//读入原图
// Mat3b origin = imread("../Texture/origin/img4.png");
// Mat3b img = imread("../Texture/sp_result/sp4.png");//5,1
//读入二值化的mask图像
// Mat1b mask = Mat::zeros(img.rows, img.cols, CV_8UC1);
// mask = imread("../Texture/mask/mask4.bmp", 0);
threshold(mask, mask, 125, 255, CV_THRESH_BINARY_INV);
/*imshow("img", img);
waitKey(10);
imshow("mask", mask);
waitKey(10);*/
//生成带有mask但是没有进行补全的图
Mat3b result;
result.zeros(img.size());
img.copyTo(result, mask);
/*imshow("result", result);
waitKey(10);*/
//读入linemask
// Mat1b Linemask = Mat::zeros(img.rows, img.cols, CV_8UC1);
// Linemask = imread("../Texture/line/mask_s4.bmp", 0);
/*imshow("line", Linemask);
waitKey(10);*/
//最终结果变量
// Mat3b finalResult2(img.size());
img.copyTo(finalResult2);
// Mat3b finalResult1(img.size());
// img.copyTo(finalResult1);
/*imshow("final", finalResult1);]
waitKey(10);*/
Mat1b map = getContous(listpath, Linemask);
//TextureCompletion1(mask, Linemask, result, finalResult1);
//TextureCompletion2(mask, Linemask, result, finalResult2);
TextureCompletion3(origin, map, mask, Linemask, result, finalResult2);
// imshow("final", finalResult2);
// waitKey(0);
}