Color to depth mapping Using Kin2 toolbox

2 次查看(过去 30 天)
Hello
I am using Kin2 Toolbox for Matlab for the Kinect V2 sensor. There is a code for mapping the color to depth that I am manipulating. I want to determine an red object's centroid and get the depth of that object at that pixel so that I get the distance from the sensor to the object. Below is the code. However the code takes only one frame and keeps on repeating those coordinates after every five seconds and doesn't update. What could I be doing wrong? Other frame acquiring funcrions like getsnapshot, snapshot or step are giving errors saying becuase its uint8 or struct. Anyone who might help, please help.
addpath('Mex');
clear
close all
redThresh = 0.25;
k2 = Kin2('color','depth');
% images sizes
d_width = 512; d_height = 424; outOfRange = 4000;
c_width = 1920; c_height = 1080;
% Color image is to big, let's scale it down
COL_SCALE = 0.5;
% Create matrices for the images
depth = zeros(d_height,d_width,'uint16');
color = zeros(c_height*COL_SCALE,c_width*COL_SCALE,3,'uint8');
% Images used to draw the markers
depthAdditions = zeros(d_height,d_width,3,'uint8');
colorAdditions = zeros(c_height*COL_SCALE, c_width*COL_SCALE,3,'uint8');
% depth stream figure
d.h = figure;
d.ax = axes('units','pixels');
d.im = imshow(depth,[0 255]);
title('Depth Source')
% color stream figure
c.h = figure;
c.im = imshow(color,[]);
title('Color Source');
while true
% Get frames from Kinect and save them on underlying buffer
validData = k2.updateData;
% Before processing the data, we need to make sure that a valid
% frame was acquired.
if validData
% Copy data to Matlab matrices
depth = k2.getDepth;
color = k2.getColor;
% update depth figure
depth8u = uint8(depth*(255/outOfRange));
depth8uc3 = repmat(depth8u,[1 1 3]);
set(d.im,'CData',depth8uc3 + depthAdditions);
% update color figure
color = imresize(color,COL_SCALE);
%drawnow
set(c.im,'CData',color + colorAdditions);
end
nFrame = 0
while (nFrame<300)
red = color(:,:,1);
diffFrame = imsubtract(color(:,:,1), rgb2gray(color));
diffFrame = medfilt2(diffFrame, [3 3]);
binFrame = imbinarize(diffFrame, redThresh);
binFrame = bwareaopen(binFrame,300);
bw = bwlabel(binFrame, 8);
stats = regionprops((bw), 'BoundingBox', 'Centroid');
centroids = cat(1, stats.Centroid);
imshow(color)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X = bc(1); Y = bc(2);
x = X;
y = Y;
disp('Input Color Coordinates')
disp([x y]);
depthCoords = k2.mapColorPoints2Depth([x/COL_SCALE y/COL_SCALE]);
disp('Output depth coordinates')
disp(depthCoords);
depthAdditions = insertMarker(depthAdditions,depthCoords,'Color','green');
end
pause(5)
end
nFrame = nFrame+1;
end
k2.delete;
close all

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by