forked from ahof1704/VocalMat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorphimage.m
73 lines (65 loc) · 2.35 KB
/
morphimage.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
%morphimage morphs an image (grayscale) using a morphing matrix.
% MorphedImage = morphimage(Image, morphfield) morphs Image.
%
% MorphedImage --> Result of the morphing process
%
% Image --> Original grayscale image
% morphfield --> morphing matrix of size m,n,2 where
% :,:,1 --> morphing matrix for x-coordiantes
% :,:,2 --> morphing matrix for y-coordiantes
%
%example for a grayscale image:
%Read a grayscale image
% image = imread('example.pgm');
% imagesize = size(image);
%Creating a random morphield
% morphfield = 8*imresize(rand(5,5,2)-0.5,imagesize,'bilinear');
%Morph the original image
% MorphedImage = morphimage(image,morphfield);
%Show original image
% figure(1); imagesc(image,[0 255]); colormap gray;
%Show morphed image
% figure(2); imagesc(MorphedImage,[0 255]); colormap gray;
%Show morphing matrix for x-coordinates
% figure(3); imagesc(morphfield(:,:,1)); colorbar;
%Show morphing matrix for y-coordinates
% figure(4); imagesc(morphfield(:,:,2)); colorbar;
% Dima Pröfrock (2024). morphimage (https://www.mathworks.com/matlabcentral/fileexchange/11975-morphimage), MATLAB Central File Exchange.
function res = morphimage(orig,morphfield)
%get size of the image
origsize = size(orig);
%create an empty image
res = zeros(origsize);
%compute the new grayvalues for every pixel
for cnt1 = 1:1:origsize(1)
for cnt2 = 1:1:origsize(2)
%get the morphing-vector for the current pixel
xn = cnt2-morphfield(cnt1,cnt2,1);
yn = cnt1-morphfield(cnt1,cnt2,2);
if xn < 1
xn = 1;
end
if xn > origsize(2)
xn = origsize(2);
end
if yn < 1
yn = 1;
end
if yn > origsize(1)
yn = origsize(1);
end
%compute the pixel coordinates (x1,x2,y1,y2) and weighting
%(fx1,fx2,fy1,fy2) which are used to compute the new
%pixel grayvalue
x1 = floor(xn);
x2 = ceil(xn);
fx1 = x2-xn;
fx2 = 1-fx1;
y1 = floor(yn);
y2 = ceil(yn);
fy1 = y2-yn;
fy2 = 1-fy1;
%compute the new grayvalue of the current pixel
res(cnt1,cnt2) = fx1*fy1*orig(y1,x1)+fy1*fx2*orig(y1,x2)+fy2*fx1*orig(y2,x1)+fy2*fx2*orig(y2,x2);
end
end