How can I segment depth image?

I have a depth image data obtained from kinect sensor in form of 424x512 unit 16 which contains an object. I want to segment only the object from the depth image. I have tried using pcfitplane using the coressponding point cloud data of the depth image but I was able to segment one plane. Could anyone please suggest me a way to segment only the object(a box) from the depth image?

 Accepted Answer

Try this:
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
fprintf('Beginning to run %s.m ...\n', mfilename);
rgbImage = imread('color_image.png');
subplot(2, 2, 1);
imshow(rgbImage);
axis('on', 'image');
title('Original RGB Image');
s = load('meta_data.mat')
imgDepth = s.imgDepth;
subplot(2, 2, 2);
imshow(imgDepth, []);
impixelinfo
title('Depth Image');
subplot(2, 2, 3);
histogram(imgDepth);
grid on;
title('Histogram of Depth Image');
binaryImage = imgDepth > 1025 & imgDepth < 1050;
% Fill holes
binaryImage = imfill(binaryImage, 'holes');
% Take largest blob.
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 4);
imshow(binaryImage, []);
impixelinfo
title('Binary Image');
fprintf('Done running %s.m ...\n', mfilename);

6 Comments

Thanks a ton. It works fine.
Could you please help me with this line:
binaryImage = imgDepth > 1025 & imgDepth < 1050;
Is that the depth distance mentioned in cm?
The numbers are whatever units your depth image has. Maybe they're arbitrary/relative (gray levels) or maybe they're cm or inches or whatever. I don't know the units of a Kinect depth map.
They are mostly in cm since the image was taken approximately at 1000cm from the camera. I see that you have so much expertise with image processing domain. Could you please help me with another issue? I want to measure the dimensions of the obtain object as shown previously. I am able to manually check the data points of the corners from the plot and measure their distance. But, is there any way to code them to so that it finds the data points of the corners automatically from the plot or binary image and estimate their distance between them.
Thanks in advance.
Perhaps. What is unique about the data points you click on. I'm sure there are hundred of points in the image that could be considered as "a corner" according to some definition. How are you deciding upon only the 4 ones that you picked? What's unique about those that made you pick them over all the other possible locations?
Say, for instance I want to measure the length and breadth of the object that I mentioned. To do so, choosing the points on the edges/sides would help me to measure length or breadth. Since, the box is rectangular so, I thought if I could find furthest two points(i.e. corners) on the edges of the same side lying in the same vertcial/x-axis, I could find the length and similary other two points to find breadth. In other words, measuring the boundaries. Am I right or this way is not possible to actually measure length and breadth of the object? If I'm wrong please correct me.
You can either find the bounding box or find the centroid and the boundary and the distance of the centroid to all the boundary pixels, then use findpeaks() to find the 4 corners. I'm attaching shape recognition demos.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!