I want to record video from webcam - matlab

I am using Matlab code for that.
But when I run it, It shows errors.
Code :
fig=figure;
set(fig,'DoubleBuffer','on');
set(gca,'xlim',[-80 80],'ylim',[-80 80],'NextPlot','replace','Visible','off');
mov = avifile('movie.avi','compression','cinepak');
mov.quality=90;
i=imread('white.jpg');
for k=1:1000
imshow(i);
F = getframe(gca);
mov = addframe(mov,F);
end
mov = close(mov);
Errors :
??? Error using ==> capturescreen
Figure destroyed during getframe
Error in ==> getframe at 35
x=capturescreen(varargin{:});
Error in ==> a at 9
F = getframe(gca);
Please help me with this.
Thank you.

Works fine here, although I had to change compression to 'None' since RLE and MSVC won't won't work with the truecolor image I was using and indeo/cinepak wouldn't work with x64. (Not either version of indeo, oddly)
My guess was that you needed a drawnow after your imshow command to clear the buffer so you have an image to copy. I produced a different error the first time I ran it, which seemed to confirm that, but I couldn't reproduce that one. It definitely works with the drawnow; in.
Update: For anyone running win7 x64, you should be able to use 'i420' as your codec. Using that the above code works fine for me either with drawnow in after imshow or not.

Related

MATLAB R2017a VideoWriter error: invalid properties

I have recently upgraded from MATLAB R2016a to R2017a (also changed from Windows 7, now using Windows 10). Trying to run my existing code (nothing changed) using VideoWriter to create a video object, I'm getting the following error, when I get to the stage of writing the video:
Error using VideoWriter/writeVideo (line 369)
One or more properties of the video is invalid. Ensure 'Quality' and 'FrameRate' property of the object and the frame dimensions are within allowed range.
This is my code to save the video:
v = VideoWriter('vidOutput','MPEG-4');
v.FrameRate = 240;
open(v);
for i = 1:size(data,2)
... code that plots an image frame...
F = getframe;
pause(0.000001)
writeVideo(v,F.cdata);
end
The issue seems to be with the FrameRate property. When it's set to more than 150, the error happens. Anything lower than 150 runs ok. Important to note, that when running on MATLAB R2016a, it did work with FrameRate = 240.
Any idea what might have gone wrong?

Issue with VideoWriter and writeVideo in Matlab

I'm trying to create a movie in Matlab where I run through my code several times, and the output of each runthrough is added to the video. However, I keep getting a strange error when I actually try to add the frame to the movie. Here's a snippet of my code:
writerobj = VideoWriter('RHI_P.avi');
writerobj.FrameRate = 60;
open(writerobj);
\\ Runs through code and generates a pcolor figure
figure(14);
\\ Plotting script goes here....
fig = figure;
F = getframe(fig);
writerobj = writeVideo(writerobj,F);
>> Error using VideoWriter/writeVideo: Too many output arguments.
However, if I look at 'help writeVideo', it says this: "writeVideo(OBJ,FRAME) writes a FRAME to the video file associated with OBJ."
From my point of view (though I could be wrong!), I'm following the example given. I've been stuck on this for a couple days now. Does anyone have any advice?
Also, I've been using getframe(gcf) as a workaround for now. However, I can't keep doing that, because my code takes several days to run, and gcf captures the screensaver once that kicks on.
writeVideo does not have an output argument which is what the error states.
You just need to say
writeVideo(writerObj, F);
Hope this helps.

analogRead function not working in MATLAB

I am new to Arduino and I am trying to control it through MATLAB. I have downloaded the arduino IO package and installed it.I am using MATLAB 2014a.I have a LED and I change its brightness by changing the PWM voltage as follows :
a=arduino('COM3');
brightness_step = (5-0)/20;
for i = 1:20
writePWMVoltage(a, 11, i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
for i = 1:20
writePWMVoltage(a, 11, 5-i*brightness_step);
val=a.analogRead(0);
display(val)
pause(0.1);
end
clear a
Everything works great except for the analogRead part which throws the following error :
No appropriate method, property, or field analogRead for class arduino.
What am I doing wrong?
Maybe you can try to use fscanf() like in the following example from AllAboutEE.
I had the same problem with the analogRead when I connected my ArduinoUno R3.
After an hour's struggle, I changed to readVoltage, as Controller replied in 2014, and it works perfectly.

face detection for matlab

i searched face detection for matlab to my project.
i found one:
http://people.kyb.tuebingen.mpg.de/kienzle/fdlib/fdlib.htm
i downloaded the source code, but it didn't work, i got that error from matlab:
??? Undefined function or method 'fdmex' for input arguments of type
'uint8'.
Error in ==> tinytest at 10 s = fdmex(x', threshold);
the main script is:
x = imread('geeks.jpg');
% decision threshold.
% change this to a smaller value, if too many false detections occur.
% change it to a larger value, if faces are not recognized.
% a reasonable range is -10 ... 10.
threshold = 0;
imagesc(x); hold on; colormap gray;
s = fdmex(x', threshold);
for i=1:size(s,1)
h = rectangle('Position',[s(i,1)-s(i,3)/2,s(i,2)-s(i,3)/2,s(i,3),s(i,3)], ...
'EdgeColor', [1,0,0], 'linewidth', 2);
end
axis equal;
axis off
can you find the error?
Usually when I see a uint8 error and a grayscale image, its a red flag to me that I need to do
colorImg=imread('imageName.jpg')
% Even if the image is grayscale, if its png or jpg,
% it will load in as a color image almost exclusively
img=rgb2gray(colorImg)
If you look at the img output, you will notice now its of type double instead of uint8 :)
If that doesn't work, hopefully macduffs will, mine just seems easier if that actually does fix it. :)
Depending on your version of matlab, it looks like the fdlib, comes with a .dll, rename it to .mexw32 or whatever your host machine desires. You can determine this by running:
>> mexext
mexw32
on the Matlab command prompt. Use the mex extension and rename the fdmex.dll to fdmex.mexw32, or whatever mexext returns and it should run flawlessly.
If I run in on my Windows XP machine, I get that beautiful picure:
However, if you do not have a 32 bit machine, the author of the software writes on the link in the question:
Please note that all builds were optimized for Intel Pentium CPUs. If
you would like to run it on a different platform, or have any other
questions, please let me know.
He has a link to his profile and email, so I recommend contacting him for a 64 bit version of the executable.
If you have a recent version of Matlab with the Computer Vision System Toolbox installed, you can use vision.CascadeObjectDetector system object to detect faces in images.

How can I get a waitbar to work in Matlab?

I want to have a waitbar for an operation that takes quite a while. Here is my code:
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/waitbarcounter)
Atemp = At+i*step;
handle = #(M) 1/M^2*((2/(gamma+1))*(1+(gamma-1)*M^2/2))^((gamma+1)/(gamma-1))-(Atemp/At)^2;
Mach = fzero(handle, 5);
Aplot(i) = Atemp/At;
Tplot(i) = Tc / (1+(gamma-1)*Mach^2/2);
Mplot(i) = Mach;
plot(Aplot, Tplot)
end
close(h)
The error Matlab gives is:
??? Error using ==> waitbar at 249
Improper arguments for waitbar
After investigation, I am sure that this error must occur because of the sorrounding code in the loop.
Note: The loop works fine without the waitbar.
Running
counterend = 10000;
>> h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h);
Works as expected on 2007a / Windows XP.
On a side note, it would help knowing what countered is defined as. Something quick to check would be to ensure that you are not passing it a CELL.
Running
counterend = {10000};
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h);
Yields a different error (see below) in 2007a, but this error message may have changed in 2008.
??? Undefined function or method
'_colonobj' for input arguments of
type 'cell'.
My last bit of advice would be caution you on the use of waitbar for large arrays/data sets. While I think it is important to inform the user of the progress, to me there is also a concern for how much time is added to the loop. Working with arrays that have 100k+ entries, I became a religious user of the Profiler to see where the time was really being spent. I can tell you the time is not in the calculation of the i/X, it was all in updating the waitbar's display. To soften the blow of the update/drawnow, I only updated the waitbar every 100 to 1000 entry which helped tremendously.
EDIT: Updated response to match latest code
I first started to attack this problem at the anonymous function, having problems with them in the past it's a personal vendetta of mine. When looking into the function I found that you are using gamma, do you have this defined as a constant (constant to the loop / function)? The reason I ask is that 'gamma' is a Matlab function and was giving me errors when trying to run your function by itself. Below I have modified you code slightly and this does run fine here. If any of the assumptions I have made are wrong please let me know.
In addition, if you did intend to use the gamma function, your function is missing any arguments to it. Hope this helps!
clc
h = waitbar(0,'Please wait...');
counterend = 1000;
waitbarcounter = counterend;
g_amma = 7;
At = 34;
step = 2;
Tc = 42;
for i=1:counterend
waitbar(i/waitbarcounter)
Atemp = At+i*step;
handle = #(M) 1/M^2*((2/(g_amma+1))*(1+(g_amma-1)*M^2/2))^((g_amma+1)/(g_amma-1))-(Atemp/At)^2;
Mach = fzero(handle, 5);
Aplot(i) = Atemp/At;
Tplot(i) = Tc / (1+(g_amma-1)*Mach^2/2);
Mplot(i) = Mach;
plot(Aplot, Tplot)
end
close(h)
Regards,
Adam
I have checked waitbar on R2008b. So far, the only ways I was able to reproduce your error was by having i/counterend evaluate to an array with multiple rows (a 1x2 vector gives interesting results), and by closing the waitbar before calling waitbar(i/counterend).
I do not get any error running the following:
h = waitbar(0,'Please wait...');
counterend = 1000;
for i=1:counterend
waitbar(i/counterend)
end
close(h)
Could you make sure that the small example above runs without error? If yes, please check that the waitbar is not closed during the execution of the loop, and that counterend is a scalar (use dbstop if error to stop execution of your code at the time of the error).
If the above example does not work without error, you should use which waitbar to check that you are using Matlab's waitbar, and not any of the many updated version from the Matlab File Exchange.
Running
counterned=1000;
h = waitbar(0,'Please wait...');
for i=1:counterend
waitbar(i/counterend)
end
close(h)
works perfectly as expected on MATLAB R2009a on Windows XP.
The above runs fine on R2008a on XP also.
However, you get the error you describe if you kill the waitbar window before the next waitbar command comes around. If you want to be nice about it you should check if the handle h is still valid before issuing waitbar.
I prefer to use progressbar written by Steve Hoelzer on the MATLAB FEX. I haven't had any problems with it.
Your suppose to use the handle that you created with your first line of code when you want to update the waiter,
Waiter(it/itmax,h,'progress')