%OBSERVE Create a video file, by continuously reading an image from a local
% image file, an image file on the internet, a video device connected to
% your computer or your computer screen.
% IMG_CNT = OBSERVE(IMAGE_URL, INTERVAL_SEC, VIDEO_FILENAME_PATTERN, VIDEO_FPS,
% VIDEO_CODEC_FOURCC, VIDEO_CODEC_QUALITY, LOG_TO_FILE)
% loads the image present at the specified IMAGE_URL every INTERVAL_SEC
% seconds and stores it in an avi file with name VIDEO_FILENAME_PATTERN.
%
% There are three possible ways of capturing:
%
% 1. Capturing from an image file on your computer or the internet:
% If for example IMAGE_URL == 'c:\hello.png' then this image will be
% used as video source for the capturing. Of course this only makes
% sense if the content of the file changes during the capturing process.
% You can also use an image url such as
% 'http://www.heavens-above.com/images/tinylogo.jpg'
% to capture online webcams or weather satellite images.
%
% 2. Capturing from video devices connected directly to your computer:
% If IMAGE_URL has the format 'vid:D,F' where D and F are numbers
% defining a video device nr D and corresponding video format nr F,
% that video input will be used for capturing.
%
% To get a list of available video devices and corresponding video
% formats enter observe('vid?');
%
% 3. Capturing from the screen:
% If IMAGE_URL has the format 'scr:X,Y,W,H' a screen section with the
% specified position and size will be used as video source.
% If no parameters are specified the entire screen serves as input.
%
% OBSERVE returns the # of images that were stored in the avi file.
%
% The resulting avi file will have a framerate of VIDEO_FPS fps.
% VIDEO_CODEC_FOURCC is the four character code of the video codec to be
% used and VIDEO_CODEC_QUALITY is the "quality" (between 0 and 100)
% of the video. If LOG_TO_FILE is true, a log file will be created.
% If VIDEO_CODEC_FOURCC is 'MPG' and the mpgwrite function is available,
% an MPEG1 video file will be created at the end of the observation.
%
% Default values are:
% IMAGE_URL = 'vid:1'
% INTERVAL_SEC = 20
% VIDEO_FILENAME_PATTERN = 'video####.avi'
% VIDEO_FPS = 15
% VIDEO_CODEC_FOURCC = 'IV50'
% VIDEO_CODEC_QUALITY = 100
% LOG_TO_FILE = true
%
% If the parameter VIDEO_FILENAME_PATTERN contains one or more '#'
% characters, a new video file will be created automatically.
% The program ends, by closing the figure window.
%
function img_cnt = observe(image_url, interval_sec, video_filename_pattern, video_fps, video_codec_fourcc, video_codec_quality, log_to_file)
img_cnt = 0;
% codec image size granularity (change to e.g. 8 or 16 if codec complains)
codec_img_size_granularity = 4;
% parameter preprocessing (default value substitution)
if nargin < 7, log_to_file = true; end
if nargin < 6, video_codec_quality = 100; end
if nargin < 5, video_codec_fourcc = 'IV50'; end
if nargin < 4, video_fps = 15; end
if nargin < 3, video_filename_pattern = 'video####.avi'; end
if nargin < 2, interval_sec = 20; end
if nargin < 1, image_url = 'vid:1'; end
% show list of video devices and formats
if beginswith(image_url, 'vid?')
show_video_devices;
return;
end
% get screen input if needed
scr_input = false;
if beginswith(image_url, 'scr')
% get rectangle parameters --> x,y,width,height
rect_params = sscanf(image_url,'scr:%d,%d,%d,%d');
% java stuff... (thx to saurabh kumar for the code)
robo = java.awt.Robot;
toolkit = java.awt.Toolkit.getDefaultToolkit();
% if no or not enough parameters where specified...
if length(rect_params)<2
% use entire screen
rectangle = java.awt.Rectangle(toolkit.getScreenSize());
elseif length(rect_params)<4
% use default width and height if only x and y where specified
rectangle = java.awt.Rectangle(rect_params(1),rect_params(2),256,256);
else
% use specified parameters
rectangle = java.awt.Rectangle(rect_params(1),rect_params(2),rect_params(3),rect_params(4));
end
scr_input = true;
end
% get video input if video device is specified (instead of an image url)
vid_input = [];
if beginswith(image_url, 'vid:')
% get video parameters
vid_params1 = sscanf(image_url,'vid:%d:%d'); % <- just for compability the "old syntax" still works (but considered as deprecated)
vid_params2 = sscanf(image_url,'vid:%d,%d');
if length(vid_params1)>length(vid_params2)
vid_params = vid_params1;
else
vid_params = vid_params2;
end
if length(vid_params)==1
vdevice_nr = vid_params(1);
vformat_nr = 0;
elseif length(vid_params)==2
vdevice_nr = vid_params(1);
vformat_nr = vid_params(2);
else
logit('Invalid video device syntax. Correct is e.g. vid:1,15\r');
return;
end
% get video input
vid_input = get_video_input(vdevice_nr, vformat_nr);
if isempty(vid_input)
logit('Video input could not be opened.\r');
return;
end
set(vid_input, 'FramesPerTrigger', 1);
set(vid_input, 'TriggerRepeat', Inf);
triggerconfig(vid_input, 'manual');
vid_src = getselectedsource(vid_input);
set(vid_src, 'FrameRate', '5');
start(vid_input);
end
% true if MPEG1 video file is to be created
is_mpg = 0;
if strcmpi(video_codec_fourcc, 'mpg')
is_mpg = 1;
video_codec_fourcc = 'IV50';
end
% determine filename and create avi file
if ~endswith(video_filename_pattern, '.avi'), video_filename_pattern = strcat(video_filename_pattern, '.avi'); end
filename = next_filename(video_filename_pattern);
aviobj = avifile(filename, 'compression', video_codec_fourcc, 'fps', video_fps, 'quality', video_codec_quality);
if log_to_file
logfilename = strcat(getfilename_root(filename), '.log');
log_fid = fopen(logfilename, 'w');
logit('Logging to %s\r', logfilename);
end
logit('Video source = %s\r', image_url);
logit('Video filename = %s\r', filename);
% observe...
fig_handle = figure('Name',['Observation of ',image_url,' to ',filename]);
t0 = clock;
interval_cnt = 0;
frame_size = [];
while true
% load image from url
A = get_image;
if isempty(A)
logit('Image could not be loaded. Trying again');
else
% if first image captured...
if isempty(frame_size)
% store height and width of final video
frame_size(1) = size(A,1);
frame_size(2) = size(A,2);
% apply image size granularity and check for zero dimensions
frame_size = round(frame_size / codec_img_size_granularity)*codec_img_size_granularity;
frame_size = frame_size + (frame_size==0);
logit('Final video size is %dx%d.\r', frame_size(2), frame_size(1) );
end
% if image has changed dimensions
if (size(A,1) ~= frame_size(1) || size(A,2) ~= frame_size(2))
A = imresize(A, [frame_size(1), frame_size(2)], 'bicubic');
end
end
there_was_problem = false;
while isempty(A)
there_was_problem = true;
logit('.');
pause(2.0);
没有合适的资源?快使用搜索试试~ 我知道了~
matlab Webcam 支持数据包.zip
共6个文件
m:2个
rar:2个
mht:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 34 浏览量
2021-12-26
12:55:28
上传
评论
收藏 220KB ZIP 举报
温馨提示
1.版本:matlab2019a,内含运行结果,不会运行可私信 2.领域:基础教程 3.内容:matlab Webcam 支持数据包.zip 4.适合人群:本科,硕士等教研学习使用
资源推荐
资源详情
资源评论
收起资源包目录
matlab Webcam Support Package.zip (6个子文件)
matlab Webcam Support Package
18061192ImageAcquisition
observe.rar 4KB
VCAPG2
vcapg2.dll 144KB
VCAPG2.mht 160KB
VCAPG2.rar 101KB
observe
observe.m 18KB
shishizhuizong.m 4KB
共 6 条
- 1
资源评论
- xiaohuihui0000002023-09-30实在是宝藏资源、宝藏分享者!感谢大佬~
天天Matlab科研工作室
- 粉丝: 3w+
- 资源: 7261
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功