Clear Filters
Clear Filters

Unable to draw just the boundary of an image

3 views (last 30 days)
I want to draw just the boundary of a ROI. segFP1.jpg is the original image. I applied the code below but I don't get the desired result. My result is displayed in e1.jpg. Any suggestions would be appreciated. Thank you.

Accepted Answer

DGM on 19 Jul 2021
Edited: DGM on 19 Jul 2021
Well, you're loading a grayscale image subject to destructive compression as a jpg file. At no point do you explicitly threshold the image, so it's probably just getting thresholded at I>0, and so a bunch of compression artifacts are part of the binarized image.
I=imread('segFP1.jpg'); % this is not a binary image
I = I>128; % threshold it somewhere
I1=bwperim(I,8); % otherwise this will
DGM on 19 Jul 2021
Edited: DGM on 19 Jul 2021
I'm not sure what's going on here, so let's back up and clarify variable names a bit.
I=imread('segFP1.jpg'); % intensity image
BW = I>128; % binarized image
BWP=bwperim(BW,8); % perimeter of BW
M = true(size(I)); % select everything
S = regionprops(M,I,'weightedcentroid');
C = S(1).WeightedCentroid
[y x] = find(BWP);
D = sqrt((C(1,1)-x).^2 + (C(1,2)-y).^2)
imshow(BWP); hold on
Given that you're taking the weighted centroid instead of the regular centroid (and given the syntax) it's implied that you're trying to operate on an intensity image, but instead, you're using the perimeter (so I made the substitution). If you're just trying to find the centroid of the perimeter, then you don't need to use M. You could just do:
S = regionprops(BWP,'centroid');
The weightedcentroid for a binary object is the same as its regular centroid.
If you're taking the weighted centroid of the intensity image, there is a difference between using M to select everything and doing something like this:
S = regionprops(BW,I,'weightedcentroid'); % the better way
because using BW to select the object cuts off all the garbage left behind by jpg compression, and any other noise in the image background -- which would otherwise influence the weighted average (centroid).
Unless you did something elsewhere, doing this:
x = I1(:, 2);
y = I1(:, 1);
just selects the first two columns of the perimeter image, which are all zero. Using find() will get the coordinates.
Warid Islam
Warid Islam on 19 Jul 2021
Hi @DGM,
That worked perfectly. Thanks a lot.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!