【问题标题】:Hand detection code not working while playing video?播放视频时手部检测代码不起作用?
【发布时间】:2014-02-16 12:13:50
【问题描述】:

我正在研究手部检测和跟踪。 问题是我已经开发了用于检测图像中的手的代码(如下所示)及其作品(如图所示),但问题是当我使用录制的视频运行相同的代码时,没有检测到甚至跟踪到手(如图所示)。

视频的代码是

filename = 'hand_2.wmv';
video = vision.VideoFileReader(filename);
player = vision.DeployableVideoPlayer('Location', [10,100],'FrameRate',60);

while ~isDone(video)
    img_orig = step(video);

    % Capture the dimensions
    height = size(img_orig,1);
    width = size(img_orig,2);

    %Initialize the output images
    out = img_orig;
    bin = zeros(height,width);

    %Convert the image from RGB to YCbCr
    img_uint8 = uint8(img_orig);
    img_ycbcr = rgb2ycbcr(img_uint8);
    Cb = img_ycbcr(:,:,2);
    Cr = img_ycbcr(:,:,3);

    %Detect Skin
    [r,c,v] = find(Cb>=67 & Cb<=137 & Cr>=133 & Cr<=173);
    numind = size(r,1);

    %Mark Skin Pixels
    for i=1:numind
        out(r(i),c(i),:) = [0 0 255];
        bin(r(i),c(i)) = 1;
    end

    % Detect radius
    [x1,y1] = find(out(:,:,3) == 255, 1,'first');
    [x2,y2] = find(out(:,:,3) == 255, 1, 'last');
    if isempty(y1) && isempty(y2)
        y1 = 2; y2 = 0;
    end

    % Detect Hand Center
    hits = 0;
    hitsArr = zeros(1,height);
    for i = 1:height
        hits = numel(find(bin(i,:) == 1));
        hitsArr(i) = hits;
    end
    maxHitr = max(hitsArr);
    y =  find(hitsArr == maxHitr,1,'first');

    hitsArr = zeros(1,width);
    for i = 1:width
        hits = numel(find(bin(:,i) == 1));
        hitsArr(i) = hits;
    end
    maxHitc = max(hitsArr);
    x = find(hitsArr == maxHitc,1,'first');

    label = 'Hand';
    position = [x y abs(y2-y1)/2; x y 1];
    img_out = insertObjectAnnotation(img_orig,'Circle',position,label);
    step(player, img_out);

end

release(video);
release(player);

检测单个图像的代码是:

%Read the image, and capture the dimensions
tic;
img_orig = imread('65.png');
height = size(img_orig,1);
width = size(img_orig,2);

%Initialize the output images
out = img_orig;
bin = zeros(height,width);

%Convert the image from RGB to YCbCr
img_ycbcr = rgb2ycbcr(img_orig);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);


%Detect Skin
[r,c,v] = find(Cb>=67 & Cb<=137 & Cr>=133 & Cr<=173);
numind = size(r,1);
numcol = size(r,2);

%Mark Skin Pixels
for i=1:numind
    out(r(i),c(i),:) = [0 0 255];
    bin(r(i),c(i)) = 1;
end

% Detect radius
[x1,y1] = find(out(:,:,3) == 255, 1,'first');
[x2,y2] = find(out(:,:,3) == 255, 1, 'last');

% Detect Hand Center
hits = 0;
hitsArr = zeros(1,height);
for i = 1:height
    hits = numel(find(bin(i,:) == 1));
    hitsArr(i) = hits;
end
maxHitr = max(hitsArr);
y =  find(hitsArr == maxHitr,1,'first');

hitsArr = zeros(1,width);
for i = 1:width
    hits = numel(find(bin(:,i) == 1));
    hitsArr(i) = hits;
end
maxHitc = max(hitsArr);
x = find(hitsArr == maxHitc,1,'first');

label = 'Hand';
position = [x y abs(y2-y1)/2; x y 1];
img_out = insertObjectAnnotation(img_orig,'Circle',position,label);
imshow(img_orig);
figure; imshow(img_out);title('Detected hand');
imwrite(img_out,'hand_detect.jpg');
% viscircles([x y],abs(y2-y1)/2,'EdgeColor','r');
% viscircles([x y],1,'EdgeColor','r');
% figure; imshow(out);
figure; imshow(bin);
toc;

我首先认为这是因为视频的帧速率,但更改它也无济于事。 如上所述,当我运行视频进行检测和跟踪代码失败时。 任何帮助将不胜感激。

【问题讨论】:

    标签: matlab video image-processing matlab-cvst


    【解决方案1】:

    发生这种情况是因为当您从 png 文件中读取图像时,您会得到一个像素值介于 0 和 255 之间的 uint8 图像。另一方面,vision.VideoFileReader 会为您提供一个带有像素的 single 类图像值标准化为 0 和 1 之间。您可以通过将“VideoOutputDataType”设置为“uint8”来解决此问题:

    video = vision.VideoFileReader(filename, 'VideoOutputDataType', 'uint8');
    

    在不同的主题上,如果您想跟踪手牌,您可能想尝试使用vision.HistogramBasedTrackervision.PointTracker

    【讨论】:

    • vision.HistogramBasedTracker 或 vision.PointTracker 怎么样?它们如何帮助我进行手部检测?
    • 它们不会帮助检测,但它们可能有助于跟踪。
    猜你喜欢
    • 2016-06-08
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2017-02-16
    相关资源
    最近更新 更多