Exceed array bound at position 2
    4 views (last 30 days)
  
       Show older comments
    
    Miles Hao Peng Su
 on 14 Nov 2022
  
    
    
    
    
    Commented: Miles Hao Peng Su
 on 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!
0 Comments
Accepted Answer
  Askic V
      
 on 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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
