Image acquisition, frame rgb2gray conversion

2 次查看(过去 30 天)
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.
CODE:
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';
preview(vid);
start(vid);
pause(2);
%frame = getsnapshot(vid);
frames = getdata(vid);
delete(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.

采纳的回答

David Tarkowski
David Tarkowski 2011-6-14
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 个评论
Ashish
Ashish 2011-6-14
Thanks your solution solved my problem. As you specified, I used " Sgray = frames(:,:,1,:); " for obtaining gray scale image.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by