Skip to content

Instantly share code, notes, and snippets.

@zahlenteufel
Last active December 14, 2019 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zahlenteufel/9513461 to your computer and use it in GitHub Desktop.
Save zahlenteufel/9513461 to your computer and use it in GitHub Desktop.
Magnifier Glass effect in MATLAB

Sample Image

c  = 0.2;
f  = @(z) sqrt(1 - z.^2) - c;
x0 = -0.87;
x1 = c * x0 / sqrt(1 - x0^2);

clf
hold on
axis equal
axis off

x = linspace(-1, 1, 100);
plot(x, f(x), 'b');
plot(x, 0*x, 'k-');
line([x0 x1], [f(x0) 0], 'Color', 'r', 'LineStyle', '--');
line([ 0 x1], [-c    0], 'Color', 'r', 'LineStyle', ':');
plot(x1, 0, 'ro', 'MarkerSize', 5);
plot(0, -c, 'r+');
original_image = imread('butterfly.jpg');
[n m c] = size(original_image);
assert(c == 3);
angles = linspace(0, 20 * pi, 1000);
for i=1:length(angles)
radius = 200;
c = 0.5 + sin(angles(i)) / 2;
[x y] = meshgrid(1:m, 1:n);
center = [radius, 200];
dy = y - center(1);
dx = x - center(2);
r0 = hypot(dx, dy) ./ radius * 2;
quot = c ./ sqrt(1 - r0.^2);
quot(r0.^2 > sqrt(1 - c^2)) = 1;
nx = center(1) + round(dx .* quot);
ny = center(2) + round(dy .* quot);
idx = n * (nx - 1) + ny;
new_image = reshape(original_image([idx(:); m*n+idx(:); 2*m*n+idx(:)]), n, m, 3);
imshow(new_image);
pause(0.01);
end
@ignaciowg93
Copy link

Hello, excellent code! Is there any chance that you can explain how did you calculate that algorithm? i've been trying to understand , but so far, i haven't been able to fuly get it.

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment