# find the object closest to the center

5 views (last 30 days)
Elysi Cochin on 9 Apr 2021
Edited: Matt J on 9 Apr 2021
i did adaptive threshold and got the output as below for 3 different inputs
Now, i wanted to keep only regions marked 1 and 2 from the 3 images
I tried using area of the blob object, but it differs for each input
Is there any way i can do so as to keep the inner - almost center most blobs -object towards the center
How can i extract the blob closest to the center? Or any method to extract the objects 1 and 2
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Matt J on 9 Apr 2021
Edited: Matt J on 9 Apr 2021
I found it highly challenging to close the blobs surgically enough so that the two lungs and the bed are always guaranteed to form 3 distinct blobs. As an alternative, the code below excludes the bed by using a filtering criterion that the blob centroids of interest will lie in the upper 80% of the image.
bw{1}=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/578437/bw1.bmp');
bw{2}=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/578442/bw2.bmp');
bw{3}=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/578447/bw3.bmp');
for i=1:3
figure(i)
montage({bw{i},getlungs(bw{i})})
end
function bw=getlungs(bw)
bw0=bw;
bw=imclose(bw,strel('sphere',5));
[M,N]=size(bw);
bw=bwpropfilt(bw,'ConvexArea', [0,M*N/2]);
T=regionprops(bw,'Centroid','PixelIdxList');
C=vertcat(T.Centroid);
I=C(:,2)/M;
T=T( I<=0.8); %Upper 80%
bw=false(M,N);
bw(vertcat(T.PixelIdxList))=1;
bw=bwareafilt( bwareafilt(bw,[50,inf]),2);
bw=bw&bw0;
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (1)

Image Analyst on 9 Apr 2021
First I'd get rid of the outer ring-shaped blob. Then I'd fill the remaining blobs. Then call regionprops() to get the centroids. Then find the distances to the centers and sort them and take the two lowest values. I know you know a lot about image processing already, and could definitely do it yourself, but here's a start (untested because you didn't post a binary image, just RGB images with red labels):
[rows, columns] = size(mask);
se = strel('disk', 4, 0);
mask2 = imclose(mask, se);
[labeledImage, numBlobs] = bwlabel(mask2);
mask2 = ismember(labeledImage, 2:numBlobs); % Extract inner blobs only
% Might want to call beareaopen() here to use only blobs bigger than a
% certain size and not include small noise blobs.
% Fill holes on remaining blobs.
mask2 = imfill(mask2, 'holes');
% Measure Centroids.
[labeledImage, numBlobs] = bwlabel(mask2);
props = regionprops(labeledImage, 'Centroid');
xy = vertcat(props.Centroid);
x = xy(:, 1);
y = xy(:, 2);
% Compute distances from each blob's centroid to the middle of the image.
distances = sqrt((columns/2 - x) .^ 2 + (rows/2 - y) .^ 2)
% Sort distances so we know which two are closest.
[sortedDistances, sortOrder] = sort(distances, 'ascend');
% Now we know the two closest. Extract only those.
finalMask = ismember(labeledImage, sortOrder(1:2));
% Display the final mask that should have only two blobs in it.
imshow(finalMask);
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!