-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_synthesis.m
583 lines (398 loc) · 20.5 KB
/
image_synthesis.m
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
function [indicative,label_mat,opimage] = image_synthesis(img1,img2)
%%%%%
%%%minesh
%%%%
%%%%
%%%%
rws=size(img2,1);%no of rows in the 2nd image
cols=size(img2,2);%no of columns in the 2nd image
%where to keep image 1 in image2; the input is the point where the top left
%crner of img1 should be placed on image2
imagesc(img2);
axis image;
indicative=zeros(size(img2,1),size(img2,2));
%msgbox(['Click on the image where you want to place the first images top left corner and hit enter' ]);
point=ginput();
disp(point);
%getting exact pixel values
posx=round(point(1)); %postion along the x axis or how many columns from the origin
posy=round(point(2));%how many rows from the origin
disp(posx);
disp(posy);
if((posx+size(img1,2) > cols) || (posy+size(img1,1)> rws))
disp('image 1 wont fit on image 2 if it is placed at the given position');
return
end
%in the first iamge select aregion whcih you insist to come from that
%image only
%in the rectangular matrix the area covered by img1 over img2 is marked by
%a new indcaiteve value=255 , white color in the indicative image
for i=posy:posy+size(img1,1)-1 %row by row
for j=posx:posx+size(img1,2)-1 %column by column
indicative(i,j)=255;
end
end
imagesc(img1);
axis image;
%msgbox(['draw a recatnglualr region that must appear in the final output' ]);
rect=getrect();
%close(h2);
disp(rect);
%gettig exact pixel values
xmin=round(rect(1));
ymin=round(rect(2));
width=round(rect(3));
height=round(rect(4));
%in the iundicative matrix the rectangular portion is identified by a new
%indicative value =128 that is grey color ; so pixels comulsairly from
%image1 will be idetified by a value=128; view the indicativee matrix as an image for
%bteer understanding
for i=(posy+ymin):(posy+ymin+height-1) %row by row
for j=(posx+xmin):(posx+xmin+width-1) %column by column
%disp('eneterd');
indicative(i,j)=128;
end
end
%imshow(indicative);
%our graph will have number of vertices = no of pixels
%=size(img2,1)*size(img2,2) ie no fo pixels in the img2 ie the image on
%which the other image is aligned
%so the adjacency matrix will be a square matrix with dimension as
%nofpixels*nof pixels
%to store pixels as node, pixels are taken in column major oreder
A=sparse(rws*cols,rws*cols);
T=sparse(rws*cols,2);
%Now we need a matrix to save weights to the terminal nodes S and T
% so for that we need a matrix of dimension no.of pixels *2
%because we have only two labels S or T
border_img2=0;
border_img1=0;
img1_comp=0;
img2_comp=0;
img1_new=img1;%making copies of the two images
img2_new=img2;
% img1=rgb2gray(img1);
% img2=rgb2gray(img2);
img1=double(img1);%for calcuations double values are required
img2=double(img2);
for i=1:cols %traverse column by column
for j=1:rws %in a column traverse row by row
%%%%%FILLING THE T Matrix
if(indicative(j,i)==0) %if the pixel is compulsairly from img2
img2_comp=img2_comp+1;
%disp('compulsary frm img2');
T((i-1)*rws+j,1)=100000; %weight set for connection to label1
end
if(indicative(j,i)==128) %if pixel is compulsairly from img1
img1_comp=img1_comp+1;
T((i-1)*rws+j,2)=100000;%weight set for conncetion to label 2
%disp('compulsary frm img1');
end
if(indicative(j,i)==255)%if its a pixel in the overlap region
Asr=img2(j,i,1);%red value of node in focus if color is from img2
Asg=img2(j,i,2);
Asb=img2(j,i,3);
Bsr=img1(((j-posy)+1),((i-posx)+1),1); %red value of node in focus if color is frm img1
Bsg=img1(((j-posy)+1),((i-posx)+1),2);
Bsb=img1(((j-posy)+1),((i-posx)+1),3);
% disp('colors are');
% disp(Asr);
% disp(Asg);
% disp(Asb);
% disp(Bsr);
% disp(Bsg);
% disp(Bsb);
%
%
%
% disp('diff are');
%
%
%
%
% disp(Asr-Bsr);
% disp(Asg-Bsg);
% disp(Asb-Bsb);
%
% disp('powers are');
%
%
%
%
% disp((Asr-Bsr)^2);
% disp((Asg-Bsg)^2);
% disp((Asb-Bsb)^2);
% As_grey=(Asr+Asg+Asb)/3;
% Bs_grey=(Bsr+Bsg+Bsb)/3;
Snorm= ((Asr-Bsr)^2+(Asg-Bsg)^2+(Asb-Bsb)^2)^(1/2);
%Snorm= abs (img2(j,i) - img1(((j-posy)+1),((i-posx)+1)));
if (( (j+1) > 0) && ((j+1) <= rws))%pixel below %( indicative(j+1,i)==255)
%above if checks if there is a pixel below the current pixel
if ( indicative(j+1,i)==0)%if pixel below is compulsairly from img2,then it is a border pixel in the overlap region
%and so it is connected to img2 node with infinte
%weight
%disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;%for counting the nof pixles bordering with img2
end
if ( indicative(j+1,i)==128)
%disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if( (indicative(j+1,i)==255))% if the pixel below is also in the overlaping region
% (1,2) and 2,1 cas ein adj matrix
% A((i-1)*rws+(j+1),(i-1)*rws+j) == 0 &&
%disp('below');
Atr=img2(j+1,i,1);
Atg=img2(j+1,i,2);
Atb=img2(j+1,i,3);
Btr=img1(((j+1)-posy)+1,(i-posx)+1,1);
Btg=img1(((j+1)-posy)+1,(i-posx)+1,2);
Btb=img1(((j+1)-posy)+1,(i-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
%Tnorm=abs(img2(j+1,i)-img1(((j+1)-posy)+1,(i-posx)+1));
A((i-1)*rws+j,(i-1)*rws+(j+1))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (j-1) > 0) && ((j-1) <= rws))%pixel above % &&( indicative(j-1,i)==255)
%if the above pixel is within limits of the image
if (indicative(j-1,i)==0)%if the pixel above is compulsairly frm image 2
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if (indicative(j-1,i)==128) %if compusairly frm img1
% disp('new');
T((i-1)*rws+j,2)=100000;
border_img1=border_img1+1;
end
if( ( indicative(j-1,i)==255))
%A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
%disp('above');
% A((i-1)*rws+j,(i-1)*rws+(j-1))= equation;
Atr=img2(j-1,i,1);
Atg=img2(j-1,i,2);
Atb=img2(j-1,i,3);
Btr=img1(((j-1)-posy)+1,(i-posx)+1,1);
Btg=img1(((j-1)-posy)+1,(i-posx)+1,2);
Btb=img1(((j-1)-posy)+1,(i-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
%Tnorm=abs( img2(j-1,i) - img1(((j-1)-posy)+1,(i-posx)+1) );
A((i-1)*rws+j,(i-1)*rws+(j-1))=Snorm+Tnorm;
%
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (i-1) > 0) && ((i-1) <= cols) )%pixel to the left %&&( indicative(j,i-1)==255)
if ( indicative(j,i-1)==0)
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j,i-1)==128)
% disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if( ( indicative(j,i-1)==255))
%A((i-1)*rws+(j+1),(i-1)*rws+j) ==0 &&
%disp('leftr');
% A((i-1)*rws+j,(i-2)*rws+j)= equation;
Atr=img2(j,i-1,1);
Atg=img2(j,i-1,2);
Atb=img2(j,i-1,3);
Btr=img1((j-posy)+1,((i-1)-posx)+1,1);
Btg=img1((j-posy)+1,((i-1)-posx)+1,2);
Btb=img1((j-posy)+1,((i-1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
%Tnorm=abs(img2(j,i-1) -img1((j-posy)+1,((i-1)-posx)+1));
A((i-1)*rws+j,(i-2)*rws+(j))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (i+1) > 0) && ((i+1) <=cols) )%pixel to the right %&&( indicative(j,i+1)==255)
if ( indicative(j,i+1)==0)
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j,i+1)==128)
% disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if(( indicative(j,i+1)==255))
% A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
%disp('right');
% A((i-1)*rws+j,(i*rws)+j)= equation;
Atr=img2(j,i+1,1);
Atg=img2(j,i+1,2);
Atb=img2(j,i+1,3);
Btr=img1((j-posy)+1,((i+1)-posx)+1,1);
Btg=img1((j-posy)+1,((i+1)-posx)+1,2);
Btb=img1((j-posy)+1,((i+1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
% Tnorm=abs(img2(j,i+1) - img1((j-posy)+1,((i+1)-posx)+1));
A((i-1)*rws+j,(i)*rws+(j))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
%
% =======
%======= diagonal componedtes
%=============
if (( (i-1) > 0) && ((i-1) <=cols) && (j-1) >0 && (j-1) <=rws)%pixel to the left top %&&( indicative(j,i+1)==255)
if ( indicative(j-1,i-1)==0)
%disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j-1,i-1)==128)
%disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if(( indicative(j-1,i-1)==255))
% A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
% disp('right');
% A((i-1)*rws+j,(i*rws)+j)= equation;
Atr=img2(j-1,i-1,1);
Atg=img2(j-1,i-1,2);
Atb=img2(j-1,i-1,3);
Btr=img1(((j-1)-posy)+1,((i-1)-posx)+1,1);
Btg=img1(((j-1)-posy)+1,((i-1)-posx)+1,2);
Btb=img1(((j-1)-posy)+1,((i-1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
%Tnorm=abs(img2(j-1,i-1) - img1(((j-1)-posy)+1,((i-1)-posx)+1));
A((i-1)*rws+j,(i-2)*rws+(j-1))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (i-1) > 0) && ((i-1) <=cols) && (j+1) >0 && (j+1) <=rws)%pixel to the left below position %&&( indicative(j,i+1)==255)
if ( indicative(j+1,i-1)==0)
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j+1,i-1)==128)
%disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if(( indicative(j+1,i-1)==255))
% A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
% disp('right');
% A((i-1)*rws+j,(i*rws)+j)= equation;
Atr=img2(j+1,i-1,1);
Atg=img2(j+1,i-1,2);
Atb=img2(j+1,i-1,3);
Btr=img1(((j+1)-posy)+1,((i-1)-posx)+1,1);
Btg=img1(((j+1)-posy)+1,((i-1)-posx)+1,2);
Btb=img1(((j+1)-posy)+1,((i-1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
%Tnorm=abs(img2(j+1,i-1) - img1(((j+1)-posy)+1,((i-1)-posx)+1));
A((i-1)*rws+j,(i-2)*rws+(j+1))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (i+1) > 0) && ((i+1) <=cols) && (j-1) >0 && (j-1) <=rws)%pixel to the right top position %&&( indicative(j,i+1)==255)
if ( indicative(j-1,i+1)==0)
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j-1,i+1)==128)
% disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if(( indicative(j-1,i+1)==255))
% A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
% disp('right');
% A((i-1)*rws+j,(i*rws)+j)= equation;
Atr=img2(j-1,i+1,1);
Atg=img2(j-1,i+1,2);
Atb=img2(j-1,i+1,3);
Btr=img1(((j-1)-posy)+1,((i+1)-posx)+1,1);
Btg=img1(((j-1)-posy)+1,((i+1)-posx)+1,2);
Btb=img1(((j-1)-posy)+1,((i+1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
% Tnorm=abs(img2(j-1,i+1) - img1(((j-1)-posy)+1,((i+1)-posx)+1));
% A((i-1)*rws+j,(i)*rws+(j-1))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
if (( (i+1) > 0) && ((i+1) <=cols) && (j+1) >0 && (j+1) <=rws)%pixel to the right below position %&&( indicative(j,i+1)==255)
if ( indicative(j+1,i+1)==0)
% disp('new');
T((i-1)*rws+j,1)=100000;
border_img2=border_img2+1;
end
if ( indicative(j+1,i+1)==128)
% disp('new');
border_img1=border_img1+1;
T((i-1)*rws+j,2)=100000;
end
if(( indicative(j+1,i+1)==255))
% A((i-1)*rws+(j+1),(i-1)*rws+j)==0 &&
% disp('right');
% A((i-1)*rws+j,(i*rws)+j)= equation;
Atr=img2(j+1,i+1,1);
Atg=img2(j+1,i+1,2);
Atb=img2(j+1,i+1,3);
Btr=img1(((j+1)-posy)+1,((i+1)-posx)+1,1);
Btg=img1(((j+1)-posy)+1,((i+1)-posx)+1,2);
Btb=img1(((j+1)-posy)+1,((i+1)-posx)+1,3);
Tnorm= ((Atr-Btr)^2+(Atg-Btg)^2+(Atb-Btb)^2)^(1/2);
% Tnorm=abs(img2(j+1,i+1) - img1(((j+1)-posy)+1,((i+1)-posx)+1));
A((i-1)*rws+j,(i)*rws+(j+1))=Snorm+Tnorm;
% At_grey=(Atr+Atg+Atb)/3;
% Bt_grey=(Btr+Btg+Btb)/3;
% A((i-1)*rws+j,(i-1)*rws+(j+1))=(As_grey-Bs_grey)+(At_grey-Bt_grey);
end
end
end
end
end
[flow,labels] = maxflow(A,T);
disp(rect);
disp('no of border pixels with image1 is');
disp(border_img1);
disp('no ofpixels for which infinte weigt added to img 1');
disp(img1_comp);
disp('no ofpixels for which infinte weigt added to img 2');
disp(img2_comp);
label_mat=reshape(labels,rws,cols);
opimage=zeros(rws,cols,3);
opimage=uint8(opimage);
for i=1:rws
for j=1:cols
if(label_mat(i,j)==0)
opimage(i,j,1)=img2_new(i,j,1);
opimage(i,j,2)=img2_new(i,j,2);
opimage(i,j,3)=img2_new(i,j,3);
else
opimage(i,j,1)=img1_new((i-posy)+1,(j-posx)+1,1);
opimage(i,j,2)=img1_new((i-posy)+1,(j-posx)+1,2);
opimage(i,j,3)=img1_new((i-posy)+1,(j-posx)+1,3);
end
end
end
end