1

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?

Prachi
  • 141
  • 2
  • 2
  • 13
  • Your `A` is of type `uint8` and has values `{0,1}` to display "white" Matlab expects uint8 value of 255. Try `imshow(A*255);` or `imshow(A,[]);` or `imagesc(A);`. – Shai Jul 31 '17 at 06:53
  • It seems like your depth info `A` is stored in "row major" order, as opposed to matlab's "column major". Try read A: `A = fread(I, 480*640, 'uint8=>uint8'); A = reshape(A, 640,480).';` – Shai Jul 31 '17 at 07:39
  • I tried that but still the same. – Prachi Aug 01 '17 at 04:55
  • are you sure data is stored with 8bits per pixel? – Shai Aug 01 '17 at 05:14
  • when the image is read, MATLAB is showing 480*640 uint8. So I suppose, it is 8bits per pixel. – Prachi Aug 01 '17 at 06:38
  • you read it as 8bits per pixel, so this is what Matlab shows. Are you sure this is the right format? – Shai Aug 01 '17 at 06:41
  • How to check what is the right format? – Prachi Aug 01 '17 at 10:12
  • Variable A (which is a .bin file) is showing 480*640 double. The final "mask" image created according to your code is showing 480*640 logical. Is this wrong for display? – Prachi Aug 01 '17 at 10:41
  • Sorry A is 480*640 uint8 only – Prachi Aug 01 '17 at 10:43
  • `A` is uint8 because you read it as such. The question is is the data in the corresponding '.bin' file is also uint8? what is the file size in bytes? do you have any documentation on how to read this file? – Shai Aug 01 '17 at 20:09
  • I checked the read me file of the dataset. The .bin file is "short 16 bit". I haven't checked it before. So I have to use 'int16=>int16' instead of 'uint8=>uint8'. – Prachi Aug 02 '17 at 05:24
  • 1
    Thanks Shai!! Now the picture is coming proper black and white :) Thanks for bearing with me on this silly question. ;D – Prachi Aug 02 '17 at 05:27

1 Answers1

0

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, []);
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thanks Shai!! Now I am getting some output other than the black window. :) But I am getting some weird output now which has some black portion but the white portion is not that white, it is somewhat off white. Can you tell why? I have changed the threshold value several times but never got proper white color. – Prachi Jul 31 '17 at 07:26
  • @Prachi please add screen shots to your question to illustrate the problem. – Shai Jul 31 '17 at 07:31
  • I have attached the output image above. – Prachi Jul 31 '17 at 07:35