Black and White Optical Illusion in MATLAB


An optical or visual phenomenon that creates the perception of distortion, movement or any other type of fascinating visual effect by using back and white patterns is referred to as back and white optical illusion. These back and white patterns utilize the way our eyes and brain process the visual information and create an illusion to mislead our perception.

The back and white optical illusions use various black and white patterns, shapes, and lines arranged in a specific style. These arrangements are made such that they can create illusion of movement, distortion, depth, and more.

In this article, we will learn to create different types of black and white optical illusions using MATLAB programming.

Example

The following MATLAB code will generate a simple square box shaped back and white optical illusion.

% Define the size of illusion box
BoxSize = 400;
% Create a grid of coordinates
[a, b] = meshgrid(1:BoxSize, 1:BoxSize);
% Specify the optical illusion parameters
% Define number of pattern cycles
cycle = 50;
% Define intensity of the illusion
intensity = 150;
% Create a black and white optical illusion pattern
illusion_pattern = cos(2 * pi * a / cycle) + cos(2 * pi * b / cycle);
% Control the intensity of the illusion pattern
illusion_pattern = illusion_pattern * intensity;
% Perform normalization of pattern values to the range [0, 1]
illusion_pattern = (illusion_pattern - min(illusion_pattern(:))) / (max(illusion_pattern(:)) - min(illusion_pattern(:)));
% Display the black and white optical illusion
imshow(illusion_pattern, 'InitialMagnification', 'fit'); title('Black and White Optical Illusion');

Output

Explanation

In this MATLAB program, we have generated a black and white optical illusion. In the above MATLAB code, first we have defined a box of size 400px and store in the ‘BoxSize’ variable. After that we call the ‘meshgrid’ function to create a grid of coordinates of the pattern. Then, we specify the parameters of the optical illusion ‘cycle’ and ‘intensity’. Where, the parameter ‘cycle’ determines the number of cycles in the pattern and the parameter ‘intensity’ determines the strength or amplitude of the illusion.

After that we specify a formula to generate a black and white optical illusion pattern and store the result in the ‘illusion_pattern’ variable. Then, we normalize the values of the pattern to ensure that they are within the range of [0, 1];

Finally, we display the generated black and white optical illusion by using the ‘imshow’ function.

Note – You can change the values of illusion parameters ‘cycle’ and ‘intensity’ to generate different variations of this optical illusion.

Example

The following MATLAB code demonstrates the creation of black and white optical illusion on images.

% MATLAB Program to create black and white optical illusion on image
% Read the input image
img = imread('https://www.tutorialspoint.com/matlab/images/matlab-mini-logo.jpg');
% Transform the input image to binary image
img2 = im2bw(img);
% Create a complemented image of the binary image
img3 = imcomplement(img2);
% Display the original, binary, and complemented image
subplot(1, 3, 1); imshow(img); title('Original Image');
subplot(1, 3, 2); imshow(img2); title('Binary Image');
subplot(1, 3, 3); imshow(img3); title('Complemented Image');

Output

Explanation

In this MATLAB program, we start by reading an input image by using the ‘imread’ function. Then, we convert the input image to a binary image by using the ‘im2bw’ function. Next, we use the ‘imcomplement’ function to create a complemented image of the binary image. Finally, we use the ‘imshow’ function to display the original, binary, and complemented images with proper titles.

This is how, we can create a black and white optical illusion on an image in MATLAB.

Conclusion

In conclusion, in this article, we have discussed the use of MATLAB programming to create a raw black and white optical illusion and the black and white optical illusion on images. The above two MATLAB programs demonstrate the creation of black and white illusion with simple codes.

Updated on: 07-Aug-2023

45 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements