MATLAB: Separate the foreground from background in an image

foreground and backgroundimage segmentation

I'm taking a gray scale image and the background is white. How can I get the foreground i.e. the object and remove the background. I want to do all this without any change in the pixel values, so that I can use it for future use.
Can anyone please sort out this problem ……..

Best Answer

Let "Th" be the threshold level between foreground and background -- the point at which the gray of the foreground has become bright enough white to be considered background. Then,
mask = YourImage < Th;
will be a logical array "mask" that is true for the foreground locations.
That's as far as you can get without changing any pixel values.
What kind of data structure were you thinking of that had the background removed? MATLAB cannot have irregularily-shaped arrays. You can construct rectangular arrays that have the original values in all of the foreground locations, but something has to be in the background locations.