2013-12-23 86 views
1

我想在視頻幀上使用rgb2gray轉換視頻文件,事情是我不完全知道如何。將rgb-video輸入轉換爲灰度輸出

我有這個腳本文件,使用滑塊播放視頻文件:

%-------------------------- -----------------------------------------

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));,這會使第一幀灰色。我如何計算所有幀?我的目標是跟蹤視頻幀中的一個對象,所以我想將幀轉換爲二進制圖像,並應用像邊緣檢測或其他類似的附加濾鏡。

預先感謝

回答

2
for i=1:numImgs 
    frames(:,:,:,i)=rgb2gray(frames(:,:,:,i)); 
end 
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); 
爲灰度
1

我寧願擺脫rgb2gray並將其向量化

GRAYframes = uint8(mean(RGBframes,3));