Display each object in a binary image separately

Hello everybody,
I have an image, with undefined number of objects, just like the image below.
I want to create an image for each one of the objects. For this case, I want to create 4 images each one with only one object. The new images should have the same size as the original image.
Is there anyone who could help me?
Cheers,
Joaquim

3 Comments

You can look into regionprops. This will identify the number of objects and give you an idea about how to proceed. If you can't make sense of the usage of this function, do get back and we will try to help you.
I used this instead
[L,num] = bwlabel(BW);
[sx,sy]=size(BW);
a=L;
element=zeros(sx,sy,num);
for i=1:num
a(L~=i)=0;
a(L==i)=1;
element(:,:,i)=a(:,:);
a=L;
end
I dont know how to use regionprops, although I know that it may use less memory.
As long as it gets the job done it usually doesn't matter how efficient your code is in terms of memory (in most of my cases anyway).

Sign in to comment.

 Accepted Answer

Or, simply use ismember() to extract the blob you want. For example
[L, num] = bwlabel(BW);
for k = 1 : num
thisBlob = ismember(L, k);
figure
imshow(thisBlob, []);
end

13 Comments

Thank you very much Image Analyst. If possible, I would require your help for more things.
After extracting each object, I flipped the object around the x axis in the image(right or left). Then I want to use xcorr2 between each flipped object and each original object (except the one that was flipped), and find the best correspondence. For now, I only could make until after the flipping:
[L,num] = bwlabel(BW);
for i=1:num
element(:,:,i)=ismember(L,i);
flip_element(:,:,i)=flip(element(:,:,i),2);
end
Can you help me?
Do you mean like saying you can use fliplr() or xcorr2()?
Exactly. But then calculating each oject is more similar with the flipped object and displaying it.
I don't know what "calculating each oject" means. Can you describe the steps involved with that?
Sorry, I'll try to be clearer. 1st: Separate each object in a binary image. (done) 2nd: Flip each object along the x-axis. (done) 3rd: calculate the cross correlation between the flipped object and each one of the other objects (each object in the original image, except the object that was flipped) 4th: Identify, for each flipped object, the object in the original image wich has the maximum of correlation with the flipped object. ( additional steps)
5th- if the maximum correlated object has more than 10 voxels, hold the object. Otherwise set the value of the original object to 0. Last step: return an image with all the original objects that follow the two criteria 9maximum correlation and overlapping of more than 10 voxels).
I dont know if I explained well the last two steps, but what I really need is the other steps. If you could help me I would be very grateful!
You can use xcorr2:
outputImage = xcorr2(binaryImage, flippedBinaryImage);
@Joaquim How did you extract each object , can you provide the code for it ?
Since it's been 4 years, I'll answer. It looks like he's already starting with a binary image. Not sure how he got it or what the original gray scale image looked like, or if there even was one. But it might have been through imbinarize() or thresholding. Then once the image is binary, you can simply extract each blob one at a time using bwlabel() and ismember() like I showed.
What is L in your code @Image Analyst ? Same goes for 'num' . Are "ismember" and "thisblob" in-built matlab functions? 'image segmentation tutorial' is a bit difficult to understand.
Looks like I might have used the MATLAB documentation, which is not always a model of good coding, especially when it comes to variable names. I would rather do it like this:
[labeledImage, numberOfBlobs] = bwlabel(binaryImage);
So labeled image is an image where each blob's value (currently they're just 1 or true) is given an ID number like 1, 2, 3, 4, etc. That process is called "connected components labeling". It allows you to deal with each individual blob one at a time.
ismember() is a built-in function - it's in base MATLAB. ismember takes a labeled image and returns a binary image with only the requested blob label (ID number) in the image. I accept it into a variable I call thisBlob, which is a binary image of only one blob, not all of them like we had before.
@Image Analyst, Understood.
I have made changes using your code to display only a specific blob(labelled component) instead of all the blobs(labelled components) .It is displaying blob1 perfectly in separate figure, but i can't understand ,what should be replaced with 'k' in 'ismember'? 'ismember' takes two input arguments right? I have achieved what i wanted but i want to make the code,'simpler'.
Also, What will be changes if i want to display blob 1,6 and 7 on the figure?
BW=imread('CAP.jpg');
threshold = graythresh(BW);
BW =~im2bw(BW,threshold);
BW = bwareaopen(BW,30);
imshow(~BW);
[L, num] = bwlabel(BW);
thisBlob = ismember(L==1,k);
figure
imshow(thisBlob, []);
My code puts it in a loop over all blobs and shows you each blob one at a time because k varies in the loop. Your code only shows you one blob -- the k'th blob. But you need to specify k. Which one of the 4 do you want? See the attached demo to understand how the blobs are numbered/labeled.
CODE :
L= bwlabel(BW);
thisBlob= ismember(L,1);
figure
imshow(thisBlob, []);
@Image Analyst,I understand how are blobs labelled. In the above code , I have displayed blob 1 on the figure. Using the similar syntax , Is it possible to use "ismember" to display more than one blob? for example: I have an image with 9 objects in it and i want to display only blob no.1 ,6 and 7 to be displayed in one figure.

Sign in to comment.

More Answers (0)

Asked:

on 23 Mar 2017

Commented:

on 6 Sep 2021

Community Treasure Hunt

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

Start Hunting!