clear all
close all
clc

%% Retirar a informação do hardware
% Adaptor name
imaqhwinfo;

% Device ID
imaqhwinfo('kinect');

% Determinar o formato de vídeo
imaqhwinfo('kinect',2);

%% Criar video input
% Vizualizar video input summary
vid_DEPTH = videoinput('kinect',2,'Depth_640x480');

% Depth cam general settings
src = get(vid_DEPTH);

% Visualizar video da Depth cam
preview(vid_DEPTH);

% Depth cam demora 16seg até a camara ficar "nitida"
pause(16)

%% Adquirir frame DEPTH
img_DEPTH = getsnapshot(vid_DEPTH);
% figure(2)
% imshow(img_DEPTH, [])

%% 


% imag=round(img_DEPTH/256)+rem(img_DEPTH,256);
% 
% imshow(imag)



[filledImage,zeroPixels] = Kinect_DepthNormalization(vid_DEPTH);
figure;
subplot(1,2,1)
imagesc(img_DEPTH);
% subplot(1,2,2)
% imshow(filledImage);

figure
subplot(1,3,1)
imagesc(img_DEPTH);
subplot(1,3,2)
imagesc(filledImage);
subplot(1,3,3)
imshow(zeroPixels)

figure(4);
imshowpair(img_DEPTH,filledImage,'montage');

filledImage = double(filledImage)/65535;
channel1Min = 59.000;
channel1Max = 113.000;

% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 255.000;

% Define thresholds for channel 3 based on histogram settings
channel3Min = 191.000;
channel3Max = 255.000;

% sliderBW =( filledImage(:,:,1) >= channel1Min ) & (filledImage(:,:,1) <= channel1Max) & ...
%     (filledImage(:,:,2) >= channel2Min ) & (filledImage(:,:,2) <= channel2Max) & ...
%     (filledImageI(:,:,3) >= channel3Min ) & (filledImage(:,:,3) <= channel3Max);

BW = sliderBW;



% Set background pixels where BW is false to zero.
img_DEPTH(repmat(~BW,[1 1 3])) = 0;
imshow(img_DEPTH)