Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert color video to grayscale video in MATLAB

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 video. I mean I need to convert color video to grayscale video and write it back to .avi file

  • How can I perform some operation (say edge detection) on grayscale frames (extracted from color video) and then can write back the result of edge detection in .avi video format?

My incomplete code (which consist of color format conversion) is as follows:

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 problems?


1 Answers

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]);
like image 69
Trisoloriansunscreen Avatar answered Dec 15 '25 06:12

Trisoloriansunscreen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!