How to calculate gradient of 512*512*4 image?
1 view (last 30 days)
Show older comments
Is this wrong or write?
If my output image is g(512*512*4) then gradient of g() is
gradver1=sqrt(g1(:,:,1).^2+g1(:,:,2).^2+g1(:,:,3).^2+g1(:,:,4).^2);
0 Comments
Answers (2)
Raghava S N
on 23 Oct 2024
Moved: Walter Roberson
on 20 Dec 2024
Hi,
From my understanding, you are looking to calculate the gradient of a 3D image.
Refer to this link which describes how to find the gradient magnitude and direction of 3-D image using the “imgradient3” function - https://www.mathworks.com/help/images/ref/imgradient3.html.
The directional gradients of a 3-D image can be found using the “imgradientxyz” function. Refer to this link for more information - https://www.mathworks.com/help/images/ref/imgradientxyz.html.
Hope this helps!
0 Comments
KALYAN ACHARJYA
on 20 Dec 2024
This 512×512×4 representation corresponds to four channels. It simplifies the computation of gradients for individual channels. Each channel g(:,:,1),g(:,:,2),g(:,:,3),g(:,:,4) is treated as a separate 2D image, and the gradient is calculated independently for each channel across the spatial dimensions.
[gx1, gy1] = gradient(g(:,:,1));
[gx2, gy2] = gradient(g(:,:,2));
[gx3, gy3] = gradient(g(:,:,3));
[gx4, gy4] = gradient(g(:,:,4));
Gradient Magnitude of each Channel
gradMag1 = sqrt(gx1.^2 + gy1.^2);
gradMag2 = sqrt(gx2.^2 + gy2.^2);
gradMag3 = sqrt(gx3.^2 + gy3.^2);
gradMag4 = sqrt(gx4.^2 + gy4.^2);
Now compute the combine gradient of all channels
grad_final= sqrt(gradMag1.^2 + gradMag2.^2 + gradMag3.^2 + gradMag4.^2);
Hope it helps!
1 Comment
DGM
on 20 Dec 2024
We can also use imgradient to skip a step here too. If we want the same behavior as gradient(), we need to specify the non-default method for calculating the directional derivative.
inpict = imread('pep_cmyk.tif.fakeextension.txt'); % CMYK, uint8
inpict = im2double(inpict); % needs to be float
% using gradient()
[gx1, gy1] = gradient(inpict(:,:,1));
[gx2, gy2] = gradient(inpict(:,:,2));
[gx3, gy3] = gradient(inpict(:,:,3));
[gx4, gy4] = gradient(inpict(:,:,4));
gradMag1 = sqrt(gx1.^2 + gy1.^2);
gradMag2 = sqrt(gx2.^2 + gy2.^2);
gradMag3 = sqrt(gx3.^2 + gy3.^2);
gradMag4 = sqrt(gx4.^2 + gy4.^2);
grad_final0 = sqrt(gradMag1.^2 + gradMag2.^2 + gradMag3.^2 + gradMag4.^2);
% using IPT imgradient()
gradMag1 = imgradient(inpict(:,:,1),'central');
gradMag2 = imgradient(inpict(:,:,2),'central');
gradMag3 = imgradient(inpict(:,:,3),'central');
gradMag4 = imgradient(inpict(:,:,4),'central');
grad_final = sqrt(gradMag1.^2 + gradMag2.^2 + gradMag3.^2 + gradMag4.^2);
% the error is negligible
immse(grad_final0,grad_final)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!