I get this error "index exceeds matrix dimensions" How can I solve it?

4 vues (au cours des 30 derniers jours)
Han
Han le 1 Mar 2018
Commenté : Han le 1 Mar 2018
I have this code and I apply it in image and it was work. Now I'm trying to use it in Live video
Code:
vid = videoinput('winvideo',1, 'MJPG_640x360');
set(vid,'FramesPerTrigger',4);
set(vid,'TriggerRepeat',Inf);
start(vid);
BackgroundImage = getsnapshot(vid);
I1 = rgb2gray(BackgroundImage);
figure;
subplot(2,2,1); imshow(I1); title('background'); pause(2);
while 1
frames = getdata(vid);
I3 = frames(:,:,[1 1 1]);
I2 = rgb2gray(I3);
subplot(2,2,2); imshow(I2); title('frames gray');
Objects = I1 ~= I2; pause(2);
BlobAnalysis = vision.BlobAnalysis('MinimumBlobArea',500,'MaximumBlobArea',230000);
[area,centroid,BoundingBox] = step(BlobAnalysis,Objects);
pause(1);
[row , col ] = size (BoundingBox);
for i=1 : row
x = BoundingBox(i,1);
y =BoundingBox(i,2);
w=BoundingBox(i,3);
h=BoundingBox(i,4);
TestImage = frames(y :(y+h),x:(x+w), :);
r = TestImage(:,:,1);
g = TestImage(:,:,2);
b = TestImage(:,:,3);
subplot(2,2,3); title('Histogram');
histogram2(r,g,'DisplayStyle','tile','ShowEmptyBins','on', ...
'XBinLimits',[0 255],'YBinLimits',[0 255]);
histogram(r,'BinMethod','integers','FaceColor','r','EdgeAlpha',0,'FaceAlpha',1)
hold on
histogram(g,'BinMethod','integers','FaceColor','g','EdgeAlpha',0,'FaceAlpha',0.7)
histogram(b,'BinMethod','integers','FaceColor','b','EdgeAlpha',0,'FaceAlpha',0.7)
xlabel('RGB value')
ylabel('Frequency')
xlim([0 257])
thresholding = 2097150;
rth=graythresh(TestImage(:,:,1))*255
gth=graythresh(TestImage(:,:,2))*255
bth=graythresh(TestImage(:,:,3))*255
if ( rth*gth*bth >= thresholding )
% Ishape = insertShape(object,'rectangle',BoundingBox,'Color', 'green','Linewidth',6);
msgbox('Alarm it is white !');
load gong.mat;
while ( rth*gth*bth >= thresholding)%endless loop
sound(y);
lastTime = clock;
while etime(clock, lastTime) < 5
pause(0.03);
end
break;
end
else
msgbox('ok');
end
clear TestImage;
end
end
error:
Index exceeds matrix dimensions.
Error in TryG (line 35)
TestImage = frames(y :(y+h),x:(x+w), :);

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Mar 2018
The line
TestImage = frames(y :(y+h),x:(x+w), :);
is incorrect. It should be
TestImage = frames(y :(y+h-1),x:(x+w-1), :);

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by