user3129463
user3129463

Reputation: 35

Converting rgb-video input to grayscale output

I would like to convert a video file using rgb2gray on the videoframes, thing is i'm not completely sure how.

I have got this script file, playing the video file using a slider:

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

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

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

At line 40 i've added: grayframe = rgb2gray(frames(:,:,:,1));, this makes the first frame gray. How do I make this count for all frames? My goal is to track an object in the videoframe, so I want to convert the frames to binary images aswell, and apply a additional filter like edge detection or something similar.

Thanks in advance

Upvotes: 1

Views: 4621

Answers (3)

josoler
josoler

Reputation: 1423

I'd rather get rid of rgb2gray and vectorize it

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

Upvotes: 1

ashkan
ashkan

Reputation: 476

1- construct a VideoReader object

2- read each frame and convert it to grayscale using RGB2GRAY

3- playback the video

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);

Upvotes: 0

Christopher Creutzig
Christopher Creutzig

Reputation: 8774

for i=1:numImgs
  frames(:,:,:,i)=rgb2gray(frames(:,:,:,i));
end

Upvotes: 2

Related Questions