Exceed array bound at position 2

1 次查看(过去 30 天)
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!

采纳的回答

Askic V
Askic V 2022-11-14
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 个评论
Miles Hao Peng Su
Miles Hao Peng Su 2022-11-14
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by