Guys I need someone to help me with this code , I would like to use webcamn to see the convex hull for hand detection , I'm stuck with this for a really long time

3 visualizzazioni (ultimi 30 giorni)
I have done a colour detection using colour thresholding in matlab , basically rgb to hsv and creating the function below
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 30-Mar-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.080;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
After all I have this for convex hull
clc
clear
close all
warning off
detect=vision.CascadeObjectDetector;
c=webcam;
while true
RGB=snapshot(c);
bboxes =step(detect,RGB);
[rows, colu, channels2]=size(RGB);
[BW,maskedRGBImage] = createMask(RGB);
S=imcrop(RGB,bboxes);
[mks, gks, channels]=size(S);
V=zeros(mks,gks);
BW(bboxes(2):bboxes(2)+bboxes(4),bboxes(1):bboxes(1)+bboxes(3),:)=V;
hand=imfill(BW,'holes');
R=bwareaopen(bwareafilt(hand,2),2500);
CH_objects = bwconvhull(R,'objects');
g=bwperim(CH_objects);
er=imdilate(g,strel('disk',5));
for ij=1:rows
for jk=1:colu
if(er(ij,jk)==1)
RGB(ij,jk,2)=255;
RGB(ij,jk,1)=0;
RGB(ij,jk,3)=0;
end
end
end
imshow(RGB);
drawnow;
end
I get this error for imcrop.
Error using imcrop
Expected input number 2, RECT, to be a vector.
Error in imcrop>validateRectangle (line 493)
validateattributes(rect,{'numeric'},{'real','vector'}, ...
Error in imcrop>parseInputs (line 299)
validateRectangle(spatial_rect,2);
Error in imcrop (line 99)
[x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
Error in convexhull (line 12)
S=imcrop(RGB,bboxes);
If someone will be more than happy to help with this I will really appreciate I'm been stuck with this for a long time I even read about the imcrop in mathwork and could't find a solution

Risposta accettata

Walter Roberson
Walter Roberson il 30 Mar 2021
bboxes =step(detect,RGB);
That is going to return an N x 4 array, where N is the number of objects detected.
Expected input number 2, RECT, to be a vector.
Your code is detecting more than one object (face)
It is not uncommon for the object detector to pick up objects that a vaguely face-like.
I would suggest that you examine the bboxes returned and discard ones that are significantly smaller than the others.
However...
The function you are using to detect hands... you are using it in a mode configured to detect faces. And if you look at the built-in shapes it can detect, https://www.mathworks.com/help/vision/ref/vision.cascadeobjectdetector-system-object.html#d123e86039 you will see that hands are not listed. It is possible to train a cascade object detector to detect hands, but you would need to have done that and your call to the cascade object detector in this code would have to refer to the file of information about how to detect the objects you trained on.
  3 Commenti
Walter Roberson
Walter Roberson il 10 Dic 2022
You have to examine bboxes returned from the step() call. If the number of rows is greater than 1, then you need to figure out which of the detected boxes are relevant to you.
In some situations, you can know what the range of sizes is for actual hands, so by examine the width and height you might be able to eliminate some possibilities.
But sometimes there are just multiple inputs that might match. For example someone might be wearing a T-shirt which includes an image of a hand, or there might be a poster or painting that includes a hand... or someone might be holding something with two hands. So sometimes what you need to do is track objects over multiple frames: an unmoving object is less likely to be of interest to you.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by