How to extract only object with white background using bounding box in MatLab?

20 views (last 30 days)

Hi everyone,

I am a newbie in matlab.

I try to do segmentation to get seperately image and text in an image.

I use regionprops, so i got each boundingbox and one of the image is like this:

My problem is that I crop the original image to get each connected component using boundingbox. However, I want only the object for instance the coin with white background.

Any idea ?

_ EDITED _

I crop the original image to get each connected component using boundingbox .

My problem is that when the original image are cropped in rectangle shape, some background of the crop image have coin(that what i want) and some text or line within the rectangle shape (that i dont want),

How can I remove those text or line within the rectangle shape of the crop image?

  2 Comments
Bruno Pop-Stefanov
Bruno Pop-Stefanov on 20 Jan 2014
Just to clarify what you would like to do: you would like to get bounding boxes on objects extracted from an image, but you only want the object itself and not what is in the background? For example, in the image you provided, you would like to extract bounding boxes around each of the four coins?
Vortana
Vortana on 21 Jan 2014
Sorry for confusing,
I already found the connected components and i can crop the image using their bounding box.
I crop the original image to get each connected component using boundingbox .
My problem is that when the original image are cropped in rectangle shape, some background of the crop image have coin(that what i want) and some text or line within the rectangle shape (that i dont want),
How can I remove those text or line within the rectangle shape of the crop image?

Sign in to comment.

Accepted Answer

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 20 Jan 2014
Edited: Bruno Pop-Stefanov on 21 Jan 2014
I think what you are looking for is activecontour. Active contours allow you to separate an image into foreground and background.
Using the image of coins you provided, the following code generates a binary image bw in which white pixels belong to the foreground (coins) and black pixels belong to the background:
% Open image
I = imread('coins.jpg');
% Convert to grayscale
I = rgb2gray(I);
% Show image
figure(1);
imshow(I)
title('Image with objects')
% mask is the initial contour state
mask = zeros(size(I));
mask(25:end-25,25:end-25) = 1;
% Show mask
figure(2);
imshow(mask);
title('Initial contour location')
% bw is a mask of the detected objects
numIter = 2500;
bw = activecontour(I, mask, numIter);
% Show detected objects
figure(3);
imshow(bw);
title('Detected objects')
When using activecontour, keep in mind that the default number of iterations is 100. Depending on the image you are working on, you might want to change that to a larger number of iterations. In the example code above I chose 2500 and all four coins are successfully separated.
Then, you can use the bwconncomp function to find each coin in the image as a connected component. However, noise might be present in bw after running active contours. You can use the bwareaopen function to perform a morphological opening on the binary image bw and remove connected components that have less than a certain number of pixels.
I included the entire script (extractCoins.m) with all operations, from opening the image to cropping the coins from their background and displaying them. This is the final result:
  17 Comments

Sign in to comment.

More Answers (1)

Nivetha m
Nivetha m on 15 Apr 2016
I want to be extract text from given image...... please help me to solve this problem...

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!