我想在videoframes上使用rgb2gray转换视频文件,事情是我不完全确定如何。
我有这个脚本文件,使用滑块播放视频文件:
%---------------------------------------------- ---------------------
function frametracking()
%# read all frames at once
filename = uigetfile('*.avi');
vid = VideoReader(filename);
numImgs = get(vid, 'NumberOfFrames');
frames = read(vid);
% Make the UI
mx = numImgs-1;
hFig = figure('Menubar','none');
uicontrol('Style','slider', 'Parent',hFig, ...
'Callback',@slider_callback, ...
'Units','pixels', 'Position',[150 0 260 20], ...
'Value',1, 'Min',1, 'Max',mx, 'SliderStep',[1 10]./mx);
pB1 = uicontrol(hFig, 'Position',[150 20 130 20], ...
'Units','pixels', ...
'String','Select file', ...
'Callback',@button1_callback);
pB2 = uicontrol(hFig, 'Position',[280 20 130 20], ...
'Units','pixels', ...
'String','Calibrate', ...
'Callback',@button2_callback);
eT1 = uicontrol(hFig, 'Style','edit',...
'Units','pixels',...
'Position',[490 400 60 20],...
'CallBack',@edit1_callback,...
'String','');
eT2 = uicontrol(hFig, 'Style','edit',...
'Units','pixels',...
'Position',[490 370 60 20],...
'CallBack',@edit2_callback,...
'String','');
eT3 = uicontrol(hFig, 'Style','edit',...
'Units','pixels',...
'Position',[490 370 60 20],...
'CallBack',@edit3_callback,...
'String','');
hAx = axes('Parent',hFig,'units','pixels',...
'Position',[80 80 400 400]);
grayframe = rgb2gray(frames(:,:,:,1));
hMainImg = imshow(grayframe(:,:,:,1), 'Parent',hAx);
%# callback functions
function slider_callback(src,~)
val = round(get(src,'Value')); %# starting index
%# update the thumbnails
for ii = 1 : numel(hMainImg)
set(hMainImg(ii), 'CData',frames(:,:,:,ii+val-1))
drawnow
end
end
function click_callback(src,~)
%# update the main image
% grayframe = rgb2gray(frames(:,:,:,1));
set(hMainImg, 'CData',get(src,'CData'));
drawnow
end
function button1_callback(src,~)
end
end
%---------------------------------------------- ---------------------
在第40行,我添加了:grayframe = rgb2gray(frames(:,:,:,1));,这使第一帧变为灰色。如何对所有帧进行计数?我的目标是跟踪视频帧中的对象,因此我想将帧转换为二进制图像,并应用额外的过滤器,如边缘检测或类似的东西。
提前致谢
答案 0 :(得分:2)
for i=1:numImgs
frames(:,:,:,i)=rgb2gray(frames(:,:,:,i));
end
答案 1 :(得分:1)
我宁愿摆脱rgb2gray并将其矢量化
GRAYframes = uint8(mean(RGBframes,3));
答案 2 :(得分:0)
1-构建一个VideoReader对象
2-读取每个帧并使用RGB2GRAY将其转换为灰度
3-播放视频
clear
clc
obj = VideoReader('xylophone.mp4');
nFrames = obj.NumberOfFrames;
vidHeight = obj.Height;
vidWidth = obj.Width;
mov(1:nFrames) =struct('cdata',zeros(vidHeight,vidWidth,1,'uint8'),...
'colormap',[]);
% Read one frame at a time.
for k = 1 : nFrames
mov(k).cdata =rgb2gray( read(obj,k));
end
implay(mov);