Can anyone please explain me the Fourier Descriptor coding?

4 views (last 30 days)
I have the coding for Fourier Descriptor.But i can't able to understand what is happening in some lines. This is the full coding
function [f]= foury(im)
% input: image in binary form, from which the Fourier Descriptors are calculated
% output: Fourier Descriptor ,which describes the shape of the image by calculating Fourier Transform of its Boundary
im = im2bw(im);
b = bwboundaries(im);
[max_size, max_index] = max(cellfun('size', b, 1));
if size(b,1)==0
c=zeros(10,2);
else
c = b{max_index};
end
% calculating Fourier Transform of image boundaries
s = c(:, 1) + 1i*c(:, 2);
descriptors = fft(s,512);
descriptors=descriptors';
D=abs(descriptors(1));
descriptor=zeros(511,1);
for i=1:511
descriptor(i)=abs(descriptors(i+1))/D;
end
f = descriptor;
f = transpose(f);
i want to know the explanation of this part.Please help me
[max_size, max_index] = max(cellfun('size', b, 1));
if size(b,1)==0
c=zeros(10,2);
else
c = b{max_index};
end

Answers (1)

Hari
Hari on 4 Feb 2025
Edited: Walter Roberson on 4 Feb 2025
Hi Gobika,
I understand that you want an explanation of a specific part of the Fourier Descriptor code, particularly the lines involving max(cellfun('size', b, 1)) and the subsequent conditional statement.
  1. The line [maxsize, maxindex] = max(cellfun('size', b, 1)); is used to find the boundary with the maximum number of points in the binary image. Here, b is a cell array where each cell contains the boundary points of a connected component in the binary image.
  2. The function cellfun('size', b, 1) applies the 'size' operation to each element of the cell array b, returning the number of rows (points) for each boundary. This results in a vector where each element represents the size of a boundary.
  3. The max function is then used to find the maximum size (maxsize) and its corresponding index (maxindex) in the vector. This index is used to identify the largest boundary in the image.
  4. The conditional statement if size(b,1)==0 checks whether there are any boundaries detected in the image. If b is empty (i.e., no boundaries found), it initializes c as a 10x2 zero matrix to handle cases where no valid boundary is present.
  5. If boundaries exist, c = b{maxindex}; assigns the largest boundary (in terms of the number of points) to the variable c for further processing. This boundary will be used to compute the Fourier Descriptors.
Refer to the documentation of MATLAB's bwboundaries function for more details on boundary extraction: https://www.mathworks.com/help/images/ref/bwboundaries.html
Refer to the documentation of MATLAB's cellfun function for more details on applying functions to cell arrays: https://www.mathworks.com/help/matlab/ref/cellfun.html
Hope this helps!

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!