Hi @yuchen,
You mentioned,”does anyone know how to generate the graph as shown below based on a torus?”
Please see my response to your comments below.
I have implemented two different Matlab code snippets in order for you to choose and customize it based on your requirements. Both Code 1 and Code 2 serve to generate a 3D torus shape, but they approach the task differently, particularly in terms of how they handle the input image and the visual output.
Code 1 Breakdown
% Load the original image img = imread('/MATLAB Drive/IMG_7974.PNG');
%Convert the image to grayscale grayImg = rgb2gray(img);
% Create a binary mask binaryMask = imbinarize(grayImg);
For more information on imbinarize function, please refer to
% Generate the torus shape theta = linspace(0, 2*pi, 100); % Angle around the torus phi = linspace(0, 2*pi, 100); % Angle around the tube [Theta, Phi] = meshgrid(theta, phi);
% Define the torus parameters R = 5; % Major radius r = 2; % Minor radius
% Parametric equations for the torus X = (R + r * cos(Phi)) .* cos(Theta); Y = (R + r * cos(Phi)) .* sin(Theta); Z = r * sin(Phi);
% Plot the torus figure; surf(X, Y, Z, 'FaceColor', 'interp', 'EdgeColor', 'none'); axis equal; xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); title('Torus Shape Generated from Image'); view(3); grid on;
Image Loading and Processing: Loads an image and converts it to grayscale, then creates a binary mask. However, the binary mask is not utilized further in generating the torus.
Torus Generation: Defines parameters for the torus (major radius R and minor radius r) and uses parametric equations to compute X,Y and Z coordinates for plotting the torus.
Visualization: Utilizes MATLAB’s surf function to create a surface plot of the torus without any image texture. For more information on surf function, please refer to:
https://www.mathworks.com/help/matlab/ref/surf.html
Please see attached.
Code 2 Breakdown
%Define the path to the image imagePath = '/MATLAB Drive/IMG_7974.PNG';
% Read the image img = imread(imagePath);
% Create a figure figure;
% Define parameters for the torus R = 3; % Major radius r = 1; % Minor radius [theta, phi] = meshgrid(linspace(0, 2*pi, 100), linspace(0, 2*pi, 50));
% Parametric equations for the torus X = (R + r * cos(phi)) .* cos(theta); Y = (R + r * cos(phi)) .* sin(theta); Z = r * sin(phi);
% Create the torus surface surf(X, Y, Z, 'FaceColor', 'texturemap', 'CData', img, 'EdgeColor', 'none');
% Set axis properties axis equal; xlabel('X-axis'); ylabel('Y-axis'); zlabel('Z-axis'); title('Torus with Embedded Image'); view(3); grid on;
% Adjust lighting light; lighting gouraud;
Image Loading: Similar to Code 1, it loads an image but does not convert it to grayscale or create a binary mask.
Torus Generation: Defines different radii for the torus, which results in a smaller torus compared to Code 1. Also uses parametric equations for generating coordinates for plotting.
Visualization with Image Texture: The key difference is that this code employs CData to map the loaded image onto the surface of the torus using texturemap creating a visually integrated representation of the image on the toroidal shape.
Please see attached.
Both codes address the requirement to generate a toroidal graph based on an image; however, their execution differs significantly:
Code 1 focuses primarily on creating a basic 3D torus shape without incorporating any visual data from the image itself, which might not meet your expectations if you seek an integrated visual output. However, Code 2, in contrast, effectively meets your requirements by embedding the image as a texture on the surface of the torus. This approach enhances visual appeal and fulfills yours desire for a graphical representation that incorporates elements from the original image. Now, depending on the size of img, performance may vary. Also, larger images can lead to longer rendering times or memory issues. If you notice Code 2 adds lighting effects (light and lighting gouraud), which can enhance depth perception and realism in visualization—an important aspect when presenting graphical data.
Hope this helps.
Please let me know if you have any further questions.