【问题标题】:Converting rgb-video input to grayscale output将 rgb 视频输入转换为灰度输出
【发布时间】:2014-12-08 23:21:51
【问题描述】:

我想在视频帧上使用 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));,这会使第一帧变为灰色。如何计算所有帧的数量?我的目标是跟踪视频帧中的对象,所以我也想将帧转换为二进制图像,并应用一个额外的过滤器,如边缘检测或类似的东西。

提前致谢

【问题讨论】:

    标签: matlab


    【解决方案1】:
    for i=1:numImgs
      frames(:,:,:,i)=rgb2gray(frames(:,:,:,i));
    end
    

    【讨论】:

      【解决方案2】:

      我宁愿摆脱 rgb2gray 并将其矢量化

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-11
          • 2020-04-29
          • 1970-01-01
          • 2010-10-15
          • 2013-09-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多