Skip to content

Instantly share code, notes, and snippets.

@abcsds
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abcsds/0c52efb1c14a61b25685 to your computer and use it in GitHub Desktop.
Save abcsds/0c52efb1c14a61b25685 to your computer and use it in GitHub Desktop.
Matrix transformations for an image to be painted on a screen. Implemented on Octave.
GNU Octave, version 3.8.0
Copyright (C) 2013 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'.
Octave was configured for "x86_64-apple-darwin13.0.0".
Additional information about Octave is available at http://www.octave.org.
Please contribute if you find this software useful.
For more information, visit http://www.octave.org/get-involved.html
Read http://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.
Continue?
ans = [](0x0)
Transformation 1
===========================
Pixel:
1
1
Pixel falls in image point:
2.1433
1.7960
1.0000
Drawn!
Interpolated Value:
2.0000
continue?
ans = [](0x0)
===========================
Pixel:
1
2
Pixel falls in image point:
2.3169
3.7656
1.0000
Drawn!
Interpolated Value:
15
continue?
ans = [](0x0)
===========================
Pixel:
1
3
Pixel falls in image point:
2.4906
5.7352
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
1
4
Pixel falls in image point:
2.6642
7.7048
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
1
Pixel falls in image point:
4.1129
1.6223
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
2
Pixel falls in image point:
4.2865
3.5919
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
3
Pixel falls in image point:
4.4602
5.5616
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
4
Pixel falls in image point:
4.6338
7.5312
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
1
Pixel falls in image point:
6.0825
1.4487
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
2
Pixel falls in image point:
6.2561
3.4183
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
3
Pixel falls in image point:
6.4298
5.3879
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
4
Pixel falls in image point:
6.6034
7.3575
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
1
Pixel falls in image point:
8.0521
1.2750
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
2
Pixel falls in image point:
8.2258
3.2446
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
3
Pixel falls in image point:
8.3994
5.2143
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
4
Pixel falls in image point:
8.5731
7.1839
1.0000
Not Drawn
continue?
ans = [](0x0)
ans =
2.00000 0.00000 0.00000 0.00000
15.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
Continue with Transformation 2?
Transformation 2
===========================
Pixel:
1
1
Pixel falls in image point:
-0.066987
0.933013
1.000000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
1
2
Pixel falls in image point:
-0.56699
1.36603
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
1
3
Pixel falls in image point:
-1.0670
1.7990
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
1
4
Pixel falls in image point:
-1.5670
2.2321
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
1
Pixel falls in image point:
0.36603
1.43301
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
2
Pixel falls in image point:
-0.13397
1.86603
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
3
Pixel falls in image point:
-0.63397
2.29904
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
2
4
Pixel falls in image point:
-1.1340
2.7321
1.0000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
1
Pixel falls in image point:
0.79904
1.93301
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
2
Pixel falls in image point:
0.29904
2.36603
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
3
Pixel falls in image point:
-0.20096
2.79904
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
3
4
Pixel falls in image point:
-0.70096
3.23205
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
1
Pixel falls in image point:
1.2321
2.4330
1.0000
Drawn!
Interpolated Value:
4.0000
continue?
ans = [](0x0)
===========================
Pixel:
4
2
Pixel falls in image point:
0.73205
2.86603
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
3
Pixel falls in image point:
0.23205
3.29904
1.00000
Not Drawn
continue?
ans = [](0x0)
===========================
Pixel:
4
4
Pixel falls in image point:
-0.26795
3.73205
1.00000
Not Drawn
continue?
ans = [](0x0)
ans =
0.00000 0.00000 0.00000 4.00000
0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
Scene1 = [8,1,1,2;6,5,8,1;10,2,8,1;4,3,1,6];
imagesc(Scene1);
Screen = [];
input('Continue?')
t1 = [.5*cosd(10),-sind(10),0;sind(10),.5*cosd(10),0;0,0,1];
t1p = [2*cosd(10),sind(10),0;-sind(10),2*cosd(10),0;0,0,1];
t2 = [2*cosd(30),sind(30),0;-sind(30),2*cosd(30),0;0,0,1];
t2p = [.5*cosd(30),-sind(30),0;sind(30),.5*cosd(30),0;0,0,1];
disp('Transformation 1');
for i = 1:4
for j = 1:4
disp('===========================');
disp('');
disp('Pixel: '), disp(i), disp(j);
pixel = [i;j;1];
pixelAddress = t1p*pixel;
disp('Pixel falls in image point:'), disp(pixelAddress);
if (pixelAddress(1) >= 1) && (pixelAddress(1) <=4 && pixelAddress(2) >= 1) && (pixelAddress(2) <=4)
drawn = true;
disp('Drawn!');
if drawn
% image address
xLeft = floor(pixelAddress(1));
xRight = ceil(pixelAddress(1));
yUp = floor(pixelAddress(2));
yDown = ceil(pixelAddress(2));
% intensity
i1 = Scene1(xLeft,yUp);
i2 = Scene1(xRight,yUp);
i3 = Scene1(xLeft,yDown);
i4 = Scene1(xRight,yDown);
% interpolation 1 x-x top
% Generate linear system
equations = [xLeft,1;xRight,1];
solutions = [i1;i2];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
ix1 = AB(1)*pixel(1) + AB(2);
% interpolation 2 x-x bottom
% Generate linear system
equations = [xLeft,1;xRight,1];
solutions = [i3;i4];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
ix2 = AB(1)*pixel(1) + AB(2);
% interpolation 3 y-y from previous interpolated points
% Generate linear system
equations = [yUp,1;yDown,1];
solutions = [ix1;ix2];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
InterpolatedValue = AB(1)*pixel(2) + AB(2);
% Add value to Screen
Screen(end+1) = InterpolatedValue;
disp('Interpolated Value: '), disp(InterpolatedValue);
end
else
Screen(end+1) = 0;
disp('Not Drawn');
end
input('continue?')
end
end
reshape(Screen,4,4)
imagesc(Screen);
input('Continue with Transformation 2?');
Screen = [];
disp('Transformation 2');
for i = 1:4
for j = 1:4
disp('===========================');
disp('');
disp('Pixel: '), disp(i), disp(j);
% pixel address in homogenous form
pixel = [i;j;1];
% pixel adress within image
pixelAddress = t2p*pixel;
disp('Pixel falls in image point:'), disp(pixelAddress);
% if the pixel is within the image, draw it
if (pixelAddress(1) >= 1) && (pixelAddress(1) <=4 && pixelAddress(2) >= 1) && (pixelAddress(2) <=4)
drawn = true;
disp('Drawn!');
if drawn
% image address
xLeft = floor(pixelAddress(1));
xRight = ceil(pixelAddress(1));
yUp = floor(pixelAddress(2));
yDown = ceil(pixelAddress(2));
% intensity
i1 = Scene1(xLeft,yUp);
i2 = Scene1(xRight,yUp);
i3 = Scene1(xLeft,yDown);
i4 = Scene1(xRight,yDown);
% interpolation 1 x-x top
% Generate linear system
equations = [xLeft,1;xRight,1];
solutions = [i1;i2];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
ix1 = AB(1)*pixel(1) + AB(2);
% interpolation 2 x-x bottom
% Generate linear system
equations = [xLeft,1;xRight,1];
solutions = [i3;i4];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
ix2 = AB(1)*pixel(1) + AB(2);
% interpolation 3 y-y from previous interpolated points
% Generate linear system
equations = [yUp,1;yDown,1];
solutions = [ix1;ix2];
% Solve linear system
AB = equations\solutions;
% Replace A and B values in equations to get next intensity
InterpolatedValue = AB(1)*pixel(2) + AB(2);
% Add value to Screen
Screen(end+1) = InterpolatedValue;
disp('Interpolated Value: '), disp(InterpolatedValue);
end
else
Screen(end+1) = 0;
disp('Not Drawn');
end
input('continue?')
end
end
reshape(Screen,4,4)
imagesc(Screen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment