Effacer les filtres
Effacer les filtres

Exceed array bound at position 2

1 vue (au cours des 30 derniers jours)
Miles Hao Peng Su
Miles Hao Peng Su le 14 Nov 2022
Hi guys, I am trying to calculate the matrix moments for a binary image, however I encountered the following error:
>> run_A_2
Index in position 2 exceeds array bounds. Index must not exceed 438.
Error in features_2 (line 35)
m01 = m01 + j * Iin(i,j) / 255;
Error in run_A_2 (line 6)
[P, A, C, xbar, ybar, phione] = features_2(I1);
Here is my code:
function [P, A, C, xbar, ybar, phione] = features_2(Iin)
%perimeter
P = sum(sum(bwperim(Iin)==1)); %bwperim returns the perimeter
%by calculating the distance between
%each adjoining pair of pixels around
%the border of the binary image.
%area
A = sum(sum(Iin == 255)); %summing the number of pixel that are white
%compactness
C = P^2/(4 * pi * A);
%centroid
[rows cols] = size(Iin);
m00 = A;
m10 = 0;
m01 = 0;
%calculating m10
for i = 1:rows
for j = 1:cols
m10 = m10 + j * Iin(i,j) / 255;
end
end
%calculating m01
for i = 1:cols
for j = 1:rows
m01 = m01 + j * Iin(i,j) / 255;
end
end
xbar = m10 / m00; %m10/m00
ybar = m01 / m00; %m01/m00
May I know what is the issue?
Thanks!

Réponse acceptée

Askic V
Askic V le 14 Nov 2022
This is your problem:
for i = 1:cols
for j = 1:rows
m01 = m01 + j * Iin(i,j) / 255;
end
end
you mixed columns and rows comparing to the previous for loop
  1 commentaire
Miles Hao Peng Su
Miles Hao Peng Su le 14 Nov 2022
Ohh I see! Here is my improved code:
for i = 1:rows
m10 = m10 + i * sum(Iin(i,:) / 255);
end
It works fine now! Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by