Unable to use a value of type cell as an index

7 次查看(过去 30 天)
so I'm trying to detect a circle on a plate but i always got an error at line 18
Unable to use a value of type cell as an index.
Error in xxxx (line 18)
for k = 1:length(B)
addpath 'xxxxxx'
RGB = imread('xxx.jpg');
I = imadjust(RGB, [0.2; 0.3], [0.2; 0.3])
imtool(I)
imshow(I)
I = rgb2gray(I);
bw = imbinarize(I);
imshow(bw)
bw = bwareaopen(bw,30);
se = strel('disk',2);
bw = imclose(bw,se);
imshow(bw)
[B,L,N] = bwboundaries(bw);
figure; imshow(bw); hold on;
for k = 1:length(B)
boundary = B{k};
if(k > N)
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2);
elseif(k < N)
plot(boundary(:,2), boundary(:,1), 'b', 'LineWidth', 2);
else
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);
end
end
stats = regionprops (L, 'Perimeter')
for k = 1:length(B)
if (k <= N)
boundary = B{k}
perimeter = stats(k).Perimeter
if perimeter < 1000
length = perimeter/4.05
lenght_real = 30
c = lenght_real/length
metric_string = sprintf('%2.2f', length_real)
text(boundary(1,2)+20,boundary(1,1)+50,meteric_string,'Color','g','FontSize',12,'FontWeight','bold')
end
else
boundary = B{k}
perimeter_c = stats(k).Perimeter
length = perimeter/4.05
lenght_real = 30
c = lenght_real/length
if perimeter_c > 120
diameter = perimeter_c/pi
diameter_real = c*diameter
metric_string = sprintf('%2.2f', diameter_real)
text(boundary(1,2)-10,boundary(1,1)-40,meteric_string,'Color','r','FontSize',12,'FontWeight','bold')
else
diamter = perimeter_c/pi
diamter_real = c*diamter
metric_string = sprintf('%2.2f', diameter_real)
text(boundary(1,2)-10,boundary(1,1)-40,meteric_string,'Color','r','FontSize',12,'FontWeight','bold')
end
end
title(['Diameter of middle hole and ',...
'Length if block gauge in millimeters'])
end
  2 个评论
James Tursa
James Tursa 2021-11-1
Please post the entire error message, including the offending line.
Muhammad Hilmi Al Farid Ni'man
so the entire error mesaage is just
Unable to use a value of type cell as an index.
Error in xxxx (line 18)
for k = 1:length(B)

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2021-11-1
Don't use length as a variable name. If your code does, you will not be able to call the length function.
length = perimeter/4.05

yanqi liu
yanqi liu 2021-11-1
addpath 'xxxxxx'
RGB = imread('xxx.jpg');
I = imadjust(RGB, [0.2; 0.3], [0.2; 0.3]);
imtool(I)
imshow(I)
I = rgb2gray(I);
bw = imbinarize(I);
imshow(bw)
bw = bwareaopen(bw,30);
se = strel('disk',2);
bw = imclose(bw,se);
imshow(bw)
[B,L,N] = bwboundaries(bw);
figure; imshow(bw); hold on;
for k = 1:numel(B)
boundary = B{k};
if(k > N)
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 2);
elseif(k < N)
plot(boundary(:,2), boundary(:,1), 'b', 'LineWidth', 2);
else
plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);
end
end
stats = regionprops (L, 'Perimeter');
for k = 1:numel(B)
if (k <= N)
boundary = B{k};
perimeter = stats(k).Perimeter;
if perimeter < 1000
len = perimeter/4.05;
len_real = 30;
c = len_real/len;
metric_string = sprintf('%2.2f', len_real_real);
text(boundary(1,2)+20,boundary(1,1)+50,meteric_string,'Color','g','FontSize',12,'FontWeight','bold');
end
else
boundary = B{k};
perimeter_c = stats(k).Perimeter;
len = perimeter/4.05;
len_real = 30;
c = len_real/len;
if perimeter_c > 120
diameter = perimeter_c/pi;
diameter_real = c*diameter;
metric_string = sprintf('%2.2f', diameter_real);
text(boundary(1,2)-10,boundary(1,1)-40,meteric_string,'Color','r','FontSize',12,'FontWeight','bold');
else
diamter = perimeter_c/pi;
diamter_real = c*diamter;
metric_string = sprintf('%2.2f', diameter_real);
text(boundary(1,2)-10,boundary(1,1)-40,meteric_string,'Color','r','FontSize',12,'FontWeight','bold');
end
end
title(['Diameter of middle hole and ',...
'len_real if block gauge in millimeters'])
end

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by