MATLAB Answers

what is zernike moments?

45 views (last 30 days)
Mansoor ahmadi
Mansoor ahmadi on 20 Jan 2015
Edited: arun anoop m on 9 Sep 2019
hello!
I have gotten the code of zernike moments from mathworks site it work good and it return tow value, but I don't know what is that and also I don't know how many moment it extract from image if it extract one moment how I edit this code that extract several moment. this is the code:
each function is in separate script.
sorry for my bad English!
thanks in advance.
a=imread('Oval_H.png');
p = im2bw(a,0.5);
a=imread('4.png');
figure(1);subplot(2,3,1);imshow(p);
title('Horizontal oval');
p = logical(not(p));
tic
[~, AOH, PhiOH] = Zernikmoment(p,n,m); % Call Zernikemoment fuction
Elapsed_time = toc;
xlabel({['A = ' num2str(AOH)]; ['\phi = ' num2str(PhiOH)]});
%***************************************************************************
function [Z A Phi] = Zernikmoment(p,n,m)
N = size(p,1);
x = 1:N; y = x;
[X,Y] = meshgrid(x,y);
R = sqrt((2.*X-N-1).^2+(2.*Y-N-1).^2)/N;
Theta = atan2((N-1-2.*Y+2),(2.*X-N+1-2));
R = (R<=1).*R;
Rad = radialpoly(R,n,m); % get the radial polynomial
Product = p(x,y).*Rad.*exp(-1i*m*Theta);
Z = sum(Product(:)); % calculate the moments
cnt = nnz(R)+1; % count the number of pixels inside the unit circle
Z = (n+1)*Z/cnt; % normalize the amplitude of moments
A = abs(Z); % calculate the amplitude of the moment
Phi = angle(Z)*180/pi; % calculate the phase of the mement (in degrees)
%**********************************************************************************
function rad = radialpoly(r,n,m)
rad = zeros(size(r)); % Initilization
for s = 0:(n-abs(m))/2
c = (-1)^s*factorial(n-s)/(factorial(s)*factorial((n+abs(m))/2-s)*factorial((n-abs(m))/2-s));
rad = rad + c*r.^(n-2*s);
end

  1 Comment

Mansoor ahmadi
Mansoor ahmadi on 21 Jan 2015
Please answer my above question if someone know!

Sign in to comment.

Answers (5)

student
student on 19 Dec 2016
Hi Mansoor. I used the same code but i have different error which is : Index exceeds matrix dimensions.
Error in Zernikmoment (line 47) Product = p(x,y).*Rad.*exp(-1i*m*Theta);
Error in process_predict_leaf (line 80) [mom, amplitude, angle] = Zernikmoment(b,4,0); % Call Zernikemoment fuction n=4, m=0
Did you know why I got this error?

  0 Comments

Sign in to comment.


ancy micheal
ancy micheal on 21 Nov 2017
unable to execute : Undefined function or variable 'n'.

  0 Comments

Sign in to comment.



arun anoop m
arun anoop m on 9 Sep 2019
hello Mansoor sir,
your code is working fine
But you need to keep it as seperate files.
%*******************************************************************************
zerniketest.m
a=imread('Oval_H.png');
p = im2bw(a,0.5);
% a=imread('4.png');
figure(1);subplot(2,3,1);imshow(p);
title('Horizontal oval');
p = logical(not(p));
tic
[~, AOH, PhiOH] = Zernikmoment(p,n,m); % Call Zernikemoment fuction
Elapsed_time = toc;
xlabel({['A = ' num2str(AOH)]; ['\phi = ' num2str(PhiOH)]});
%*******************************************************************************
radialpoly.m
function rad = radialpoly(r,n,m)
rad = zeros(size(r)); % Initilization
for s = 0:(n-abs(m))/2
c = (-1)^s*factorial(n-s)/(factorial(s)*factorial((n+abs(m))/2-s)*factorial((n-abs(m))/2-s));
rad = rad + c*r.^(n-2*s);
end
%*******************************************************************************
Zernikemoment.m
function [Z A Phi] = Zernikmoment(p,n,m)
N = size(p,1);
x = 1:N; y = x;
[X,Y] = meshgrid(x,y);
R = sqrt((2.*X-N-1).^2+(2.*Y-N-1).^2)/N;
Theta = atan2((N-1-2.*Y+2),(2.*X-N+1-2));
R = (R<=1).*R;
Rad = radialpoly(R,n,m); % get the radial polynomial
Product = p(x,y).*Rad.*exp(-1i*m*Theta);
Z = sum(Product(:)); % calculate the moments
cnt = nnz(R)+1; % count the number of pixels inside the unit circle
Z = (n+1)*Z/cnt; % normalize the amplitude of moments
A = abs(Z); % calculate the amplitude of the moment
Phi = angle(Z)*180/pi; % calculate the phase of the mement (in degrees)
%*******************************************************************************
Reference:
[1] Zernike Moments, version 1.5 (15.1 KB) by Amir Tahmasbi ,"MATLAB Code for the Fast Calculation of Zernike Moments of order n and repetition m on NxN images",Available:https://in.mathworks.com/matlabcentral/fileexchange/38900-zernike-moments

  0 Comments

Sign in to comment.


arun anoop m
arun anoop m on 9 Sep 2019
Edited: arun anoop m on 9 Sep 2019
One more thing sir,
The program will only for black and white images. So if u need to show other images, has to do the following,
a=imread('flowers.jpg');
p = im2bw(a,0.5);
figure(1);subplot(2,3,1);imshow(p);
title('Horizontal oval');
p = logical(not(p));
tic
[~, AOH, PhiOH] = Zernikmoment(p,n,m); % Call Zernikemoment fuction
Elapsed_time = toc;
xlabel({['A = ' num2str(AOH)]; ['\phi = ' num2str(PhiOH)]});
Actually i am searching a code for getting Angle(A) and magnitude(Phi) as image output. I dont know how to call array function in another file.
If all the programs are in same file, can use the following code,
figure,imshow(uint8(A)),title('Zernike Moment Angle');
instead
xlabel({['A = ' num2str(AOH)]; ['\phi = ' num2str(PhiOH)]});
Reference:
[1] Zernike Moments, version 1.5 (15.1 KB) by Amir Tahmasbi ,"MATLAB Code for the Fast Calculation of Zernike Moments of order n and repetition m on NxN images",Available:https://in.mathworks.com/matlabcentral/fileexchange/38900-zernike-moments

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by