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

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)

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
figure;
p1 = patch(isosurface(image3D, 0.5));
set(p1, 'FaceColor', 'red', 'EdgeColor', 'none');
daspect([1,1,1]);
view(3); axis tight;
camlight; lighting gouraud;
Step-2 : Utilize the “imgradientxyz” function to get the directional gradients “Gx”, ”Gy”, and “Gz”.
%Step-2
[Gx, Gy, Gz] = imgradientxyz(image3D);
Step-3 : Use the “imgradient3” function to get the gradient magnitude for the image.
%Step-3
[Gmag,Gazimuth,Gelevation] = imgradient3(Gx,Gy,Gz);
Step-4 : Apply the “gradient” function to calculate the change.
%Step-4
[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.

6 Comments

First I run the above code.
Problem1: In image3d every points we are getting are zero value.
Problem2: Even in Gx, Gy and Gz every values are zero.
Problem3: Execution of script imgradient3 as a function is not supported:
/MATLAB Drive/imgradient3.m
Error in imgradient3 (line 22)
[Gmag,Gazimuth,Gelevation] = imgradient3(Gx,Gy,Gz);
Question: I have stacked tiff file can I put it in place of image3d
Yes, you can. use 'imread' function to read the image.
How to resolve the above three problem? problem1, problem 2 and problem3
Share your image file and code that you are using, I wll try to help.
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');
%disp(files);
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);
end
% %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);
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.

Categories

Asked:

on 9 Apr 2024

Commented:

on 23 Apr 2024

Community Treasure Hunt

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

Start Hunting!