To clean up noise in your depth images, consider using a median filter, which is a straightforward method for noise removal. Check out this guide here: https://www.mathworks.com/discovery/denoising.html. Applying this before binarization will help reduce unwanted noise.
For binarizing your depth images, you can manually set a threshold in "imbinarize" by passing a second numerical argument. Experiment with thresholds to find one that best segments your object. More details on "imbinarize" can be found here: https://www.mathworks.com/help/images/ref/imbinarize.html. Rest assured that this will work consistently if your camera and table setup remain unchanged.
If you want an automated approach to automatically adjust to your setup, I suggest programming the following framework:
- Noise Removal: For every captured depth image, perform noise removal.
- Calibration Step (One-Time): Capture a depth image of your table without any objects on it. Calculate the mean of the smallest 40,000 pixels, which represents about 20% of the 424x512 resolution. This mean value will serve as your "baseEstimate," providing a good approximation of the table's surface. You can tinker with this percentage if it does not work too well for your task.
- Object Segmentation Step: With an object on the table, capture a depth image. Calculate an estimate with a similar approach above for the top of the object, referred to as "topEstimate." Use these estimates to determine a threshold for the "imbinarize" function. A threshold like "baseEstimate + 0.1*(topEstimate-baseEstimate)" should effectively separate the object from the table in the binarized image.
I enjoyed tinkering with depth cameras during my undergraduate, I'd be happy to help in answering any questions you have about the above approach :)