MATLAB: How do i crop an image (delete the background)

color segmentationcrop

I m new to Matlab but here is what I'm trying to do. Here is the original image https://picasaweb.google.com/117741942742967044268/MatlabTests#5715055956158535938
i'm trying to crop the box. is it possible that i crop this using a binary image like this? https://picasaweb.google.com/117741942742967044268/MatlabTests#5715056388429220994
if yes, but the mask is still not very clear. there are some black pixels inside the white area. how can i superfine it?
here is the code to generate the binary image from rgbImage:
% Convert image from RGB colorspace to lab color space.
cform = makecform('srgb2lab');
lab_Image = applycform(im2double(rgbImage),cform);
% Extract out the color bands from the original image
% into 3 separate 2D arrays, one for each color component.
LChannel = lab_Image(:, :, 1);
aChannel = lab_Image(:, :, 2);
bChannel = lab_Image(:, :, 3);
%Convert aChannel(double) to unit8 RGB image
aChannelToRgb8 = repmat (uint8(255.*aChannel),[1 1 3]);
%multiply the original rgb image with aChannelToRgb8
multiplyResult = immultiply (rgbImage,aChannelToRgb8);
%convert multiplyResult to binary with a level
BW = im2bw (multiplyResult, 0.4);
imshow (BW);

Best Answer

Once you have the binary image you can use any() and find() to determine where the top line, bottom line, left column, and right column are. The call imcrop(). By the way you don't need to do all that stuff about going to LAB color space.