Captain4725
Captain4725

Reputation: 65

Convert video into greyscale in matlab

I'm trying to convert a video file into greyscale. When I try running the Matlab script I get a "Invalid video data-must be a numeric or logical data type". Can someone help me with what I am doing wrong? I am also new to matlab.

filename = 'Project1.m4v'; 
vid = VideoReader(filename); 
newVid = VideoWriter('NewVid');

open(newVid);
numFrames = vid.NumberOfFrames;
for frame = 1 : numFrames
    % Extract the frame from the movie structure.
    thisFrame = read(vid, frame); 
    %Convert each frame to black and white
    gray = rgb2gray(thisFrame); 
    writeVideo(newVid,gray); 

end

close(newVid); 

implay(newVid); 

Upvotes: 1

Views: 1056

Answers (1)

Rotem
Rotem

Reputation: 32094

Use implay('NewVid.avi') instead of implay(newVid);

The only problem in your code is the last line: implay(newVid);.
newVid is a VideoWriter object - you create it using newVid = VideoWriter('NewVid');.

I recommend you to add '.avi' file extension to 'NewVid' file name:
Use: newVid = VideoWriter('NewVid.avi');

implay does't take VideoWriter object as input parameter.
Instead of displaying an error message in Matlab workspace, it displays the error message in the video window.

All you need to do, is replacing last code line with implay('NewVid.avi').

enter image description here

Upvotes: 2

Related Questions