MATLAB Answers

Atul Morey

How do i crop an image (delete the background)

Asked by Atul Morey
on 1 Mar 2012

I m new to Matlab but here is what I'm trying to do. Here is the original image

i'm trying to crop the box. is it possible that i crop this using a binary image like this?

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);



No products are associated with this question.

1 Answer

Answer by Image Analyst
on 2 Mar 2012
 Accepted 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.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!