Webcam preview won't open

2 次查看(过去 30 天)
hdzito
hdzito 2023-12-5
评论: hdzito 2023-12-6
So, im trying to preview the video from my webcam, but the window with the video won't open. Any suggestions? i'm using this code and so far not getting any explicit errors on the console.
Also, when i do "imaqhwinfo" it shows up:
InstalledAdaptors: {'winvideo'}
Detail: the webcam on-off indication led, turns on when i run the program.
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480','ReturnedColorSpace', 'rgb')
n=0
while (n<300)
frame = vidDevice();
colormap(hsv);
frame(80:80,:,:)=1;
imagesc(frame(:,:,2));
pause(0.2);
n=n+1;
end
release(vidDevice);

回答(1 个)

Shreshth
Shreshth 2023-12-6
Hello Hdzito,
It appears that you are using MATLAB with the Image Acquisition Toolbox to access your webcam and preview the video. Your code snippet has a few issues that might be preventing the video from displaying correctly. Let's go through them step by step:
  1. Missing Figure and Axes: You need to create a figure and axes to display the video frames. If you don't create a figure, MATLAB might not know where to display the images.
  2. Incorrect Use of Colormap: You are using colormap(hsv) inside the loop, which is not necessary for displaying RGB images and might be causing issues with the display.
  3. Incorrect Frame Manipulation: You are trying to modify the frame with frame(80:80,:,:)=1;. This line is attempting to set a single row of the frame to 1, which is not a valid operation. If you want to modify a part of the frame, you need to specify a range for both rows and columns.
  4. Displaying Only One Channel: The line imagesc(frame(:,:,2)); is displaying only the second color channel (green) of the frame. If you want to display the full color image, you should just use imshow(frame);
Here is a revised version of your code:
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', 'ReturnedColorSpace', 'rgb');
% Create a figure window to display the frames
hFig = figure;
% Loop to continually capture frames from the webcam
n = 0;
while (n < 300) && ishandle(hFig) % Check if the figure window is still open
frame = step(vidDevice); % Acquire one frame
% If you want to modify the frame, do it here
% Example: Draw a red line across the frame
frame(80,:,1) = 255; % Red channel
frame(80,:,2) = 0; % Green channel
frame(80,:,3) = 0; % Blue channel
% Display the frame
imshow(frame, 'Parent', axes); % Display the RGB image
pause(0.2); % Pause for 0.2 seconds
n = n + 1;
end
% Clean up
release(vidDevice);
close(hFig); % Close the figure window
Make sure to run this code in your MATLAB environment. If your webcam's LED indicator turns on, it means the webcam is being accessed. If the video still does not display, there may be additional issues not covered by the code, such as driver problems or conflicts with other software accessing the camera.
Remember that if you modify the frame's content (as in the example where a red line is drawn), the modifications should be within the valid range of the frame dimensions.
Thank you,
Shubham Shreshth
  1 个评论
hdzito
hdzito 2023-12-6
Hello, Shreshth
The figure window opens but it doesnt show up the webcam video.
When i stopped the program i got this error on console:
Error using VideoDeviceInternal/step
GETSNAPSHOT interrupted with ^C (Control-C) before frame was available.
Error in imaq.VideoDevice/stepImpl
Error in video (line 9)
frame = step(vidDevice); % Acquire one frame - Show complete stack trace
-------------
if i do this sequence of instructions with my webcam name:
they open up a figure with my webcam video so, im assuming its not an hardware issue.

请先登录,再进行评论。

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by