i am taking image as input to the code.But image are not proper.

This is my code
clc;
close all;
clear all;
obj = videoinput('winvideo', 1);
set(obj, 'SelectedSourceName', 'input1');
src_obj = getselectedsource(obj);
get(src_obj);
preview(obj);
frame = getsnapshot(obj);
image(frame);
delete(obj);
Its simple image from videoinput.....But i am attaching picture that matlab shows me....Is there any kind of setting need while using this code....Code is correct at has been taking from library file of videoinput() function.I have changed two camera. Check out attachment of image.

 Accepted Answer

Is it color or black and white? What function are you using to display it, imshow(), image(), or imagesc()?

13 Comments

i am using imshow.....its rgb image
You might need to adjust what video mode you're using. Try running my InitializeCamera routine. Step through it step by step and see what's going on:
%=====================================================================
function InitializeVideoCamera(handles)
% Initialize the video camera.
global vidobj; % Video camera object.
try
% First see if any video mode is selected in advance. If it is, we should use that if we can.
% Now get the selection from the lstVideoModes listbox.
lstVideoModes = get(handles.lstVideoModes, 'String');
lstSelectedItem = 1; % Flag for indicating no video mode is selected.
if ~isempty(lstVideoModes)
lstSelectedItem = get(handles.lstVideoModes, 'Value');
% Let's see what it is, just for fun.
if ~isempty(lstSelectedItem)
selectedVideoMode = lstVideoModes{lstSelectedItem};
end
end
% Print info to command window:
hardwareInfo = imaqhwinfo % Print what cameras are there.
adaptorNames = hardwareInfo.InstalledAdaptors;
% Might look something like:
% InstalledAdaptors: {'dcam' 'gentl' 'gige' 'lumeneraimaqw64' 'matrox' 'winvideo'}
matches = strfind(adaptorNames, 'winvideo');
% Find out which index is the Lumenera Camera.
winvideoIndex = find(~cellfun(@isempty, matches));
thewinvideoAdaptor = adaptorNames{winvideoIndex}
% Print out other useful information to the command window.
hw2 = imaqhwinfo(thewinvideoAdaptor);
devInfo = hw2.DeviceInfo
numberOfImagingDevices = length(devInfo)
logitechModes = '';
for d = 1 : numberOfImagingDevices
thisDevice = devInfo(d); % A structure.
defaultFormat = thisDevice.DefaultFormat; % A character array.
deviceFileSupported = thisDevice.DeviceFileSupported; % A logical.
devName{d} = thisDevice.DeviceName; % A cell.
devID = thisDevice.DeviceID; % A double
videoInputConstructor = thisDevice.VideoInputConstructor; % A character array.
videoDeviceConstructor = thisDevice.VideoDeviceConstructor; % A character array.
SupportedFormats{d} = thisDevice.SupportedFormats; % A cell array
numberOfSupportedFormats(d) = length(SupportedFormats);
% Print out stuff to command window.
fprintf('\nFor device #%d, named %s, the attributes are:\n DefaultFormat = %s\n DeviceFileSupported = %d\n DeviceID = %d\n VideoInputConstructor = %s\n VideoDeviceConstructor = %s\n',...
d, devName{d}, defaultFormat, deviceFileSupported, devID, videoInputConstructor, videoDeviceConstructor);
% Let's start a counter for the video modes if it's the Logitech camera.
if strfind(lower(devName{d}), 'logitech')
modeCounter = 1;
end
% Print out all the supported formats for this device.
fprintf(' For device named : %s, the supported formats are:\n', devName{d});
for k = 1 : numberOfSupportedFormats(d)
theseFormats = SupportedFormats{k}
for k2 = 1 : length(theseFormats)
thisFormat = theseFormats{k2};
fprintf(' Format #%d = %s\n', k2, thisFormat);
% If the device name contains Logitech, and the format contains RGB, let's add the mode to the listbox.
if ~isempty(strfind(lower(devName{d}), 'logitech')) && ~isempty(strfind(thisFormat, 'RGB'))
logitechModes{modeCounter} = thisFormat;
modeCounter = modeCounter + 1;
end
end
end
end
% Add the modes to the listbox:
if ~isempty(logitechModes)
set(handles.lstVideoModes, 'string', logitechModes);
end
% The device name, devName, might be something like : {'Hauppauge WinTV 885 Video Capture' 'Logitech QuickCam Ultra Vision'}
% Find the one that is for our Logitch camera.
matches = strfind(devName, 'Logitech');
% Find out which index is the Logitech Camera.
LogitechIndex = find(~cellfun(@isempty, matches));
% Get the Logitech device:
LogitechCamera = devInfo(LogitechIndex);
% Get the device ID:
LogitechDeviceID = LogitechCamera.DeviceID;
% Now we know which device is the Logitech camera. Now let's let the user pick some RGB modes;
% Initialize winvideo webcam with the first mode on the list.
% Or the selected mode if one was selected in advance of entering this function.
vidobj = videoinput(thewinvideoAdaptor, LogitechDeviceID, logitechModes{lstSelectedItem});
if ~isempty(vidobj)
src = getselectedsource(vidobj);
vidobj.FramesPerTrigger = 1;
axes(handles.axesImage);
hImage = findobj(handles.axesImage, 'Type', 'image');
preview(vidobj, hImage);
% src.ZoomMode = 'manual';
% Turn on the live video preview. Display the bounding box over it if there is one selected.
TurnOnLiveVideo(handles);
end
% Turn on the live video preview. Display the bounding box over it if there is one selected.
TurnOnLiveVideo(handles);
catch ME
errorMessage = sprintf('Error in function logitech_webcam_OpeningFcn.\nNo Logitech webcam detected!\nMake sure you plug in your webcam BEFORE you start MATLAB, NOT AFTER!\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
set(handles.txtInfo, 'string', errorMessage);
uiwait(warndlg(errorMessage));
end
%=====================================================================
% Turn on the live video preview.
function TurnOnLiveVideo(handles)
global vidobj; % Video camera object.
% Bail out if there is no video object class instantiated.
if isempty(vidobj), return, end;
% Switch the current graphic axes to handles.axesImage.
% This is where we want the video to go.
axes(handles.axesImage);
% Reset image magnification. Required if you ever displayed an image
% in the axes that was not the same size as your webcam image.
hold off;
axis auto;
% Get the handle to the image in the axes.
hImage = findobj(handles.axesImage, 'Type', 'image');
% Turn on the live video.
preview(vidobj, hImage);
% Put hold on so that displaying our bounding box doesn't blow away the image.
return; % from TurnOnLiveVideo
code is showing following bug
??? Input argument "handles" is undefined.
Error in ==> InitializeVideoCamera at 98 set(handles.txtInfo, 'string', errorMessage);
You'll have to adapt it a bit since it's a snippet from my code. I used GUIDE so there is references to handles. Just delete any lines where it mentions handles.
after deleting it is showing this error..I am not able to understand
Error in function logitech_webcam_OpeningFcn. No Logitech webcam detected! Make sure you plug in your webcam BEFORE you start MATLAB, NOT AFTER!
Error Message: Input argument "handles" is undefined.
Like I said you have to adapt it. You're not using a Logitech webcam so you need to step through line by line and see where it's checking for that. Then replace Logitech with the name of your camera.
even taking out the argument of handles it is showing this error
Error in function logitech_webcam_OpeningFcn. No Logitech webcam detected! Make sure you plug in your webcam BEFORE you start MATLAB, NOT AFTER!
Error Message: Undefined function or variable "lstVideoModes".
Those were modes I put into the listbox. Maybe you should just run imaqtool and copy the commands you do into your code. You're probably using the wrong mode. Try different modes in imaqtool until you get a good image and then copy those lines into your program.
I checked all the mode.....till no result....can u please help me out
What are the names of the modes you see in imaqtool?
winvideo_1 YUY2_160x120; YUY2_352x288; YUY2_320x240; YUY2_176x144;
Oh bummer. You have one of those cameras that doesn't have an RGB mode. All is not lost though. There is an option of videoinput() that lets you set the returned video mode to convert it into an RGB image. Check out this link by David Tarkowski, the developer of this toolbox at the Mathworks: http://www.mathworks.com/matlabcentral/answers/5634#answer_8178
vid = videoinput('winvideo', 1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img)
oh man..... really thanks a lot.....:).

Sign in to comment.

More Answers (1)

It appears to me that you are getting something that is not in RGB colorspace. Use
imaqfind

3 Comments

Still same kind of image is coming.
imaqfind() does not change the image format, it shows you the available image formats. You then have to pick an appropriate one and add it to the videoinput() call.
use obj.ReturnedColorSpace='rgb'; just after obj = videoinput('winvideo', 1);

Sign in to comment.

Asked:

jay
on 27 Dec 2013

Commented:

on 20 Feb 2014

Community Treasure Hunt

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

Start Hunting!