1

I am trying to run a program that opens a webcam, takes a screenshot, processes it, and shows the output. My code runs correctly and I am getting output, but when I close the output window I get this error every-time:

Matlab System Error: Matlab has encountered an internal problem and needs to close.

As I am new to Matlab can anyone help me? I am using Windows 8 operating system and Matlab R2013a.

This is the code:

    clear all;
    close all;
    clc;
    video=videoinput('winvideo',1);
    preview(video);
    while(true)
    data=getsnapshot(video);
    R=data(:,:,1);
    G=data(:,:,2);
    B=data(:,:,3);
    for i=1:768
        for j=1:1024
           if(R(i,j)<128)
               out(i,j)=1;
           else
               out(i,j)=0;
           end
       end
   end
   cla; % Prevent stuffing too many images into the axes.
   imshow(out);
   drawnow;
   end
horchler
  • 18,384
  • 4
  • 37
  • 73
Devendra
  • 83
  • 2
  • 5
  • 2
    There is no need for nested `for`-loop to calculate `out`: `out = double( R < 128 );` that's it. – Shai Feb 19 '14 at 06:39
  • 1
    Another thing: it's best [not to use `i` and `j` as variable names in Matlab](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab). – Shai Feb 19 '14 at 06:39
  • 2
    @Shai Meh. `i` and `j` as loop indexes have been ubiquitous since (essentially) the dawn of time. The better solution is to use either `1i` or `1j` to denote the imaginary unit--no possibility of ambiguity there! – Nicu Stiurca Feb 19 '14 at 07:03
  • @Shai i and j are used to check value of every pixel and accordingly assigning value to that pixel in output image. And doing out=double(R<128) will not show image it will just show a dot. – Devendra Feb 19 '14 at 07:11
  • 1
    +1 for pointing out to use 1i and 1j. Also `R < 128` returns a logical of the same size of R (768x1024) where every element in `R` larger than 128 smaller than 128 is true. Double converts it to a double. Exactly the same as you do, but much faster and shorter. About the error: does it occur every time or did it just happen once? – patrik Feb 19 '14 at 08:31
  • If this is all Matlab code (no 3rd-party toolboxes involved), which it seems to be, I'd submit this as a bug report to matlab. Concerning `i` and `j`: there're hundreds of internal matlab functions using `i` and `j` - so it can't be that bad. It might help to post the crash-report - which is usually included in the "matlab needs to close"-dialog. – sebastian Feb 19 '14 at 09:08
  • @patrik I am getting this error every-time I run the program. – Devendra Feb 19 '14 at 12:12
  • @sebastian Thanks.. This is all code I have written.. – Devendra Feb 19 '14 at 12:12
  • @Devendra sure, this code was written by you. What I meant was, whether you're using any 3rd-party toolbox. If you're only using matlab-functions, than a crash like the one you describe should not happen and is worth a ticket/bug report imho. – sebastian Feb 19 '14 at 12:42
  • @Devendra I tend to agree with sebastian, it seems to be a matlab bug. However it would be interesting to get all the information to see if this is a general problem or if it occurs for some operative systems/computers only. – patrik Feb 19 '14 at 12:44
  • 1
    @sebastian No. I am not using any 3rd party toolbox.I am using just MATLAB. – Devendra Feb 19 '14 at 14:18

1 Answers1

0

Here's some simpler code that replicates the error on Windows or on Mac for me (R2013b, built-in FaceTime HD camera):

clear all;
close all;
% video = videoinput('macvideo',1);
video = videoinput('winvideo',1);
while true
    data = getsnapshot(video);
    cla;
    imshow(data);
    drawnow;
end

Run the above and close the the window after it draws the image and you may be able to get it to crash. The strange thing is that after I reliably made it crash a few times it stopped doing it.

What is going on?

The fact that the error went away randomly for me makes me suspect some sort of race condition. You code isn't particularly correct, but Matlab shouldn't be crashing hard like this, so it should be reported as a bug.

How can you fix this?

The problem is that you're closing a window that is being drawn to inside of an infinite while loop. You need to break the while loop when the figure is closed. And you might also want to perform some cleanup such as deleting the video object. Here's some nice fast code that shouldn't produce an error :

clear all;
close all;
clc;
if ispc
    video = videoinput('winvideo',1);
elseif ismac
    video = videoinput('macvideo',1);
else
    video = videoinput(imaq.VideoDevice);
end
% preview(video);

% Create figure and get handle to image data
data = getsnapshot(video);
R = data(:,:,1);
out = double(R < 128);
h = imshow(out);

while true
    data = getsnapshot(video);
    R = data(:,:,1);
    out = double(R < 128);
    if ishghandle(h)        % Only if figure still open
        set(h,'CData',out); % Replace image data
    else
        break;
    end
end
delete(video); % Clean up
horchler
  • 18,384
  • 4
  • 37
  • 73