title: Capture image of a detected face using webcam?
body: <p>I am new to MATLAB. I need to capture image and save it into a folder. THis is my matlab code for detect face. </p>
  
  <pre><code> % 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 &amp;&amp; frameCount &lt; 400
  
  %% while(x&lt;1)
      % Get the next frame.
      videoFrame = snapshot(cam);
      videoFrameGray = rgb2gray(videoFrame);
      frameCount = frameCount + 1;
  
      if numPts &lt; 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 &gt;= 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);
  </code></pre>
  
  <p>Please help me to capture image and save.</p>
  
  <p>I tried this code for capture image and save</p>
  
  <pre><code>vid = videoinput('dcam',1,'RGB24_640x480');
  preview(vid);
  start(vid); 
  im=getdata(vid); 
  figure,imshow(im); 
  write(im,'test1image.jpg');
  </code></pre>
  
  <p>When I try this code it gives and error,</p>
  
  <p>Error using videoinput (line 233)
  There are no devices installed for the specified ADAPTORNAME. See IMAQHWINFO.</p>
  
  <p>Error in takeimage (line 1)
  vid = videoinput('dcam',1,'RGB24_640x480');</p>