how can remove the object that has the maximum distance from center of image?
Show older comments
if we have some objects in a binary image how can remove the object that has the maximum distance from center of image? for example if this is my image:

how can I obtain this image as result:

thanks
4 Comments
Guillaume
on 17 Oct 2014
Define 'distance of an object from the centre of an image'. Do you mean distance from the centroid of the object to the centre or something else?
Guillaume
on 17 Oct 2014
By define, I meant tell us what you mean. Is the distance of the object to the centre
- the distance from the object centroid to the centre?
- the distance from the furthest point of the object to the centre?
- the distance from the closest point of the object to the centre?
- something else?
Answers (6)
how can I obtain this image as result:
I don't recommend that you use distance as a criterion. It would work, but for the object you've shown, it seems quicker and easier to use solidity,
S=regionprops(Image,'Solidity','PixelIdxList');
[~,idx]=min([S.Solidity]);
Image(S(idx).PixelIdxList)=0;
This is also shift-invariant, whereas the distance criterion is not.
9 Comments
Image Analyst
on 17 Oct 2014
The solidity approach should get rid of that C-shaped blob as long as that thing is always going to be more C-shaped or tortuous than the two central blobs. Can we count on that? What are these things anyway? It looks like lungs in a body in a CT image and for some reason the part of the body (skin and outer fat layer) or a partial air space got segmented along with the lungs.

Is that what's happening? sara, can you show the original gray scale image that gave rise to this binary image? This seems like it might be a case of a person saying "I need to do X" but not telling you that you they really want Z and it we only knew Z we could say "You'd be better off using approach Y than approach X like you asked for.
If you know the "bad blob" is always at the top, you can also ask regionprops for the bounding box and eliminate the blob with the highest Ytop value. There are also lots of other methods depending on what we can assume. If you had a bunch of blobs with random shapes (spots/circles, asterisks, C-shapes, cigar shapes, rings, etc.) at random locations would you still use the "farthest min distance from center of image" algorithm, or solidity algorithm? Maybe not. This could be a case where someone gives a specific image and then we/someone solves it and then comes along later and says "But your algorithm didn't work for this other image" - that happens all the time and it's because the original poster didn't give the range of images that they might expect to encounter.
Yes, true. I ran with the assumption that this is a segmentation of a thoracic CT image and that the unwanted segment is from the bed. It seems like a common thing. I assumed, as well, that the same shaped bed was used in the whole ensemble of images.
Since I couldn't be sure of that, I also provided the distance-based algorithm originally requested. As you say, that could end up being inadequate too (but that's out of our hands).
sara
on 17 Oct 2014
Matt J
on 17 Oct 2014
Can you explain me how can I use regionprops for the bounding box and eliminate the blob with the highest Ytop value???
sara, it should be something that is easy enough for you to figure out yourself from the regionprops documentation. Especially, since I did pretty much the exact same thing for you with 'Solidity' instead of 'BoundingBox'.
Image Analyst
on 17 Oct 2014
What do you want to find? A mask with the outer body? Mask with the body but with "holes" where the dark lungs are? You can threshold and take the largest blob. Call imfill() if you want it filled it. It doesn't look human to me. What kind of animal is this?
sara
on 18 Oct 2014
Image Analyst
on 18 Oct 2014
I still don't see from your later images how you got the first image with the table on top of the body. Did you flip it vertically?
Of course if your table is in a known location, you can just use a known mask to erase it from all images.
As far as thresholding, if the body is surrounded in a white layer like the ones you posted, you can always find a threshold that works. There are a variety of ways. For example, take the histogram and use the triangle method, which I use a lot. http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method
Or take the mean horizontal profile of a band going through the middle of the image and look for the "half way up" gray level when the profile goes into the body and leaves the body:
row1 = round(rows/2-30);
row2 = round(rows/2+30);
horizontalProfile = mean(grayImage(row1:row2, :), 1);
plot(horizontalProfile, 'b-');
grid on;
Examine that profile and use find() to find the leading edge and trailing edge gray levels.
Walter Roberson
on 15 Jul 2015
sara commented "it is good for ELCAP dataset"
Using distance as the criterion,
L=bwlabel(Image);
[M,N]=size(Image);
[X,Y]=ndgrid((1:M)-M/2-.5,(1:N)-N/2-.5);
distmask=(X.^2+Y.^2).*Image;
idx=L>0;
Lmin=accumarray(L(idx),distmask(idx),[],@min);
[~,idx]=max(Lmin);
Image(L==idx)=0;
4 Comments
sara Commented:
I used this but it didn't not work and the object didn't remove... did you get result??
I use
T=double(Image);
distmask=(X.^2+Y.^2).*T;
instead of
distmask=(X.^2+Y.^2).*Image;
because I had error.. thanks
Matt J
on 17 Oct 2014
Yep, I tested both this solution and the the solidity-based one that I proposed and both worked fine.
Matt J
on 17 Oct 2014
The error message would have given you the line number of the error. You should be able to use whos() or the workspace browser to inspect the variables used in that line and see which are integers, which are doubles, etc...
Image Analyst
on 17 Oct 2014
sara: Try the attached code (below the image in blue). I basically threshold, do a morphological opening to break away the thin table line, then extract the biggest blob. A snippet:
% Get the binaryImage
binaryImage = grayImage > 135;
% Erode to break away the table from the body
binaryImage = imopen(binaryImage, [1;1;1]);
biggestBlob = ExtractNLargestBlobs(binaryImage, numberToExtract);
It gives the image below:

sara
on 19 Oct 2014
0 votes
3 Comments
Image Analyst
on 19 Oct 2014
Then take the two biggest shapes since you know there are supposed to be two of them. If they're connected and just one blob, then it may pick up another small spurious non-lung blob. You should be able to get rid of that by size filtering like I showed in my tutorial, or with bwareaopen().
sara
on 19 Oct 2014
Image Analyst
on 22 Jan 2015
"Close" has several definitions. See Hausdorf distance. But let's just assume you want the pair that has the lowest distance between the centroids. Just get the centroids using regionprops() into two arrays centroidx and centroidy. Then calculate the distances, something like
distances = zeros(numberOfBlobs, numberOfBlobs);
for b1 = 1 : numberOfBlobs
for b2 = b1 : numberOfBlobs
distances(b1,b2) = sqrt((centroidx(b1)-centroidx(b2))^2+(centroidy(b1)-centroid(b2))^2);
end
end
Then find the min pair
[blob1, blob2] = find(distances == min(distances(:)));
Then use ismember() to extract those two blobs:
binaryImage = ismember(labeledImage, [blob1, blob2]) > 0;
or something like that. That's just off the top of my head and may need editing.
If this answers your question, can you "Accept" my answer?
3 Comments
rsnandi
on 12 Jul 2019
'hi image analyst..... 'how can I calculate the distance of each entroid from upper border line of image so that I can remove the nearest objects(centroids)) from the above image. thanks
Image Analyst
on 13 Jul 2019
Get the bounding box
props = regionprops(binaryImage, 'BoundingBox');
allBB = vertcat(props.BoundingBox);
% Find just top lines alone.
topLines = allBB(:, 2); % Extract column 2.
% Find out which ones are farther away than some threshold.
keepers = topLines > someLineNumber; % Whatever you want, for example 40.
% Extract out those into a new binary image.
newBinaryImage = ismember(binaryImage, find(keepers));
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





