Centerline and bounding curve in image
7 views (last 30 days)
Show older comments
Prabhat Srivastava
on 9 Jan 2021
Commented: Prabhat Srivastava
on 9 Jan 2021
I have an Image to which I want to fit a centerline and extract the bounding curves of the object. Here is the image and the best that I could manage with my code.
I = img(501:3500,501:5500);
im_bin = imbinarize(I);
se = strel('disk',3);
opened_im = imopen(im_bin,se);
BWdfill= imfill(opened_im, 4,'holes');
BWoutline= bwperim(BWdfill);
Segout = I;
Segout(BWoutline) = 255;
for indx = 1:5000
temp = find(BWoutline(:,indx)==1);
if isempty(temp)==1
temp= NaN;
else
end
top(indx) = max(temp);
bottom(indx) = min(temp);
end
top_temp = sgolayfilt(top,polsgf, winsgf);
top1 = fillmissing(top_temp,'movmedian',10);
bottom_temp = sgolayfilt(bottom, polsgf, winsgf);
bottom1 = fillmissing(bottom_temp,'movmedian',10);
[maxR, Idx1] = max(I,[],1);
const1 = mean(Idx1,"omitnan");
for k = 1:length(Idx1)
if Idx1(:,k) == 1
Idx1(:,k) = NaN;
else
end
end
Idx1 = fillmissing(Idx1,"constant",const1) ;
Cline = sgolayfilt(Idx1, polsgf, winsgf);
Ignore the white line in the output image that is a scale bar.
0 Comments
Accepted Answer
Roy Kadesh
on 9 Jan 2021
If this complex code doesn't work, can't you try something easy?
img=imread('1.png');
I = img(501:3500,501:5500);
im_bin = imbinarize(I);
mmm=zeros(size(im_bin,2),3);
for col=1:size(im_bin,2)
ind=find(im_bin(:,col));
mmm(col,:)=[min(ind) mean(ind) max(ind)];
end
figure(1),clf(1)
imshow(I)
hold on
plot(mmm)
This works better and should be easier to fine-tune.
1 Comment
More Answers (1)
Image Analyst
on 9 Jan 2021
You can either scan the image columns, or use bwskel. In the attached code I do it both ways.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/483168/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/483173/image.png)
If you want, you could smooth the rows.
3 Comments
Image Analyst
on 9 Jan 2021
The advantage of the skeleton is that the center will be the center even if the blob is tilted (if that's what you want). The scanning method only looks in the vertical direction (1-D), not 2-D like the skeleton. These approaches could differ at times in where they find the centerline, with the skeleton being the more accurate way. Imagine if your blob had a shape like a C or an S instead of something horizontal. What would the column scanning way give you? Yeah, exactly -- now you get it. A line straight through the half way point vertically (actually even goes outside the blob itself), it will NOT be a centerline following the curve that is the blob. So, beware.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!