Search code examples
matlabkinectimage-segmentationgesture-recognitionimage-thresholding

Hand segmentation using Depth thresholding


I want to segment hand from a depth image using depth thresholding. I used this kinect and leap dataset from this link-

http://lttm.dei.unipd.it/downloads/gesture/

I tried these 2 codes, but the output I got is total black image in both the cases. The original .png image is original depth image

I selected depth value from 1_depth.bin file in the dataset.

Code 1

I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
A = fread(I, 480*640, 'uint8=>uint8');
A = reshape(A, 480, 640);

min_row = min(A);
min_col = min(min_row);

for i = 1:480
    for j = 1:640
        if ((A(i,j) > (min_col + 10)) || (A(i,j) == (min_col + 10)))
           A(i,j) = 1;
       else
           A(i,j) = 0;
        end
    end
end
imshow(A)

Code 2

image = imread('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.png');
I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
A = fread(I, 480*640, 'uint8=>uint8');
A = reshape(A, 480, 640);

min_row = min(A);
min_col = min(min_row);
for i = 1:480
    for j = 1:640
        if ((A(i,j) > (min_col + 10)) || (A(i,j) == (min_col + 10)))
            image(i,j) = 1;
        else
            image(i,j) = 0;
        end
    end
end
imshow(image)

The output I am getting is output image

Kindly tell what is wrong in this code and why I am not getting any out?


Solution

  • Your code is extremely not vectorize. Here's how to re-write your code in a more vectorize fashion. This is both more efficient and more "readable":

    I = fopen('D:\dsktop\kinect_leap_dataset\acquisitions\P1\G1\1_depth.bin', 'r');
    A = fread(I, 480*640, 'uint8=>uint8');
    A = reshape(A, 480, 640);
    
    min_ = min(A(:));  % minimal value across rows and columns
    mask = A>=(min_+10);  % no need for loop, vectorize code.
    imshow(mask, []);