Import color video as grayscale?
3 views (last 30 days)
Show older comments
Hi,
I am using the image processing toolbox and wonder if there is a smarter way to import color video as grayscale rather than looping over the color frames with the RGB2GRAY function (see below).
colorVidFrames = read(videoObj, [startFrame endFrame]);
for f = startFrame:endFrame
J = rgb2gray(colorVidFrames(:,:,:,f)); % CONVERT COLOR TO GRAY
gFrames(:,f) = J(:); % GRAY FRAMES
end
clear colorVidFrames
While the RGB2GRAY loop method does provide the desired end result, it is quite computationally expensive for my application. I understand that the computational rigor could be alleviated via parallel computing, but I'm really looking to avoid the conversion loop altogether if possible.
Thanks, -Jeff
1 Comment
DGM
on 19 Jul 2024
Edited: DGM
on 19 Jul 2024
It's likely that the video file is somehow luma-chroma encoded. That is, the workflow is YCbCr -> RGB -> Y, when all conversions could possibly be avoided by just extracting Y directly.
That's an assumption in itself, but it reveals the compromise we're making. We can choose to use high-level tools to read the file as converted RGB data, but those conveniences don't give us direct access to the file contents in their native format.
Even 12 years layer, I don't know of a convenient, format-agnostic way to just grab Y while avoiding redundant conversions and allowing the selection of arbitrary frame ranges. To be fair, I'm not remotely familiar in every video format. Note that the 'grayscale' videoformat property is a purely descriptive read-only property. We can't use it to change the output of read().
This isn't at all an answer, but perhaps someone with a simply-formatted file could put together some low-cost code to extract luma content. The question is whether it's worth the time and effort to spare the CPU time for what is likely a single project.
Answers (1)
Sourabh
on 9 Jun 2025
Hey @Jeff
I see that you are using a “for” loop to perform the conversion operation which is computationally expensive.
There is a more efficient way to convert a video to grayscale in MATLAB. Instead of iterating frame by frame, you can vectorize the operation using matrix manipulation and perform the conversion for all frames in one shot. Kindly refer the code snippet given below:
colorVidFrames = read(videoObj, [startFrame endFrame]); % Read video frames
numFrames = size(colorVidFrames, 4);
% Convert to grayscale using vectorized luminance formula
% MATLAB's rgb2gray uses: 0.2989*R + 0.5870*G + 0.1140*B
grayFrames = 0.2989 * colorVidFrames(:,:,1,:) + ...
0.5870 * colorVidFrames(:,:,2,:) + ...
0.1140 * colorVidFrames(:,:,3,:);
% Optionally reshape into 2D columns if needed (like your original gFrames)
gFrames = reshape(grayFrames, [], numFrames);
clear colorVidFrames % Clean up
For more information on Vectorization, kindly refer the following MATLAB documentation:
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!