MATLAB Answers

Curve Extraction from Binary Image

14 views (last 30 days)
Zachariah Harris
Zachariah Harris on 24 Nov 2020
Answered: Amrtanshu Raj on 4 Feb 2021
Hi Mathworks community. I have a black and white image and I am trying to extract the polynomial equation of the third line from the left in the image shown below. I am having trouble extracting the locations of the non-zero pixels and then determining the polynomial coefficients (fitting the line). My code is below.
% Label image with 1,2,3,4 for each connected component
L = bwlabeln(BW3);
% return the row and column locations of label #3 within the image
[row,col]=find(L(:,:) == 3);
% Extract the polynomial coeffecients of each line in the image
n=2;
p = polyfit(col,row,n)
% Show the original black and white image and hold on axis
imshow(BW3)
hold on
% Plot the determined polynomial line as a red line on the same figure
x=0:.1:500;
plot(polyval(p,x),x,'r')

Answers (1)

Amrtanshu Raj
Amrtanshu Raj on 4 Feb 2021
Hi,
In the image there are only 2 labels generated by bwlabeln,
Assuming that you wish to generate a polynomial and plot it for the curve in middle (which is label 2 in L) of the image, I have made edits to your code so that it generates the required plot.
The major error was in the line
p = polyfit(col,row,n)
Code -
% Label image with 1,2,3,4 for each connected component
L = bwlabeln(BW3);
% return the row and column locations of label #3 within the image
[row,col]=find(L == 2)
% Extract the polynomial coeffecients of each line in the image
n=2;
p = polyfit(row,col,n);
% Show the original black and white image and hold on axis
imshow(BW3)
hold on
% Plot the determined polynomial line as a red line on the same figure
x=0:.1:500;
plot(polyval(p,x),x,'r')
Hope this help !!

Community Treasure Hunt

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

Start Hunting!

Translated by