Image acquisition, frame rgb2gray conversion

4 views (last 30 days)
I am using image acquisition toolbox to capture a frame from the video output of my wireless usb camera. I get an error when i perform rgb2gray conversion for the captured frame.
clear ;
close all;
vid = videoinput('winvideo', 1, 'YUY2_720x480');
vid.Timeout = 20;
set(vid, 'FrameGrabInterval', 2);
set(vid, 'FramesPerTrigger', 50);
src = getselectedsource(vid);
set(src, 'FrameRate', '30');
vid.ReturnedColorspace = 'rgb';
%frame = getsnapshot(vid);
frames = getdata(vid);
clear vid
imshow( frames(:, :, :, 1) );
Scolor = frames;
Sgray = rgb2gray(Scolor);
ERROR: RGB2GRAY only accepts a Mx3 matrix for MAP or a MxNx3 input for RGB.
Please help me to correct the code, also i want to know what is the function used to save the frame in .jpg format, in the current working directory.Thank you.

Accepted Answer

David Tarkowski
David Tarkowski on 14 Jun 2011
The rgb2gray function only works on a single image, but you are sending it an array of images.
What is the format that you ultimately need the data in? It looks like your camera is returning YUV data, but you're converting it to RGB data and then attempting to convert it to monochrome data. If you're converting to RGB data just to display one frame in the imshow command, I would leave the data as YUV, i.e. don't set the ReturnedColorSpaceProperty, and convert the frame that you want to display to RGB:
imshow( ycbcr2rgb( frames(:,:,:,1))).
The first band of the YUV data is the luminance band, so you should be able to use that directly as your monochrome data:
Sgray = frames(:,:,1,:);
The command to write a frame as jpeg (as well as many other formats) is imwrite.
  1 Comment
Ashish on 14 Jun 2011
Thanks your solution solved my problem. As you specified, I used " Sgray = frames(:,:,1,:); " for obtaining gray scale image.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!