-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap1_Bai3_circle_patch.m
70 lines (53 loc) · 1.29 KB
/
Chap1_Bai3_circle_patch.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
%[Bai 3]: them tinh nang lay patch duong tron
function Bai3_circle_patch
I = imread('hui.jpg');
I = im2double(I);
[M,N,~] = size(I);
r = 100; %Radius
k = zeros(M+2*r,N+2*r,3); %Edge filler
k (r+1:r+M, r+1:r+N, :) = I; % Add Origin Image to Filler Edges
J = k; % Duplicate of Origin Image with Filler Edges
[m,n,~] = size(k); % Take stats
% Patch P
i = floor(random('unif',1+r,M+1+r));
j = floor(random('unif',1+r,N+1+r));
% Patch Q
p = k(i-r:i+r, j-r:j+r,:);
q = k(m-i-r:m-i+r,n-j-r:n-j+r,:);
P=p;
p = patchCircular(p,q,r);
q = patchCircular(q,P,r);
k(i-r:i+r, j-r:j+r,:) = q;
k(m-i-r:m-i+r,n-j-r:n-j+r,:) = p;
figure
subplot(221);
imshow(p);
title('patch p')
set(gca,'Visible','on');
subplot(222);
imshow(q);
title('patch q')
set(gca,'Visible','on');
subplot(223);
imshow(J);
title('anh goc')
set(gca,'Visible','on');
subplot(224);
imshow(k);
title('anh hoan vi')
set(gca,'Visible','on');
end
function [i] = patchCircular(a,b,r)
i = zeros(size(a));
[m,n,~] = size(a);
[xx,yy] = ndgrid((1:m)-m/2, (1:n)-n/2);
mask = (xx.^2 + yy.^2)< r^2;
i(:,:,1) = a(:,:,1).*mask;
i(:,:,2) = a(:,:,2).*mask;
i(:,:,3) = a(:,:,3).*mask;
%fix black outside patchCircular
mask = (xx.^2 + yy.^2)>= r^2;
i(:,:,1) = i(:,:,1) + b(:,:,1).*mask;
i(:,:,2) = i(:,:,2) + b(:,:,2).*mask;
i(:,:,3) = i(:,:,3) + b(:,:,3).*mask;
end