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

조회 수: 3 (최근 30일)

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
Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014년 1월 20일
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 2014년 1월 21일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014년 1월 20일
편집: Bruno Pop-Stefanov 2014년 1월 21일
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
Bharath Kumar
Bharath Kumar 2020년 6월 19일
how to higlighted as per specific value of coin

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Nivetha m
Nivetha m 2016년 4월 15일
I want to be extract text from given image...... please help me to solve this problem...

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by