Azzera filtri
Azzera filtri

Applicability of imgradient function in below code???

2 visualizzazioni (ultimi 30 giorni)
I have find some indices using imgradient. Can anyone guid whether my code is correct or not. code is giving the result as per our rquirement. But I am not sure about the applicability of imgradient function.
imgradient function is generally used to find the edges of the image but i have given the coordinate as input.
%clear all; close all; clc;
prompt = 'Enter number of images '; % prompt for input filename
num = input(prompt);
xc = zeros(num,1); yc = xc; AI = xc; % assign a column with initial zero values
for nn = 1:num
%% Formation of binary image
% prompt = 'Enter imagename with extension '; % prompt for input filename
%
% imname = input(prompt);
imname = ['t' num2str(nn) '.png']; % nn number image file will be readclc,clear all,close all
img= imread(imname);
%figure, imshow(img);
gray=rgb2gray(img);
imb=imbinarize(gray);
%figure, imshow(imb);
bw1 = imcomplement(imb);
%figure, imshow (bw1);
bw2=imfill(bw1,'holes');
%figure, imshow(bw2);
bw3 = imcomplement(bw2);
%figure, imshow(bw3);
%% Centroid calculation
picdata = fliplr(rot90(bw3,2));
%figure; imshow(picdata);
ref_level = picdata(1,1); %=1
tol = 1e-10;
Lx = 1; Ly = 1;
% define domain x,y \in [0,1] x [0,1]
x = linspace(0,Lx,size(picdata,2));
y = linspace(0,Ly,size(picdata,1));
[X,Y] = meshgrid(x,y);
figure; mesh(X,Y,picdata); view(2);
xin = X(picdata ~= ref_level);
yin = Y(picdata ~= ref_level); % x and y coordinates of points inside
xmax = max(xin); xmin = min(xin); % x and y extents of the region
ymax = max(yin); ymin = min(yin);
I = x <= xmax & x >= xmin; % logical indices for within the region
%disp(I)
J = y <= ymax & y >= ymin;
%disp(J)
x_i = x(I)'; y_j = y(J)'; % all grids intersecting with the region
picdi = picdata(:,I); picdj = picdata(J,:); % image data along gridlines
xc(nn) = sum(xin)/length(xin); %%disp(sum(xin)); % centroid calculation
yc(nn) = sum(yin)/length(yin); %%disp(sum(yin));
%% locating interfaces around the curve from x and y intersections
yeb = zeros(length(x_i),1); yet = zeros(length(x_i),1);
xel = zeros(length(y_j),1); xer = zeros(length(y_j),1);
for i = 1:length(x_i)
% points where x = x_i intersects the interface
yini = y(picdi(:,i)~=ref_level); %
yeb(i) = min(yini); yet(i) = max(yini);
end
for j = 1:length(y_j)
xinj = x(picdj(j,:)~=ref_level);
xel(j) = min(xinj); xer(j) = max(xinj);
end
Intx = [x_i;x_i;xel;xer]-xc(nn); Inty = [yeb;yet;y_j;y_j]-yc(nn); % interface x and y in centroid coordinate
%disp(Intx)
% scatter(Intx,Inty);
[Gmag,Gdir] = imgradient(Intx,Inty);
tht = Gmag(1:2:end);
AI(nn) = sum (abs(tht(2:end)-tht(1:end-1)));
end

Risposta accettata

Prasanna
Prasanna il 22 Apr 2024
Hi,
It is my understanding that your code is structured to process a series of images, extract certain features, and perform calculations based on those features. The use of imgradient in the context you've described is unconventional, as it is typically used for edge detection in images by calculating the gradient magnitude and direction at each pixel. However, your application of imgradient to coordinate data (derived from image processing steps) for another purpose is a creative use of the function, albeit not its typical application.
While imgradient is designed for image data, using it on coordinate data is technically feasible because it simply computes derivatives (changes) along the provided data. However, the interpretation of the results (Gmag, Gdir) will differ from their usual context (image gradients). Here, you're analysing changes along the curve defined by your coordinates, not pixel intensity changes.
Just ensure a few considerations and suggestions for the use in your case:
  • Ensure that the input to imgradient (Intx, Inty) is correctly representing the interfaces' coordinates as you intend. The function treats these as two-dimensional gradients, so the resulting magnitude and direction reflect changes between consecutive points in your coordinate list.
  • Verify that the calculation of Intx and Inty correctly represents the interfaces and that translating these coordinates to a centroid-based system is meaningful for your analysis.
In summary, while your use of imgradient on coordinate data is unconventional, it's a good adaptation. Ensure that the methodology aligns with your goals and that the interpretations of the results are valid within your requirements. However, if your goal is to analyse the shape or curvature of interfaces, consider other geometric or computational geometry techniques that might be more direct or interpretable for your purposes.
  3 Commenti
Prasanna
Prasanna il 23 Apr 2024
To adapt the algorithm you've used for 2D gradient magnitude analysis (Gmag) with imgradient to work with 3D gradient magnitude analysis obtained from imgradient3, you should also consider how you're interpreting the change in gradient magnitude across a 3D volume. The imgradient3 function computes the gradient magnitude and direction across three dimensions, providing a more complex dataset to analyze compared to 2D. Hence you have to provide the imgradient3 function, the Intz parameter as well. Also, analyzing 3D data can be significantly more complex and computationally intensive than 2D data, so the best approach will depend on the specific characteristics and requirements of your project. Since we are using a un-conventional approach with the imgradient function in 2D itself, you can try other geometric or computational geometry techniques that might be more direct to the question you are trying to solve.
Saurav
Saurav il 23 Apr 2024
Ok. Thanks Prasanna for your proper guidance on above problem.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by