user21
user21

Reputation: 13

Split multiple objects in one image and display them as separate images

I am working on a project of white blood cells detection.I got the following image after applying edge detection steps for white blood cell detection: enter image description here

I want to display each cell (with white boundary) as a separate image, so far I have tried the following code: Here imabord is the image given in the link.

r=regionprops(imabord, 'BoundingBox');
exTractedCell=imcrop(imabord,r.BoundingBox);
imshow(exTractedCell);

By applying this code I just got a cropped image of all cells; but I want to display all the cells separately as separate images. Kindly help me to display all the detected cells as separate images.

Upvotes: 1

Views: 2011

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is some code which I think do what you want. I added the 'Area' property in order to reject cells too small. You can change the parameter of course. In the for-loop I commented a code used to create some kind of montage using subplots, if you would like to display all cells in one single figure.

close all
clear
clc

A = im2bw(imread('ImCells.jpg')); %// Read the image

r=regionprops(A, 'BoundingBox','Area');

r(1) = []; %// Clear 1st entry as it's the outside rectangle.

MaxArea = 40; %// Select largest area you want to keep.
r = r([r.Area] > MaxArea); %// Detect cells larger than some value.

L = length(r);

figure


%    hold all %// Use if you use the subplot command
for k = 1:L

    exTractedCell=imcrop(A,[r(k).BoundingBox]);

    imshow(exTractedCell)
    pause(0.01);
    %subplot(10,10,k); %//You could create a montage using subplots.
    %imshow(exTractedCell);

end

For instance using a maximal area of 20 and the subplots it gives something like this: enter image description here

You can add some code in the loop to manipulate/do whatever you want with the cropped images then.

Hope that helps! If not please provide more details as to what you want.

Upvotes: 1

Related Questions