-2

Error: In an assignment A(I) = B, the number of elements in B and I must be the same.

Error in ==> test at 22 Centroid(i)=k(i).Centroid;

test.m

    I=imread('1_1.jpg');
    I=rgb2gray(I);
    I2 = Thresholding(I);
    cc = bwconncomp(I2,8);
    n = cc.NumObjects;
    Area = zeros(n,1);
    Centroid = zeros(n,1);
    Perimeter = zeros(n,1);
    MajorAxis = zeros(n,1);
    MinorAxis = zeros(n,1);
    k = regionprops(cc, 'Area','Centroid','Perimeter','MajorAxisLength', 'MinorAxisLength');
    for i=1:n
    Area(i) = k(i).Area;
    Centroid(i)=k(i).Centroid;
    Perimeter(i) = k(i).Perimeter;
    MajorAxis(i) = k(i).MajorAxisLength(i);
    MinorAxis(i) = k(i).MinorAxisLength(i);
    end
    handdata(1,1) = mean(Area);
    handdata(2,1) = mean(Centroid);
    handdata(3,1) = mean(Perimeter);
    handdata(4,1) = mean(MajorAxis);
    handdata(5,1) = mean(MinorAxis);

Thresholding.m

function im = Thresholding(I);
[r,c] = size(I);
im = zeros(r,c);
for i=1:r
    for j=1:c
        if I(i,j)>105
            im(i,j)=1;
        end
    end
end
im = bwareaopen(im,5);
im = imfill(im, 'holes');
end
Saroj Shrestha
  • 2,696
  • 4
  • 21
  • 45

1 Answers1

0

Solution

The Centroid field is a vector of size 1x2 (for holding the y and x coordinates). Therefore, you'll need to modify your code accordingly:

Outside the for loop:

Centroid = zeros(n,2); %The centroids array should be nx2, to contain both x and y positions

Inside the for loop:

Centroid(i,:)=k(i).Centroid; %fill the corresponded i'th row
MajorAxis(i) = k(i).MajorAxisLength; %remove the coordinate from MajorAxisLength and MinorAxisLength fields
MinorAxis(i) = k(i).MinorAxisLength;

From that reason, you'll also need to modify the following line of code, since mean(Centroid) is a vector of size 1x2. handdata will be a vector of 4x1, and the centroid mean will be placed in a different variable, called centroidData.

handdata(1,1) = mean(Area);
handdata(2,1) = mean(Perimeter);
handdata(3,1) = mean(MajorAxis);
handdata(4,1) = mean(MinorAxis);
centroidData = mean(Centroid);

Two more suggestions

1.In the Thresholding function, instead of using double for loop you can simply write

im = I > 105;

2.in the main for loop (located in the main script) use ii instead of i as a variable name for your counter.

Community
  • 1
  • 1
ibezito
  • 5,782
  • 2
  • 22
  • 46