From: ImageAnalyst on
Kenneth:
Then you either have the wrong threshold or you have an object that is
going completely around the perimeter of your image. If that's not
the case then only the objects themselves will be white, not the
entire image. Try this code and see if it works. You will not get
the whole image white with this code and this image.

If you still have problems, upload one of your images to http://drop.io
(super easy to use and allows people to download your image really
easily), and I'll see if I can fix it.

clc;
close all;
clear all;
% Read in standard MATLAB demo image.
grayImage = imread('tire.tif');
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image');
% Threshold it - will have holes in it.
level = graythresh(grayImage);
binaryImage = im2bw(grayImage,level);
subplot(2, 2, 2);
imshow (binaryImage, []) ;
title('Thresholded Image');
% Fill the holes
filledImage = imfill(binaryImage,'holes');
subplot(2, 2, 3);
imshow (filledImage, []) ;
title('Filled Image');
% Remove blobs touching the border.
noBorder = imclearborder(filledImage);
subplot(2, 2, 4);
imshow (noBorder, []) ;
title('No Objects Touching the Border');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.