【问题标题】:Capture image of a detected face using webcam?使用网络摄像头捕获检测到的面部图像?
【发布时间】:2016-07-04 18:07:19
【问题描述】:

我是 MATLAB 新手。我需要捕获图像并将其保存到文件夹中。这是我用于检测人脸的 matlab 代码。

 % Create the face detector object.
faceDetector = vision.CascadeObjectDetector();

% Create the point tracker object.
pointTracker = vision.PointTracker('MaxBidirectionalError', 2);

% Create the webcam object.
cam = webcam();

% Capture one frame to get its size.
videoFrame = snapshot(cam);
frameSize = size(videoFrame);

% Create the video player object.
videoPlayer = vision.VideoPlayer('Position', [100 100 [frameSize(2), frameSize(1)]+30]);

runLoop = true;
numPts = 0;
frameCount = 0;
%%x = 0;  
while runLoop && frameCount < 400

%% while(x<1)
    % Get the next frame.
    videoFrame = snapshot(cam);
    videoFrameGray = rgb2gray(videoFrame);
    frameCount = frameCount + 1;

    if numPts < 10
        % Detection mode.
        bbox = faceDetector.step(videoFrameGray);

        if ~isempty(bbox)
            % Find corner points inside the detected region.
            points = detectMinEigenFeatures(videoFrameGray, 'ROI', bbox(1, :));

            % Re-initialize the point tracker.
            xyPoints = points.Location;
            numPts = size(xyPoints,1);
            release(pointTracker);
            initialize(pointTracker, xyPoints, videoFrameGray);

            % Save a copy of the points.
            oldPoints = xyPoints;

            % the orientation of the face.
            bboxPoints = bbox2points(bbox(1, :));

            % Convert the box corners into the [x1 y1 x2 y2 x3 y3 x4 y4]
            % format required by insertShape.
            bboxPolygon = reshape(bboxPoints', 1, []);

            % Display a bounding box around the detected face.
            videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);

            % Display detected corners.
            videoFrame = insertMarker(videoFrame, xyPoints, '+', 'Color', 'white');
        end

    else
        % Tracking mode.
        [xyPoints, isFound] = step(pointTracker, videoFrameGray);
        visiblePoints = xyPoints(isFound, :);
        oldInliers = oldPoints(isFound, :);

        numPts = size(visiblePoints, 1);

        if numPts >= 10
            % Estimate the geometric transformation between the old points
            % and the new points.
            [xform, oldInliers, visiblePoints] = estimateGeometricTransform(...
                oldInliers, visiblePoints, 'similarity', 'MaxDistance', 4);

            % Apply the transformation to the bounding box.
            bboxPoints = transformPointsForward(xform, bboxPoints);

            % Convert the box corners into the [x1 y1 x2 y2 x3 y3 x4 y4]
            % format required by insertShape.
            bboxPolygon = reshape(bboxPoints', 1, []);

            % Display a bounding box around the face being tracked.
            videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);

            % Display tracked points.
            videoFrame = insertMarker(videoFrame, visiblePoints, '+', 'Color', 'white');

            % Reset the points.
            oldPoints = visiblePoints;
            setPoints(pointTracker, oldPoints);
        end

    end

    % Display the annotated video frame using the video player object.
    step(videoPlayer, videoFrame);

    % Check whether the video player window has been closed.
    runLoop = isOpen(videoPlayer);
end

% Clean up.
clear cam;
release(videoPlayer);
release(pointTracker);
release(faceDetector);

请帮我抓拍并保存。

我尝试使用此代码捕获图像并保存

vid = videoinput('dcam',1,'RGB24_640x480');
preview(vid);
start(vid); 
im=getdata(vid); 
figure,imshow(im); 
write(im,'test1image.jpg');

当我尝试这段代码时,它给出了错误,

使用视频输入时出错(第 233 行) 没有为指定的 ADAPTORNAME 安装任何设备。请参阅 IMAQHWINFO。

拍摄图像错误(第 1 行) vid = videoinput('dcam',1,'RGB24_640x480');

【问题讨论】:

  • 问题很明显:MATLAB无法检测到任何名为dcam的相机
  • 我也用过'winvideo'。它也给出错误。 imaqhwinfo ans = InstalledAdaptors: {'dcam'} MATLABVersion: '8.6 (R2015b)' ToolboxName: 'Image Acquisition Toolbox' ToolboxVersion: '4.10 (R2015b)'
  • 当你做 videoinput('dcam') 时会发生什么?我无法重现您的错误。另外,尝试imaqhwinfo('dcam') 看看Device ID 是什么,有时它不是1。
  • 当我使用“dcam”作为视频输入时,它显示以下错误,使用视频输入时出错(第 233 行)没有为指定的 ADAPTORNAME 安装设备。参见 IMAQHWINFO。拍摄图像错误(第 1 行) vid = videoinput('dcam',1,'RGB24_640x480');这是设备 ID; >> info=imaqhwinfo('dcam') info = AdaptorDllName: 'C:\MATLAB\SupportPackages\R2015b\dcamhardware\toolbox\imaq\supportpackages\dcam\adaptor\win64...' AdaptorDllVersion: '4.10 (R2015b)' AdaptorName: ' dcam' DeviceIDs: {1x0 cell} DeviceInfo: [1x0 struct]

标签: matlab image-processing matlab-cvst


【解决方案1】:

您可能需要为特定相机的图像采集工具箱下载support package。如果您使用的是普通 USB 网络摄像头,那么您可能需要“OS Generic Video Interface”支持包。

【讨论】:

  • 会不会是你没有DCAM相机?您可以尝试安装“OS Generic Video Interface”支持包吗?
  • 感谢您的支持。现在我安装了“OS Generic Video Interface”。我的代码正在运行。
猜你喜欢
  • 1970-01-01
  • 2013-02-09
  • 2010-11-23
  • 2012-04-12
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多