Image Processing on Live Video

10 vues (au cours des 30 derniers jours)
Enrique Gonzalez
Enrique Gonzalez le 3 Déc 2020
Hi everyone
I created two scripts following the matlab documentation and examples. The script will simply use the camera to detect your eyes using the viola-jones algorithm, provided by the Computer Vision Toolbox. I then use the the function getIris to do some morphological operations, following one of the examples that I found here. It will provide the areas of the expected iris using regionprops etc. I tested it with images before adding the video part. The thing is that it only works for images and the first frame of the video. It then gives me this error:
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
Error in getIris (line 17)
area = stats.Area;
Error in video (line 31)
[areaMax,iMax,stats,centroids] = getIris(I2);
Can anyone give me some insight on what could be wrong or why does it not work from the second frame on? Any hints, recomendations or guidance is welcome. Thank you in advance!
Code:
%--------------------------------------------------------------------------
% Create the eye detector object.
eyeDetector = vision.CascadeObjectDetector('EyePairSmall');
% 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;
frameCount = 0;
%--------------------------------------------------------------------------
while runLoop && frameCount < 400
% Get the next frame.
videoFrame = snapshot(cam);
videoFrameGray = rgb2gray(videoFrame);
frameCount = frameCount + 1;
% Detection mode.
bbox = eyeDetector.step(videoFrameGray);
% Crop image after detecting eye region
s = length(bbox(:,4));
I2 = imcrop(videoFrameGray,bbox(s,:));
[areaMax,iMax,stats,centroids] = getIris(I2);
%-------------------------------------------------------------------------
% Get correct location on original image
stats(iMax).BoundingBox(1) = stats(iMax).BoundingBox(1) + bbox(1);
stats(iMax).BoundingBox(2) = stats(iMax).BoundingBox(2) + bbox(2);
%Get eye center
center=round(stats(iMax).Centroid);
X=center(1) + bbox(1);
Y=center(2) + bbox(2);
if ~isempty(bbox)
% Convert the rectangle represented as [x, y, w, h] into an
% M-by-2 matrix of [x,y] coordinates of the four corners. This
% is needed to be able to transform the bounding box to display
% the orientation of the eyes.
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, []);
% Check for Iris location in between the already detected eye
% Display a bounding box around the detected eyes.
videoFrame = insertShape(videoFrame, 'Polygon', bboxPolygon, 'LineWidth', 3);
% Display detected corners.
videoFrame = insertMarker(videoFrame, [X,Y], '+', 'Color', 'white');
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(eyeDetector);
getIris function:
function [areaMax,iMax,stats,centroids] = getIris(Image)
%GETIRIS Summary of this function goes here
% Identifies the iris by doing morphological operations on the
% image and returns the area and index with respect to the image on
% posible location of the iris
grayImage = Image;
%--------------------------------------------------------------------------
% Turn to binary image to filter the skin
newImage = ~imbinarize(grayImage,0.10);
newImage = bwmorph(newImage,'close');
newImage = bwmorph(newImage,'open');
newImage = bwareaopen(newImage,200);
bwI = imfill(newImage,'holes');
% Morph operations done----------------------------------------------------
% Get areas and properties
stats = regionprops(bwI);
area = stats.Area;
s = regionprops(bwI,'centroid');
centroids = cat(1,s.Centroid);
[areaMax, iMax]=max(area);
end

Réponses (0)

Catégories

En savoir plus sur Tracking and Motion Estimation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by