find the object closest to the center

조회 수: 2 (최근 30일)
Elysi Cochin
Elysi Cochin 2021년 4월 9일
편집: Matt J 2021년 4월 9일
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

채택된 답변

Matt J
Matt J 2021년 4월 9일
편집: Matt J 2021년 4월 9일
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.
for i=1:3
function bw=getlungs(bw)
bw=bwpropfilt(bw,'ConvexArea', [0,M*N/2]);
T=T( I<=0.8); %Upper 80%
bw=bwareafilt( bwareafilt(bw,[50,inf]),2);

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 4월 9일
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.

Community Treasure Hunt

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

Start Hunting!

Translated by