Convert live colour video into gray scale video?

7 visualizaciones (últimos 30 días)
Koohyar
Koohyar el 6 de Sept. de 2020
Comentada: Image Analyst el 29 de Mayo de 2022
Hi,
I am using MATLAB for image processing.I need to have a gray scale video saved in disk. Please can you help me to manipulate the following script in order to give a gray scale video.I changed the set(vid, 'ReturnedColorspace', 'rgb') to set(vid, 'ReturnedColorspace', 'grayscale'), but it shows error (Error using im2frame
Indexed movie frame must have a non-empty colormap).
imaqreset;
%warning('off','all'); %.... diable warining msg ...;
vid = videoinput('winvideo',1, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb');
% vid.FrameRate =30;
vid.FrameGrabInterval = 1; % distance between captured frames
start(vid)
aviObject = VideoWriter('myVideo.avi'); % Create a new AVI file
open(aviObject);
for iFrame = 1:50 % Capture 100 frames
% ...
% You would capture a single image I from your webcam here
% ...
I=getsnapshot(vid);
%imshow(I);
F = im2frame(I); % Convert I to a movie frame
writeVideo(aviObject,F); % Add the frame to the AVI file
end
close(aviObject); % Close the AVI file
stop(vid);
thanks
  2 comentarios
Image Analyst
Image Analyst el 6 de Sept. de 2020
You forgot to call rgb2gray()
Koohyar
Koohyar el 6 de Sept. de 2020
Many thanks for your comments, please can you show me where should I call rgb2gray()? it goes to each frame? gray = rgb2gray(vid)?

Iniciar sesión para comentar.

Respuesta aceptada

Amrtanshu Raj
Amrtanshu Raj el 9 de Sept. de 2020
Hi,
You can modify your code like this to get the desired results.
CODE :
imaqreset;
%warning('off','all'); %.... diable warining msg ...;
vid = videoinput('winvideo',1, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb');
% vid.FrameRate =30;
vid.FrameGrabInterval = 1; % distance between captured frames
start(vid)
aviObject = VideoWriter('myVideo.avi'); % Create a new AVI file
open(aviObject);
for iFrame = 1:50 % Capture 100 frames
% ...
% You would capture a single image I from your webcam here
% ...
I=getsnapshot(vid);
%changes made here
grayimg = rgb2gray(I); % Convert rgb image to grayscale img
%imshow(I);
F = im2frame(grayimg); % Convert grayimg to a movie frame
writeVideo(aviObject,F); % Add the frame to the AVI file
end
close(aviObject); % Close the AVI file
stop(vid);
  6 comentarios
almog haviv
almog haviv el 29 de Mayo de 2022
Allow an explanation of the code
Image Analyst
Image Analyst el 29 de Mayo de 2022
@almog haviv There are numerous comments all over. What lines are you confused about?

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by