1

When the webcam captured an image I will to do the background subtraction. Next I will use the image that the background is already subtracted to find the red and green mean. But there is an error. The error says 'Index exceeds matrix dimensions' and error in green = I(:,:,2); . It only shows the red mean. Can someone please help me. Thank you.

vid = videoinput('winvideo', 1, 'MJPG_1280x720');
vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';
triggerconfig(vid, 'manual');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axes2);
preview(vid, hImage);
start(vid); 
pause(6); 
trigger(vid);
pic = getsnapshot(vid);
stoppreview(vid);
pause(2);

image_resize = imresize(pic, [2448 2448]);
I = im2bw(image_resize,graythresh(image_resize));

axes(handles.axes2);
imshow(image_resize);

%Redmean
red = I(:,:,1);
RedMean = mean(mean(red));
set(handles.edit1,'string',RedMean);

%Greenmean
green = I(:,:,2);
GreenMean = mean(mean(green));
set(handles.edit2,'string',GreenMean);
Rotem
  • 30,366
  • 4
  • 32
  • 65
  • 2
    After `I = im2bw(...)` your variable `I` contains the grayscale image. As such it should be a `2448 x 2448` matrix. You then try to access `I(:,:,1)` and `I(:,:,2)` as the red and green channels of the image. This doesn't make sense since `I` is grascale and it fails since `I` is just a (2-D) matrix. Did you mean to write `image_resize(:,:,1)` instead maybe? – Florian Feb 10 '17 at 16:48
  • I'm sorry I don't understand. Could you please explain again? – Krittapat Somsiri Feb 10 '17 at 16:59
  • 2
    Look at the dimensions of `I` after `I = im2bw(...)`. There are only 2 dimensions. There is no `I(:,:,2)` since there is only 1 channel in `I`. As @Florian says, I think you meant to use `image_resize` rather than `I` for the redmean and greenmean. – beaker Feb 10 '17 at 17:20

0 Answers0