[08x05] Julia Image Processing: Mandelbrot Set on CPU and GPU

Updated on Mar 21,2024

[08x05] Julia Image Processing: Mandelbrot Set on CPU and GPU

Table of Contents

  1. Introduction
  2. Generating an Image using Julia without a Plotting Package
    1. What is the Images.jl Package
    2. Converting a Julia Matrix into an Image
    3. GrayScale Color Scale
    4. RGB Color Scale
  3. Understanding the Mandelbrot Set
    1. The Complex Plane
    2. testing Membership in the Mandelbrot Set
    3. Generating a Static GrayScale Image of the Mandelbrot Set using the CPU
    4. Exploring the Properties of the Mandelbrot Set Image
  4. Generating an Interactive Color Image of the Mandelbrot Set using the GPU
    1. Leveraging CUDA-compatible GPUs
    2. Using Symbolics.jl Package for GPU image generation
    3. Exploring the Image and Zooming In
    4. Saving the Image to Disk
  5. Conclusion

Generating an Image using Julia without a Plotting Package

In this article, we will explore how to generate an image using Julia without relying on a plotting package. While plotting packages can be slow to load and compile, there is a way to create images using Julia alone. We will use the Images.jl package along with Pluto notebooks to introduce ourselves to image processing in Julia. Specifically, we will learn about the Mandelbrot Set and how to generate an image of it using both the CPU and the GPU, without the need for a plotting package.

What is the Images.jl Package

Images.jl is a versatile package that enables us to convert Julia Matrices into images without the use of a plotting package. By adding the Images.jl package and the PlutoUI package to our project, we can start exploring the functionalities of generating images using Julia.

Converting a Julia Matrix into an Image

To generate an image, we need to understand how to convert a Julia Matrix into an image. The size of an image is determined by its width and Height, which corresponds to the number of columns and rows in a Matrix, respectively. By extending the concepts of color scales, such as GrayScale and RGB, we can generate images by assigning different shades of gray or combining different colors.

GrayScale Color Scale

The GrayScale color scale converts a value between 0 and 1 into a shade of gray. Using a Slider, we can observe the different shades of gray, where 0 represents black and 1 represents white. The N0f8 data type in the Images.jl package allows for 256 steps of gray, providing us with a range of shades. Values between 0 and 1 are mapped to different shades of gray, making it a convenient color scale for generating grayscale images.

RGB Color Scale

The RGB color scale is a combination of the colors red, green, and blue. By assigning different values to each color Channel, we can generate any visible color. Sliders can be used to Visualize the variations in color. A value of 0 for each channel results in a black image, while a value of 1 for each channel gives us a white image. Combining different values for each channel allows us to create a wide range of colors.

In the next section, we will delve into the Mandelbrot Set and explore its properties.

Understanding the Mandelbrot Set

The Mandelbrot Set is a fascinating mathematical structure that can be visualized as an image on a complex plane. In this section, we will learn about complex numbers, the complex plane, and how to determine whether a complex number is part of the Mandelbrot Set. We will also generate a static grayscale image of the Mandelbrot Set using the CPU.

The Complex Plane

The complex plane is a two-dimensional space with real numbers along the x-axis and imaginary numbers along the y-axis. Complex numbers are defined as a combination of a real number and an imaginary number. The Mandelbrot Set is drawn on the complex plane, with points on the plane representing complex numbers.

To identify which complex numbers are part of the Mandelbrot Set, we need to iterate each point of interest through a mathematical function. The function applies a formula to determine whether the value tends towards infinity or stays bounded. By repeating this iterative process, we can Colorize the complex plane to represent the Mandelbrot Set.

Testing Membership in the Mandelbrot Set

To determine whether a complex number belongs to the Mandelbrot Set, we iterate it through the function z = z^2 + c, where z and c are complex numbers. The iteration starts with z equal to c. If the absolute value of z exceeds a certain threshold (usually 2), the complex number escapes and is considered not part of the Mandelbrot Set. On the other HAND, if the absolute value of z remains bounded throughout the iterations, the complex number is included in the Mandelbrot Set.

By applying this test to a range of complex numbers on the complex plane, we obtain a set of values that can be converted into an image representing the Mandelbrot Set. The resulting image displays the points within the Mandelbrot Set in black, while the points outside the set are displayed with different shades indicating the number of iterations it took for them to escape the threshold.

Generating a Static GrayScale Image of the Mandelbrot Set using the CPU

Before diving into the GPU implementation, let's generate a static grayscale image of the Mandelbrot Set using the CPU. By leveraging the concepts we learned about the complex plane and membership testing, we can create a matrix that represents the Mandelbrot Set. Each element in the matrix corresponds to a point on the complex plane, and the value indicates the number of iterations it took for the complex number to escape.

By converting the matrix into an image, we can visualize the Mandelbrot Set. The resulting image will display the points within the set in black, while the surrounding area will show different shades of gray representing the escape time.

Most people like