Use a USB camera to detect colors
18 次查看(过去 30 天)
显示 更早的评论
Hello everyone! I have code that only recognizes the color white, and it works through the computer's camera. How can I change it to work with a USB camera?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Program Name : white Object Detection and Tracking %
% Author : Arindam Bose %
% Version : 1.05 %
% Description : How to detect and track white objects in Live Video %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initialization
thresh = 0.75; % Threshold for white detection
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
vidInfo = imaqhwinfo(vidDevice); % Acquire input video property
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 400, ...
'MaximumCount', 50);
hshapeinsWhiteBox = vision.ShapeInserter('BorderColor', 'Custom', ...
'CustomBorderColor', [1 0 0]); % Set white box handling
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [100 100 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]);
nFrame = 0; % Frame number initialization
%% Processing Loop
while(nFrame < 300)
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying
bwredFrame = im2bw(rgbFrame(:,:,1), thresh); % obtain the white component from red layer
bwgreenFrame = im2bw(rgbFrame(:,:,2), thresh); % obtain the white component from green layer
bwblueFrame = im2bw(rgbFrame(:,:,3), thresh); % obtain the white component from blue layer
binFrame = bwredFrame & bwgreenFrame & bwblueFrame; % get the common region
binFrame = medfilt2(binFrame, [3 3]); % Filter out the noise by using median filter
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs
rgbFrame(1:15,1:215,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsWhiteBox, rgbFrame, bbox); % Instert the white box
for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids
end
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
end
0 个评论
回答(2 个)
Pravarthana P
2022-2-15
Hi Almog haviv, it can be understood that you are trying to detect white color through an USB web-camera.
Toolbox required : Image Acquisition toolbox
After connecting the USB web camera, you can find the following properties using the functions:
Once, after identifying the respective web camera use webcam([%corresponding web camera number]) to know about the web camera feature.
Replace “vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480')” with
vidDevice=videoinput(‘adapter name’,’device ID’,’format’);
By this you can use the created object vidDevice to detect any live streaming video input through USB webcamera.
Links you may find helpful:
0 个评论
Image Analyst
2022-2-15
You need the Image Acquisition Toolbox, or you can probably get by with the simpler webcam utility. See the links @Pravarthana P gave.
To detect colors other than white, see my attached demo (that reads from a video file, not a live camera) that tracks a green Sharpie marker.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!