Convert color video to black and white video in MATLAB

I am trying to perform some color video operations in MATLAB, however I have to face two problems:

  • I get an error when converting color video to black and white video. I mean, I need to convert the color video to black and white video and write it back to the .avi file

  • How can I perform some operation (say, defining borders) on grayscale frames (extracted from a color video), and then record the edge detection result in .avi format?

My incomplete code (which consists of color format conversion) looks like this:

vid = VideoReader('Big_buck_bunny_480p_Cut.avi');
numImgs = get(vid, 'NumberOfFrames');
frames = read(vid);
for i=1:numImgs
  frames(:,:,:,i)=rgb2gray(frames(:,:,:,i));
end

Any pointer to fix these two issues?

+4
3

2D- rgb2gray 3D-. , RGB:

  frames(:,:,:,i)=repmat(rgb2gray(frames(:,:,:,i)),[1 1 3]);
+4
%% 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);
+3

Try it like this. That 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);
+1
source

Source: https://habr.com/ru/post/1531007/


All Articles