Applicability of imgradient function in below code???
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Object Analysis 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!