Azzera filtri
Azzera filtri

Exceed array bound at position 2

1 visualizzazione (ultimi 30 giorni)
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!

Risposta accettata

Askic V
Askic V il 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 Commento
Miles Hao Peng Su
Miles Hao Peng Su il 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Linear Algebra in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by