Index in position 2 exceeds array bounds (must not exceed 1)
3 次查看(过去 30 天)
显示 更早的评论
Can anyone help me with index in position 2 exceeds array bounds (must not exceed 1). in line 39. My project is offside detection in football but i don't even know what offside exactly is i don't know is my code gonna work properly :( im stuck
clc
close all
clear all
img=imread('offside.jpg');
img=imcrop(img,[5 2 725 584]);
imtool('offside.jpg')
figure(1), imshow(img);
title('defender');
img_h=rgb2hsv(img);
h=img_h(:,:,1);
s=img_h(:,:,2);
v=img_h(:,:,3);
level=graythresh(img);
[r c]=size(h);
for i=1:r
for j=1:c
if (h(i,j)>=0.01 && h(i,j)<=0.99)
out(i,j)=255;
else
out(i,j)=0;
end
end
end
t=bwareaopen(out,300);
se=strel('disk',0);
bw=imclose(t,se);
[B,L]= bwboundaries(bw,'noholes'),disp(B);
hold on
for k = 1:length(B)
boundary=B(k);
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth',2)
end
fprintf('Nesneler isaretlenmistir. Toplam nesne sayisi=%d\n', length(B))
stats=regionprops(L,'Area','Centroid');
figure(2), imshow(img);
[x,y]=find(imcomplement(bw)==0);
img1=imread('offside.jpg');
img1=imcrop(img1,[5 2 745 594]);
figure(3), imshow(img1);title('attacker');
img1_h1=rgb2hsv(img1);
h1=img1_h1(:,:,1);
s1=img1_h1(:,:,2);
v1=img1_h1(:,:,3);
level1=graythresh(img1);
[r c]=size(h1);
for i=1:r
for j=1:c
if s1(i,j)>=0.17 && s1(i,j)<=0.23
out1(i,j)=255;
else
out1(i,j)=0;
end
end
end
%figure, imshow(out)
t1=bwareaopen(out1,200)
%figure, imshow(t)
se=strel('disk',0);
bw1=imclose(t1,se);
[B,L]=bwboundaries(bw1,'noholes'),disp(B);
hold on
for k=1:length(B)
boundary1=B(k);
plot(boundary1(:,2), boundary1(:,1), 'w', 'LineWidtyh',2)
end
fprintf('nesneler isaretlenmistir. Toplam nesne sayisi=%d\n',length(B));
stats=regionprops(L,'Area','Centroid');
%for k=1:length(B) % nesneleri sayan loopun baslangici
[x1,y1]=find(imcomplement(bw1)==0)
d=min(x)+357;
d1=min(x1)+9;
a=(d-d1);
if(a>0)
for j=d
for i=105:405
img(i,j)=255;
end
end
figure,imshow(img);title('offside');
else
for j=d1
for i=1:20
img(i,j)=255;
end
end
figure,imshow(img);title('not offside');
end
1 个评论
KSSV
2021-12-28
We cannot rectify error unless the required input is given.
Any ways check this variable boundary, it seems it don't have two columns.
回答(2 个)
yanqi liu
2021-12-29
yes,sir,may be upload 'offside.jpg' to debug
may be check the 39 line,is it two columns matrix
1 个评论
Walter Roberson
2021-12-29
The first output of bwboundaries is a cell array.
boundary1=B(k);
applied to a cell array is going to result in a scalar cell -- which is not going to have two columns.
At that point you need the data inside the cell array, B{k}
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!