# Thilina's Blog

## Photo Effects using MATLAB 2.0 – Optical Illusion using MATLAB – Shake your head and see the image

This is something I found really interesting while wandering in facebook. Most of facebook fan pages shares a set of images which has black and white columns saying “shake your head and see the image”, which will results in seeing a hidden image in black and white column grid. Later when I search google, also found a number of images which similar to the images shared around facebook. In this article I share my attempts and experience on how I create my own optical illusion (shake your head and see image) using MATLAB.

When we look at the image closely we can notice the hidden image with very low intensity, which is not noticeable when we look at it from a average distance of our eye to monitor. This is what I have done and it gives much better results when we use gray scale images than colour images. Fist I made a frame which has black and white columns (binary image) in same size of my source image. Then I performed element-wise multiplication with source image and created brightness shifted and normalized image as follows. Then again it was multiplied element-wise with the negation of frame.

My source image is as follows.

Following code will explain how this implemented in MATLAB.

```clear all;
close all;
clc;
imgs = cast(im_source,'double');
% in here the source image is resampled into 60 gray levels so that it can be shift in 195 gray levels in 8-bit gray scale.
img = 60*((imgs)./(max(max(imgs))));
img = img + 195; % shifting by 195 gray levels
[r c d] = size(img);

step = floor(c/160); % step is the number of black columns in the resulting image

for i = 1 : 2*step : c
img(:,i:i+step,:) = 0; %replaceable line
end

imout = cast(img,'uint8');

imshow(imout);
```

Resulting image is as follows,

you can also perform this for the RGB images by making following modification to above code.

```im_source = (imread( 'image.jpg'));
imgs = cast(im_source, 'double');
img = 60*((imgs)./max(max(max(imgs))));
img = img + 195;

[r c d] = size(img);
step = floor(c/160);

for i = 1 : 2*step : c
img(:,i:i+step,:) = 0;
end

imout = cast(img, 'uint8');
imshow(imout);
```

Resulting image is as follows;

The source image is a picture of the Temple of Sacred Tooth in Kandy, Sri Lanka. Thank you very much for reading.