Clear Filters
Clear Filters

Imgradient3 function: can we used this function for finding chnage in gradient of surface points.

7 views (last 30 days)
I generated coordinates by converting 3d image to stl. Now I want to give the x coordinates, y coordinates and z coordinates of all the ponts as input in imgradient3 function. Means instead of Gx, Gy and Gz can we put Intx, Inty and Intz in that function. intx isx coordinates of all the genrated points using surface meshing. I am interested in finding the change in gradient of all the points with respect to their neighbours.

Answers (1)

Arun on 17 Apr 2024
Hi Saurav,
I understand that you wish to calculate change in gradient of all the points with respect to their neighbours for a 3-D image.
Below are the steps along with code snippets that could assist in finding the gradient of all the points for a 3-D image and then finally change in gradient of all the points with respect to their neighbours:
Step-1 : Acquire/Load the image. The below code creates a 3-D image to be used, one can use an existing image as well.
% Step-1:Add 3D image
dims = [100, 100, 100];
image3D = zeros(dims); % Initialize with zeros
[x, y, z] = ndgrid(1:dims(1), 1:dims(2), 1:dims(3));
center = dims/2;
radius = 30;
sphere = sqrt((x-center(1)).^2 + (y-center(2)).^2 + (z-center(3)).^2) <= radius;
image3D(sphere) = 1; % Assign a value to voxels inside the sphere
cubeStart = [30, 30, 30];
cubeEnd = [60, 60, 60];
image3D(cubeStart(1):cubeEnd(1), cubeStart(2):cubeEnd(2), cubeStart(3):cubeEnd(3)) = 2; % Assign a different value to the cube
% Display the image
p1 = patch(isosurface(image3D, 0.5));
set(p1, 'FaceColor', 'red', 'EdgeColor', 'none');
view(3); axis tight;
camlight; lighting gouraud;
Step-2 : Utilize the “imgradientxyz” function to get the directional gradients “Gx”, ”Gy”, and “Gz”.
[Gx, Gy, Gz] = imgradientxyz(image3D);
Step-3 : Use the “imgradient3” function to get the gradient magnitude for the image.
[Gmag,Gazimuth,Gelevation] = imgradient3(Gx,Gy,Gz);
Step-4 : Apply the “gradient” function to calculate the change.
[dx, dy, dz] = gradient(Gmag);
This will help to calculate the change in gradient of the points for a 3-D image.
Please refer to the shared MATLAB documentation links for more information:
I hope this helps.
Saurav on 23 Apr 2024
Here is the code
I have attached the tiff stack folder as well as stl image file.
TR=stlread('aggregate_1.stl'); %% This is stl file image created from 3d stacked image
trimesh(TR); %% i have attached the stl file as well
% fid = fopen('VA1.vol');
% voxels = fread(fid); %read as 16 bit unsigned. I'm assuming they're unsigned
% volumeViewer(voxels);
%number images with 1.tiff-N.tiff
files = dir('/MATLAB Drive/tiff3/*.tif');
tiff_stack = imread(fullfile(files(1).folder, files(1).name));
for ii = 2 : size(files, 1)
temp_tiff = imread(fullfile(files(ii).folder, files(ii).name));
tiff_stack = cat(3 , tiff_stack, temp_tiff);
% %volumeViewer(tiff_stack);
% isosurf = isosurface(tiff_stack, 100);
% faces = isosurf.faces;
% vertices = isosurf.vertices;
[Gx, Gy, Gz] = imgradientxyz(tiff_stack);
[Gmag,Gazimuth,Gelevation] = imgradient3(Gx,Gy,Gz);
[dx, dy, dz] = gradient(Gmag);
Arun on 23 Apr 2024
I executed the code shared by you, sharing my finding for the problems mentioned in previous comment:
  1. Problem-1: tiff_stack contains non-zero values.
  2. Problem-2: Gx, Gy and Gz have non-zero values.
  3. Problem-3: No issue while execution, in MATLAB R2024a.
To verify please use the below lines of code at the end of the shared code:
noOfNonZeroValuesInTiff_stack = size(find(tiff_stack~=0),1)
noOfNonZeroValuesInGx = size(find(Gx~=0),1)
noOfNonZeroValuesInDx = size(find(dx~=0),1)
Here is the snapshot of the output after the execution:
Hope this helps, please do let me know if you face any issue.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!