Unable to use a value of type cell as an index
5 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Steven Lord
on 1 Nov 2021
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
0 Comments
yanqi liu
on 1 Nov 2021
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
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!