Convert color video to grayscale video in MATLAB

前端 未结 3 937
时光说笑
时光说笑 2021-01-14 01:50

I am trying to do some operations on color video in MATLAB however, I am facing 2 problems:

  • I am getting an error while converting color video to grayscale

相关标签:
3条回答
  • 2021-01-14 02:29

    Your first problem is due to trying to assign the 2D output of rgb2gray into a 3D array. You can fix this by converting the gray image back to RGB format:

      frames(:,:,:,i)=repmat(rgb2gray(frames(:,:,:,i)),[1 1 3]);
    
    0 讨论(0)
  • 2021-01-14 02:35
    %% convert a RGB video to a grayscale one.
    videoFReader = vision.VideoFileReader('xylophone.mpg');
    videoFWriter = vision.VideoFileWriter('xylophone_gray.avi',...
       'FrameRate',videoFReader.info.VideoFrameRate);
    while ~isDone(videoFReader)
       videoFrame = step(videoFReader);
       step(videoFWriter, rgb2gray(videoFrame));
    end
    release(videoFReader);
    release(videoFWriter);
    
    0 讨论(0)
  • 2021-01-14 02:48

    Try it this way. This should do the trick. The code is self-explanatory.

     vid = VideoReader('xylophone.mpg');
     numImgs = get(vid, 'NumberOfFrames');
     frames = read(vid);
     obj=VideoWriter('somefile.avi');
     open(obj);
    
     for i=1:numImgs
         movie(i).cdata=rgb2gray(frames(:,:,:,i));
         movie(i).colormap=gray;
     end
    
     writeVideo(obj,movie);
     close(obj);
    
    0 讨论(0)
提交回复
热议问题